]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/base.h
12538defab5ae5164cc134604d02e4f7a8a3d867
[user/henk/code/inspircd.git] / include / base.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #ifndef __BASE_H__ 
18 #define __BASE_H__ 
19
20 #include "inspircd_config.h"
21 #include <time.h>
22 #include <map>
23 #include <deque>
24 #include <string>
25
26 typedef void* VoidPointer;
27 typedef std::map<std::string,char*> ExtensibleStore;
28
29 extern time_t TIME;
30  
31 /** The base class for all inspircd classes
32 */ 
33 class classbase
34 {
35  public:
36         /** Time that the object was instantiated (used for TS calculation etc)
37         */
38         time_t age;
39         static unsigned long id;
40
41         /** Constructor,
42          * Sets the object's time
43          */
44         classbase() { age = TIME; id++; }
45         ~classbase() { id--; }
46 };
47
48 /** class Extensible is the parent class of many classes such as userrec and chanrec.
49  * class Extensible implements a system which allows modules to 'extend' the class by attaching data within
50  * a map associated with the object. In this way modules can store their own custom information within user
51  * objects, channel objects and server objects, without breaking other modules (this is more sensible than using
52  * a flags variable, and each module defining bits within the flag as 'theirs' as it is less prone to conflict and
53  * supports arbitary data storage).
54  */
55 class Extensible : public classbase
56 {
57         /** Private data store
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, and a void* to the data (typedef VoidPointer)
69          * The data will be inserted into the map. If the data already exists, you may not insert it
70          * twice, Extensible::Extend will return false in this case.
71          *
72          * @return Returns true on success, false if otherwise
73          */
74         template<typename T> bool Extend(const std::string &key, T* p)
75         {
76                 /* This will only add an item if it doesnt already exist,
77                  * the return value is a std::pair of an iterator to the
78                  * element, and a bool saying if it was actually inserted.
79                  */
80                 return this->Extension_Items.insert(std::make_pair(key, (char*)p)).second;
81         }
82
83         /** Shrink an Extensible class.
84          *
85          * @param key The key parameter is an arbitary string which identifies the extension data
86          *
87          * You must provide a key name. The given key name will be removed from the classes data. If
88          * you provide a nonexistent key (case is important) then the function will return false.
89          * @return Returns true on success.
90          */
91         bool Shrink(const std::string &key);
92         
93         /** Get an extension item.
94          *
95          * @param key The key parameter is an arbitary string which identifies the extension data
96          * @return If you provide a non-existent key name, the function returns NULL, otherwise a pointer to the item referenced by the key is returned.
97          */
98         template<typename T> bool GetExt(const std::string &key, T* &p)
99         {
100                 ExtensibleStore::iterator iter = this->Extension_Items.find(key);
101                 if(iter != this->Extension_Items.end())
102                 {
103                         p = (T*)iter->second;
104                         return true;
105                 }
106                 else
107                 {
108                         p = NULL;       
109                         return false;
110                 }
111         }
112
113         /** Get a list of all extension items names.
114          * @param list A deque of strings to receive the list
115          * @return This function writes a list of all extension items stored in this object by name into the given deque and returns void.
116          */
117         void GetExtList(std::deque<std::string> &list);
118 };
119
120 /** BoolSet is a utility class designed to hold eight bools in a bitmask.
121  * Use BoolSet::Set and BoolSet::Get to set and get bools in the bitmask,
122  * and Unset and Invert for special operations upon them.
123  */
124 class BoolSet : public classbase
125 {
126         char bits;
127
128  public:
129
130         /** The default constructor initializes the BoolSet to all values unset.
131          */
132         BoolSet();
133
134         /** This constructor copies the default bitmask from a char
135          */
136         BoolSet(char bitmask);
137
138         /** The Set method sets one bool in the set.
139          *
140          * @param number The number of the item to set. This must be between 0 and 7.
141          */
142         void Set(int number);
143
144         /** The Get method returns the value of one bool in the set
145          *
146          * @param number The number of the item to retrieve. This must be between 0 and 7.
147          *
148          * @return True if the item is set, false if it is unset.
149          */
150         bool Get(int number);
151
152         /** The Unset method unsets one value in the set
153          *
154          * @param number The number of the item to set. This must be between 0 and 7.
155          */
156         void Unset(int number);
157
158         /** The Unset method inverts (flips) one value in the set
159          *
160          * @param number The number of the item to invert. This must be between 0 and 7.
161          */
162         void Invert(int number);
163
164         /** Compare two BoolSets
165          */
166         bool operator==(BoolSet other);
167
168         /** OR two BoolSets together
169          */
170         BoolSet operator|(BoolSet other);
171         
172         /** AND two BoolSets together
173          */
174         BoolSet operator&(BoolSet other);
175
176         /** Assign one BoolSet to another
177          */
178         bool operator=(BoolSet other);
179 };
180
181
182 #endif