]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/base.h
Merge pull request #55 from Justasic/insp20
[user/henk/code/inspircd.git] / include / base.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef BASE_H
15 #define BASE_H
16
17 #include <map>
18 #include <deque>
19 #include <string>
20
21 /** Dummy class to help enforce culls being parent-called up to classbase */
22 class CullResult
23 {
24         CullResult();
25         friend class classbase;
26 };
27
28 /** The base class for all inspircd classes with a well-defined lifetime.
29  * Classes that inherit from this may be destroyed through GlobalCulls,
30  * and may rely on cull() being called prior to their deletion.
31  */
32 class CoreExport classbase
33 {
34  public:
35         classbase();
36
37         /**
38          * Called just prior to destruction via cull list.
39          */
40         virtual CullResult cull();
41         virtual ~classbase();
42  private:
43         // uncopyable
44         classbase(const classbase&);
45         void operator=(const classbase&);
46 };
47
48 /** The base class for inspircd classes that provide a wrapping interface, and
49  * should only exist while being used. Prevents heap allocation.
50  */
51 class CoreExport interfacebase
52 {
53  public:
54         interfacebase() {}
55         static inline void* operator new(size_t, void* m) { return m; }
56  private:
57         interfacebase(const interfacebase&);
58         void operator=(const interfacebase&);
59         static void* operator new(size_t);
60         static void operator delete(void*);
61 };
62
63 /** The base class for inspircd classes that support reference counting.
64  * Any objects that do not have a well-defined lifetime should inherit from
65  * this, and should be assigned to a reference<type> object to establish their
66  * lifetime.
67  *
68  * Reference objects should not hold circular references back to themselves,
69  * even indirectly; this will cause a memory leak because the count will never
70  * drop to zero.
71  *
72  * Using a normal pointer for the object is recommended if you can assure that
73  * at least one reference<> will remain as long as that pointer is used; this
74  * will avoid the slight overhead of changing the reference count.
75  */
76 class CoreExport refcountbase
77 {
78         mutable unsigned int refcount;
79  public:
80         refcountbase();
81         virtual ~refcountbase();
82         inline unsigned int GetReferenceCount() const { return refcount; }
83         static inline void* operator new(size_t, void* m) { return m; }
84         static void* operator new(size_t);
85         static void operator delete(void*);
86         inline void refcount_inc() const { refcount++; }
87         inline bool refcount_dec() const { refcount--; return !refcount; }
88  private:
89         // uncopyable
90         refcountbase(const refcountbase&);
91         void operator=(const refcountbase&);
92 };
93
94 /** Base class for use count tracking. Uses reference<>, but does not
95  * cause object deletion when the last user is removed.
96  *
97  * Safe for use as a second parent class; will not add a second vtable.
98  */
99 class CoreExport usecountbase
100 {
101         mutable unsigned int usecount;
102  public:
103         usecountbase() : usecount(0) { }
104         ~usecountbase();
105         inline unsigned int GetUseCount() const { return usecount; }
106         inline void refcount_inc() const { usecount++; }
107         inline bool refcount_dec() const { usecount--; return false; }
108  private:
109         // uncopyable
110         usecountbase(const usecountbase&);
111         void operator=(const usecountbase&);
112 };
113
114 template <typename T>
115 class CoreExport reference
116 {
117         T* value;
118  public:
119         reference() : value(0) { }
120         reference(T* v) : value(v) { if (value) value->refcount_inc(); }
121         reference(const reference<T>& v) : value(v.value) { if (value) value->refcount_inc(); }
122         reference<T>& operator=(const reference<T>& other)
123         {
124                 if (other.value)
125                         other.value->refcount_inc();
126                 this->reference::~reference();
127                 value = other.value;
128                 return *this;
129         }
130
131         ~reference()
132         {
133                 if (value && value->refcount_dec())
134                         delete value;
135         }
136         inline operator bool() const { return value; }
137         inline operator T*() const { return value; }
138         inline T* operator->() const { return value; }
139         inline T& operator*() const { return *value; }
140         inline bool operator<(const reference<T>& other) const { return value < other.value; }
141         inline bool operator>(const reference<T>& other) const { return value > other.value; }
142         static inline void* operator new(size_t, void* m) { return m; }
143  private:
144 #ifndef WIN32
145         static void* operator new(size_t);
146         static void operator delete(void*);
147 #endif
148 };
149
150 /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
151  * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
152  * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
153  * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
154  * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
155  */
156 class CoreExport CoreException : public std::exception
157 {
158  public:
159         /** Holds the error message to be displayed
160          */
161         const std::string err;
162         /** Source of the exception
163          */
164         const std::string source;
165         /** Default constructor, just uses the error mesage 'Core threw an exception'.
166          */
167         CoreException() : err("Core threw an exception"), source("The core") {}
168         /** This constructor can be used to specify an error message before throwing.
169          */
170         CoreException(const std::string &message) : err(message), source("The core") {}
171         /** This constructor can be used to specify an error message before throwing,
172          * and to specify the source of the exception.
173          */
174         CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
175         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
176          * Actually no, it does nothing. Never mind.
177          * @throws Nothing!
178          */
179         virtual ~CoreException() throw() {};
180         /** Returns the reason for the exception.
181          * The module should probably put something informative here as the user will see this upon failure.
182          */
183         virtual const char* GetReason()
184         {
185                 return err.c_str();
186         }
187
188         virtual const char* GetSource()
189         {
190                 return source.c_str();
191         }
192 };
193
194 class Module;
195 class CoreExport ModuleException : public CoreException
196 {
197  public:
198         /** This constructor can be used to specify an error message before throwing.
199          */
200         ModuleException(const std::string &message, Module* me = NULL);
201 };
202
203 typedef const reference<Module> ModuleRef;
204
205 enum ServiceType {
206         /** is a Command */
207         SERVICE_COMMAND,
208         /** is a ModeHandler */
209         SERVICE_MODE,
210         /** is a metadata descriptor */
211         SERVICE_METADATA,
212         /** is a data processing provider (MD5, SQL) */
213         SERVICE_DATA,
214         /** is an I/O hook provider (SSL) */
215         SERVICE_IOHOOK
216 };
217
218 /** A structure defining something that a module can provide */
219 class CoreExport ServiceProvider : public classbase
220 {
221  public:
222         /** Module that is providing this service */
223         ModuleRef creator;
224         /** Name of the service being provided */
225         const std::string name;
226         /** Type of service (must match object type) */
227         const ServiceType service;
228         ServiceProvider(Module* Creator, const std::string& Name, ServiceType Type)
229                 : creator(Creator), name(Name), service(Type) {}
230         virtual ~ServiceProvider();
231 };
232
233
234 #endif