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