InspIRCd  2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations
base.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  * Copyright (C) 2006-2007 Oliver Lupton <oliverlupton@gmail.com>
6  * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  * Copyright (C) 2003-2005, 2007 Craig Edwards <craigedwards@brainbox.cc>
8  *
9  * This file is part of InspIRCd. InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #ifndef BASE_H
24 #define BASE_H
25 
26 #include <map>
27 #include <deque>
28 #include <string>
29 #include <list>
30 
33 {
34  CullResult();
35  friend class classbase;
36 };
37 
42 class CoreExport classbase
43 {
44  public:
45  classbase();
46 
50  virtual CullResult cull();
51  virtual ~classbase();
52  private:
53  // uncopyable
54  classbase(const classbase&);
55  void operator=(const classbase&);
56 };
57 
61 class CoreExport interfacebase
62 {
63  public:
64  interfacebase() {}
65  static inline void* operator new(size_t, void* m) { return m; }
66  private:
68  void operator=(const interfacebase&);
69  static void* operator new(size_t);
70  static void operator delete(void*);
71 };
72 
86 class CoreExport refcountbase
87 {
88  mutable unsigned int refcount;
89  public:
90  refcountbase();
91  virtual ~refcountbase();
92  inline unsigned int GetReferenceCount() const { return refcount; }
93  static inline void* operator new(size_t, void* m) { return m; }
94  static void* operator new(size_t);
95  static void operator delete(void*);
96  inline void refcount_inc() const { refcount++; }
97  inline bool refcount_dec() const { refcount--; return !refcount; }
98  private:
99  // uncopyable
100  refcountbase(const refcountbase&);
101  void operator=(const refcountbase&);
102 };
103 
109 class CoreExport usecountbase
110 {
111  mutable unsigned int usecount;
112  public:
113  usecountbase() : usecount(0) { }
114  ~usecountbase();
115  inline unsigned int GetUseCount() const { return usecount; }
116  inline void refcount_inc() const { usecount++; }
117  inline bool refcount_dec() const { usecount--; return false; }
118  private:
119  // uncopyable
120  usecountbase(const usecountbase&);
121  void operator=(const usecountbase&);
122 };
123 
124 template <typename T>
126 {
127  T* value;
128  public:
129  reference() : value(0) { }
130  reference(T* v) : value(v) { if (value) value->refcount_inc(); }
131  reference(const reference<T>& v) : value(v.value) { if (value) value->refcount_inc(); }
132  reference<T>& operator=(const reference<T>& other)
133  {
134  if (other.value)
135  other.value->refcount_inc();
136  this->reference::~reference();
137  value = other.value;
138  return *this;
139  }
140 
141  ~reference()
142  {
143  if (value && value->refcount_dec())
144  delete value;
145  }
146 
147  inline reference<T>& operator=(T* other)
148  {
149  if (value != other)
150  {
151  if (value && value->refcount_dec())
152  delete value;
153  value = other;
154  if (value)
155  value->refcount_inc();
156  }
157 
158  return *this;
159  }
160 
161  inline operator bool() const { return (value != NULL); }
162  inline operator T*() const { return value; }
163  inline T* operator->() const { return value; }
164  inline T& operator*() const { return *value; }
165  inline bool operator<(const reference<T>& other) const { return value < other.value; }
166  inline bool operator>(const reference<T>& other) const { return value > other.value; }
167  static inline void* operator new(size_t, void* m) { return m; }
168  private:
169 #ifndef _WIN32
170  static void* operator new(size_t);
171  static void operator delete(void*);
172 #endif
173 };
174 
181 class CoreExport CoreException : public std::exception
182 {
183  public:
186  const std::string err;
189  const std::string source;
192  CoreException() : err("Core threw an exception"), source("The core") {}
195  CoreException(const std::string &message) : err(message), source("The core") {}
199  CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
204  virtual ~CoreException() throw() {};
208  virtual const char* GetReason()
209  {
210  return err.c_str();
211  }
212 
213  virtual const char* GetSource()
214  {
215  return source.c_str();
216  }
217 };
218 
219 class Module;
220 class CoreExport ModuleException : public CoreException
221 {
222  public:
225  ModuleException(const std::string &message, Module* me = NULL);
226 };
227 
228 typedef const reference<Module> ModuleRef;
229 
230 enum ServiceType {
232  SERVICE_COMMAND,
234  SERVICE_MODE,
236  SERVICE_METADATA,
238  SERVICE_DATA,
240  SERVICE_IOHOOK
241 };
242 
244 class CoreExport ServiceProvider : public classbase
245 {
246  public:
248  ModuleRef creator;
250  const std::string name;
252  const ServiceType service;
253  ServiceProvider(Module* Creator, const std::string& Name, ServiceType Type)
254  : creator(Creator), name(Name), service(Type) {}
255  virtual ~ServiceProvider();
256 };
257 
258 
259 #endif
const std::string name
Definition: base.h:250
Definition: base.h:109
CoreException()
Definition: base.h:192
Definition: base.h:42
Definition: base.h:220
Definition: base.h:125
Definition: base.h:86
CoreException(const std::string &message, const std::string &src)
Definition: base.h:199
const std::string err
Definition: base.h:186
Definition: modules.h:350
Definition: base.h:61
const ServiceType service
Definition: base.h:252
Definition: base.h:181
Definition: base.h:244
virtual const char * GetReason()
Definition: base.h:208
CoreException(const std::string &message)
Definition: base.h:195
const std::string source
Definition: base.h:189
ModuleRef creator
Definition: base.h:248
Definition: base.h:32
virtual ~CoreException()
Definition: base.h:204