]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/base.h
First step on the road of module loader rewriting. So far this only really removes...
[user/henk/code/inspircd.git] / include / base.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 "inspircd_config.h"
18 #include <time.h>
19 #include <map>
20 #include <deque>
21 #include <string>
22
23 /** Do we use this? -- Brain */
24 typedef void* VoidPointer;
25
26 /** A private data store for an Extensible class */
27 typedef std::map<std::string,char*> ExtensibleStore;
28
29 /** Needed */
30 class InspIRCd;
31
32 /** The base class for all inspircd classes.
33  * Wherever possible, all classes you create should inherit from this,
34  * giving them the ability to be passed to various core functions
35  * as 'anonymous' classes.
36 */ 
37 class CoreExport classbase
38 {
39  public:
40         /** Time that the object was instantiated (used for TS calculation etc)
41         */
42         time_t age;
43
44         /** Constructor.
45          * Sets the object's time
46          */
47         classbase();
48
49         /** Destructor.
50          * Does sweet FA.
51          */
52         virtual ~classbase() { }
53 };
54
55 /** class Extensible is the parent class of many classes such as userrec and chanrec.
56  * class Extensible implements a system which allows modules to 'extend' the class by attaching data within
57  * a map associated with the object. In this way modules can store their own custom information within user
58  * objects, channel objects and server objects, without breaking other modules (this is more sensible than using
59  * a flags variable, and each module defining bits within the flag as 'theirs' as it is less prone to conflict and
60  * supports arbitary data storage).
61  */
62 class CoreExport Extensible : public classbase
63 {
64         /** Private data store.
65          * Holds all extensible metadata for the class.
66          */
67         ExtensibleStore Extension_Items;
68         
69 public:
70
71         /** Extend an Extensible class.
72          *
73          * @param key The key parameter is an arbitary string which identifies the extension data
74          * @param p This parameter is a pointer to any data you wish to associate with the object
75          *
76          * You must provide a key to store the data as via the parameter 'key' and store the data
77          * in the templated parameter 'p'.
78          * The data will be inserted into the map. If the data already exists, you may not insert it
79          * twice, Extensible::Extend will return false in this case.
80          *
81          * @return Returns true on success, false if otherwise
82          */
83         template<typename T> bool Extend(const std::string &key, T* p)
84         {
85                 /* This will only add an item if it doesnt already exist,
86                  * the return value is a std::pair of an iterator to the
87                  * element, and a bool saying if it was actually inserted.
88                  */
89                 return this->Extension_Items.insert(std::make_pair(key, (char*)p)).second;
90         }
91
92         /** Extend an Extensible class.
93          *
94          * @param key The key parameter is an arbitary string which identifies the extension data
95          *
96          * You must provide a key to store the data as via the parameter 'key', this single-parameter
97          * version takes no 'data' parameter, this is used purely for boolean values.
98          * The key will be inserted into the map with a NULL 'data' pointer. If the key already exists
99          * then you may not insert it twice, Extensible::Extend will return false in this case.
100          *
101          * @return Returns true on success, false if otherwise
102          */
103         bool Extend(const std::string &key)
104         {
105                 /* This will only add an item if it doesnt already exist,
106                  * the return value is a std::pair of an iterator to the
107                  * element, and a bool saying if it was actually inserted.
108                  */
109                 return this->Extension_Items.insert(std::make_pair(key, (char*)NULL)).second;
110         }
111
112         /** Shrink an Extensible class.
113          *
114          * @param key The key parameter is an arbitary string which identifies the extension data
115          *
116          * You must provide a key name. The given key name will be removed from the classes data. If
117          * you provide a nonexistent key (case is important) then the function will return false.
118          * @return Returns true on success.
119          */
120         bool Shrink(const std::string &key);
121         
122         /** Get an extension item.
123          *
124          * @param key The key parameter is an arbitary string which identifies the extension data
125          * @param p If you provide a non-existent key, this value will be NULL. Otherwise a pointer to the item you requested will be placed in this templated parameter.
126          * @return Returns true if the item was found and false if it was nor, regardless of wether 'p' is NULL. This allows you to store NULL values in Extensible.
127          */
128         template<typename T> bool GetExt(const std::string &key, T* &p)
129         {
130                 ExtensibleStore::iterator iter = this->Extension_Items.find(key); /* Find the item */
131                 if(iter != this->Extension_Items.end())
132                 {
133                         p = (T*)iter->second;   /* Item found */
134                         return true;
135                 }
136                 else
137                 {
138                         p = NULL;               /* Item not found */
139                         return false;
140                 }
141         }
142         
143         /** Get an extension item.
144          *
145          * @param key The key parameter is an arbitary string which identifies the extension data
146          * @return Returns true if the item was found and false if it was not.
147          * 
148          * This single-parameter version only checks if the key exists, it does nothing with
149          * the 'data' field and is probably only useful in conjunction with the single-parameter
150          * version of Extend().
151          */
152         bool GetExt(const std::string &key)
153         {
154                 return (this->Extension_Items.find(key) != this->Extension_Items.end());
155         }
156
157         /** Get a list of all extension items names.
158          * @param list A deque of strings to receive the list
159          * @return This function writes a list of all extension items stored in this object by name into the given deque and returns void.
160          */
161         void GetExtList(std::deque<std::string> &list);
162 };
163
164 /** BoolSet is a utility class designed to hold eight bools in a bitmask.
165  * Use BoolSet::Set and BoolSet::Get to set and get bools in the bitmask,
166  * and Unset and Invert for special operations upon them.
167  */
168 class CoreExport BoolSet : public classbase
169 {
170         /** Actual bit values */
171         char bits;
172
173  public:
174
175         /** The default constructor initializes the BoolSet to all values unset.
176          */
177         BoolSet();
178
179         /** This constructor copies the default bitmask from a char
180          */
181         BoolSet(char bitmask);
182
183         /** The Set method sets one bool in the set.
184          *
185          * @param number The number of the item to set. This must be between 0 and 7.
186          */
187         void Set(int number);
188
189         /** The Get method returns the value of one bool in the set
190          *
191          * @param number The number of the item to retrieve. This must be between 0 and 7.
192          *
193          * @return True if the item is set, false if it is unset.
194          */
195         bool Get(int number);
196
197         /** The Unset method unsets one value in the set
198          *
199          * @param number The number of the item to set. This must be between 0 and 7.
200          */
201         void Unset(int number);
202
203         /** The Unset method inverts (flips) one value in the set
204          *
205          * @param number The number of the item to invert. This must be between 0 and 7.
206          */
207         void Invert(int number);
208
209         /** Compare two BoolSets
210          */
211         bool operator==(BoolSet other);
212
213         /** OR two BoolSets together
214          */
215         BoolSet operator|(BoolSet other);
216         
217         /** AND two BoolSets together
218          */
219         BoolSet operator&(BoolSet other);
220
221         /** Assign one BoolSet to another
222          */
223         bool operator=(BoolSet other);
224 };
225
226 /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
227  * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
228  * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
229  * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
230  * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
231  */
232 class CoreExport CoreException : public std::exception
233 {
234  protected:
235         /** Holds the error message to be displayed
236          */
237         const std::string err;
238         /** Source of the exception
239          */
240         const std::string source;
241  public:
242         /** Default constructor, just uses the error mesage 'Core threw an exception'.
243          */
244         CoreException() : err("Core threw an exception"), source("The core") {}
245         /** This constructor can be used to specify an error message before throwing.
246          */
247         CoreException(const std::string &message) : err(message), source("The core") {}
248         /** This constructor can be used to specify an error message before throwing,
249          * and to specify the source of the exception.
250          */
251         CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
252         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
253          * Actually no, it does nothing. Never mind.
254          * @throws Nothing!
255          */
256         virtual ~CoreException() throw() {};
257         /** Returns the reason for the exception.
258          * The module should probably put something informative here as the user will see this upon failure.
259          */
260         virtual const char* GetReason()
261         {
262                 return err.c_str();
263         }
264
265         virtual const char* GetSource()
266         {
267                 return source.c_str();
268         }
269 };
270
271 class CoreExport ModuleException : public CoreException
272 {
273  public:
274         /** Default constructor, just uses the error mesage 'Module threw an exception'.
275          */
276         ModuleException() : CoreException("Module threw an exception", "A Module") {}
277
278         /** This constructor can be used to specify an error message before throwing.
279          */
280         ModuleException(const std::string &message) : CoreException(message, "A Module") {}
281         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
282          * Actually no, it does nothing. Never mind.
283          * @throws Nothing!
284          */
285         virtual ~ModuleException() throw() {};
286 };
287
288 #endif