]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/base.h
Windows support. Tested and working to compile on freebsd and linux. Next step is...
[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 typedef void* VoidPointer;
24 typedef std::map<std::string,char*> ExtensibleStore;
25
26 class InspIRCd;
27
28 /** The base class for all inspircd classes
29 */ 
30 class CoreExport classbase
31 {
32  public:
33         /** Time that the object was instantiated (used for TS calculation etc)
34         */
35         time_t age;
36
37         /** Constructor,
38          * Sets the object's time
39          */
40         classbase();
41         ~classbase() { }
42 };
43
44 /** class Extensible is the parent class of many classes such as userrec and chanrec.
45  * class Extensible implements a system which allows modules to 'extend' the class by attaching data within
46  * a map associated with the object. In this way modules can store their own custom information within user
47  * objects, channel objects and server objects, without breaking other modules (this is more sensible than using
48  * a flags variable, and each module defining bits within the flag as 'theirs' as it is less prone to conflict and
49  * supports arbitary data storage).
50  */
51 class CoreExport Extensible : public classbase
52 {
53         /** Private data store
54          */
55         ExtensibleStore Extension_Items;
56         
57 public:
58
59         /** Extend an Extensible class.
60          *
61          * @param key The key parameter is an arbitary string which identifies the extension data
62          * @param p This parameter is a pointer to any data you wish to associate with the object
63          *
64          * You must provide a key to store the data as via the parameter 'key' and store the data
65          * in the templated parameter 'p'.
66          * The data will be inserted into the map. If the data already exists, you may not insert it
67          * twice, Extensible::Extend will return false in this case.
68          *
69          * @return Returns true on success, false if otherwise
70          */
71         template<typename T> bool Extend(const std::string &key, T* p)
72         {
73                 /* This will only add an item if it doesnt already exist,
74                  * the return value is a std::pair of an iterator to the
75                  * element, and a bool saying if it was actually inserted.
76                  */
77                 return this->Extension_Items.insert(std::make_pair(key, (char*)p)).second;
78         }
79
80         /** Extend an Extensible class.
81          *
82          * @param key The key parameter is an arbitary string which identifies the extension data
83          *
84          * You must provide a key to store the data as via the parameter 'key', this single-parameter
85          * version takes no 'data' parameter, this is used purely for boolean values.
86          * The key will be inserted into the map with a NULL 'data' pointer. If the key already exists
87          * then you may not insert it twice, Extensible::Extend will return false in this case.
88          *
89          * @return Returns true on success, false if otherwise
90          */
91         bool Extend(const std::string &key)
92         {
93                 /* This will only add an item if it doesnt already exist,
94                  * the return value is a std::pair of an iterator to the
95                  * element, and a bool saying if it was actually inserted.
96                  */
97                 return this->Extension_Items.insert(std::make_pair(key, (char*)NULL)).second;
98         }
99
100         /** Shrink an Extensible class.
101          *
102          * @param key The key parameter is an arbitary string which identifies the extension data
103          *
104          * You must provide a key name. The given key name will be removed from the classes data. If
105          * you provide a nonexistent key (case is important) then the function will return false.
106          * @return Returns true on success.
107          */
108         bool Shrink(const std::string &key);
109         
110         /** Get an extension item.
111          *
112          * @param key The key parameter is an arbitary string which identifies the extension data
113          * @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.
114          * @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.
115          */
116         template<typename T> bool GetExt(const std::string &key, T* &p)
117         {
118                 ExtensibleStore::iterator iter = this->Extension_Items.find(key);
119                 if(iter != this->Extension_Items.end())
120                 {
121                         p = (T*)iter->second;
122                         return true;
123                 }
124                 else
125                 {
126                         p = NULL;       
127                         return false;
128                 }
129         }
130         
131         /** Get an extension item.
132          *
133          * @param key The key parameter is an arbitary string which identifies the extension data
134          * @return Returns true if the item was found and false if it was not.
135          * 
136          * This single-parameter version only checks if the key exists, it does nothing with
137          * the 'data' field and is probably only useful in conjunction with the single-parameter
138          * version of Extend().
139          */
140         bool GetExt(const std::string &key)
141         {
142                 return (this->Extension_Items.find(key) != this->Extension_Items.end());
143         }
144
145         /** Get a list of all extension items names.
146          * @param list A deque of strings to receive the list
147          * @return This function writes a list of all extension items stored in this object by name into the given deque and returns void.
148          */
149         void GetExtList(std::deque<std::string> &list);
150 };
151
152 /** BoolSet is a utility class designed to hold eight bools in a bitmask.
153  * Use BoolSet::Set and BoolSet::Get to set and get bools in the bitmask,
154  * and Unset and Invert for special operations upon them.
155  */
156 class CoreExport BoolSet : public classbase
157 {
158         char bits;
159
160  public:
161
162         /** The default constructor initializes the BoolSet to all values unset.
163          */
164         BoolSet();
165
166         /** This constructor copies the default bitmask from a char
167          */
168         BoolSet(char bitmask);
169
170         /** The Set method sets one bool in the set.
171          *
172          * @param number The number of the item to set. This must be between 0 and 7.
173          */
174         void Set(int number);
175
176         /** The Get method returns the value of one bool in the set
177          *
178          * @param number The number of the item to retrieve. This must be between 0 and 7.
179          *
180          * @return True if the item is set, false if it is unset.
181          */
182         bool Get(int number);
183
184         /** The Unset method unsets one value in the set
185          *
186          * @param number The number of the item to set. This must be between 0 and 7.
187          */
188         void Unset(int number);
189
190         /** The Unset method inverts (flips) one value in the set
191          *
192          * @param number The number of the item to invert. This must be between 0 and 7.
193          */
194         void Invert(int number);
195
196         /** Compare two BoolSets
197          */
198         bool operator==(BoolSet other);
199
200         /** OR two BoolSets together
201          */
202         BoolSet operator|(BoolSet other);
203         
204         /** AND two BoolSets together
205          */
206         BoolSet operator&(BoolSet other);
207
208         /** Assign one BoolSet to another
209          */
210         bool operator=(BoolSet other);
211 };
212
213
214 #endif