]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/base.h
...because every now and again, i have to do a massive commit.
[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  private:
56         interfacebase(const interfacebase&);
57         void operator=(const interfacebase&);
58         void* operator new(size_t);
59         void operator delete(void*);
60 };
61
62 /** The base class for inspircd classes that support reference counting.
63  * Any objects that do not have a well-defined lifetime should inherit from
64  * this, and should be assigned to a reference<type> object to establish their
65  * lifetime.
66  *
67  * Reference objects should not hold circular references back to themselves,
68  * even indirectly; this will cause a memory leak because the count will never
69  * drop to zero.
70  *
71  * Using a normal pointer for the object is recommended if you can assure that
72  * at least one reference<> will remain as long as that pointer is used; this
73  * will avoid the slight overhead of changing the reference count.
74  */
75 class CoreExport refcountbase
76 {
77         mutable unsigned int refcount;
78  public:
79         refcountbase();
80         virtual ~refcountbase();
81         inline unsigned int GetReferenceCount() const { return refcount; }
82         void* operator new(size_t);
83         void operator delete(void*);
84         inline void refcount_inc() const { refcount++; }
85         inline bool refcount_dec() const { refcount--; return !refcount; }
86  private:
87         // uncopyable
88         refcountbase(const refcountbase&);
89         void operator=(const refcountbase&);
90 };
91
92 /** Base class for use count tracking. Uses reference<>, but does not
93  * cause object deletion when the last user is removed.
94  *
95  * Safe for use as a second parent class; will not add a second vtable.
96  */
97 class CoreExport usecountbase
98 {
99         mutable unsigned int usecount;
100  public:
101         usecountbase() : usecount(0) { }
102         ~usecountbase();
103         inline unsigned int GetUseCount() const { return usecount; }
104         inline void refcount_inc() const { usecount++; }
105         inline bool refcount_dec() const { usecount--; return false; }
106  private:
107         // uncopyable
108         usecountbase(const usecountbase&);
109         void operator=(const usecountbase&);
110 };
111
112 template <typename T>
113 class reference
114 {
115         T* value;
116  public:
117         reference() : value(0) { }
118         reference(T* v) : value(v) { if (value) value->refcount_inc(); }
119         reference(const reference<T>& v) : value(v.value) { if (value) value->refcount_inc(); }
120         reference<T>& operator=(const reference<T>& other)
121         {
122                 if (other.value)
123                         other.value->refcount_inc();
124                 this->reference::~reference();
125                 value = other.value;
126                 return *this;
127         }
128
129         ~reference()
130         {
131                 if (value && value->refcount_dec())
132                         delete value;
133         }
134         inline operator bool() const { return value; }
135         inline operator T*() const { return value; }
136         inline T* operator->() const { return value; }
137         inline T& operator*() const { return *value; }
138         inline bool operator<(const reference<T>& other) const { return value < other.value; }
139         inline bool operator>(const reference<T>& other) const { return value > other.value; }
140  private:
141         void* operator new(size_t);
142         void operator delete(void*);
143 };
144
145 /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
146  * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
147  * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
148  * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
149  * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
150  */
151 class CoreExport CoreException : public std::exception
152 {
153  protected:
154         /** Holds the error message to be displayed
155          */
156         const std::string err;
157         /** Source of the exception
158          */
159         const std::string source;
160  public:
161         /** Default constructor, just uses the error mesage 'Core threw an exception'.
162          */
163         CoreException() : err("Core threw an exception"), source("The core") {}
164         /** This constructor can be used to specify an error message before throwing.
165          */
166         CoreException(const std::string &message) : err(message), source("The core") {}
167         /** This constructor can be used to specify an error message before throwing,
168          * and to specify the source of the exception.
169          */
170         CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
171         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
172          * Actually no, it does nothing. Never mind.
173          * @throws Nothing!
174          */
175         virtual ~CoreException() throw() {};
176         /** Returns the reason for the exception.
177          * The module should probably put something informative here as the user will see this upon failure.
178          */
179         virtual const char* GetReason()
180         {
181                 return err.c_str();
182         }
183
184         virtual const char* GetSource()
185         {
186                 return source.c_str();
187         }
188 };
189
190 class CoreExport ModuleException : public CoreException
191 {
192  public:
193         /** This constructor can be used to specify an error message before throwing.
194          */
195         ModuleException(const std::string &message, Module* me = NULL);
196 };
197
198 typedef const reference<Module> ModuleRef;
199
200 enum ServiceType {
201         /** is a Command */
202         SERVICE_COMMAND,
203         /** is a channel ModeHandler */
204         SERVICE_CMODE,
205         /** is a user ModeHandler */
206         SERVICE_UMODE,
207         /** is a metadata descriptor */
208         SERVICE_METADATA,
209         /** is a data processing provider (MD5, SQL) */
210         SERVICE_DATA,
211         /** is an I/O hook provider (SSL) */
212         SERVICE_IOHOOK
213 };
214
215 /** A structure defining something that a module can provide */
216 class CoreExport ServiceProvider : public classbase
217 {
218  public:
219         /** Module that is providing this service */
220         ModuleRef creator;
221         /** Name of the service being provided */
222         const std::string name;
223         /** Type of service (must match object type) */
224         const ServiceType service;
225         ServiceProvider(Module* Creator, const std::string& Name, ServiceType Type)
226                 : creator(Creator), name(Name), service(Type) {}
227         virtual ~ServiceProvider();
228 };
229
230
231 #endif