]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/base.h
ac03a748c56d59927ad237969f48a1936f3bbc8e
[user/henk/code/inspircd.git] / include / base.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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  
28 /** The base class for all inspircd classes
29 */ 
30 class 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() { age = time(NULL); }
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 Extensible : public classbase
52 {
53         /** Private data store
54          */
55         std::map<std::string,char*> 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, and a void* to the data (typedef VoidPointer)
65          * The data will be inserted into the map. If the data already exists, you may not insert it
66          * twice, Extensible::Extend will return false in this case.
67          *
68          * @return Returns true on success, false if otherwise
69          */
70         bool Extend(std::string key, char* p);
71
72         /** Shrink an Extensible class.
73          *
74          * @param key The key parameter is an arbitary string which identifies the extension data
75          *
76          * You must provide a key name. The given key name will be removed from the classes data. If
77          * you provide a nonexistent key (case is important) then the function will return false.
78          *
79          * @return Returns true on success.
80          */
81         bool Shrink(std::string key);
82         
83         /** Get an extension item.
84          *
85          * @param key The key parameter is an arbitary string which identifies the extension data
86          *
87          * @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.
88          */
89         char* GetExt(std::string key);
90
91         /** Get a list of all extension items names.
92          *
93          * @param list A deque of strings to receive the list
94          *
95          * @return This function writes a list of all extension items stored in this object by name into the given deque and returns void.
96          */
97         void GetExtList(std::deque<std::string> &list);
98 };
99
100 /** BoolSet is a utility class designed to hold eight bools in a bitmask.
101  * Use BoolSet::Set and BoolSet::Get to set and get bools in the bitmask,
102  * and Unset and Invert for special operations upon them.
103  */
104 class BoolSet
105 {
106         char bits;
107
108  public:
109
110         /** The default constructor initializes the BoolSet to all values unset.
111          */
112         BoolSet();
113
114         /** This constructor copies the default bitmask from a char
115          */
116         BoolSet(char bitmask);
117
118         /** The Set method sets one bool in the set.
119          *
120          * @param number The number of the item to set. This must be between 0 and 7.
121          */
122         void Set(int number);
123
124         /** The Get method returns the value of one bool in the set
125          *
126          * @param number The number of the item to retrieve. This must be between 0 and 7.
127          *
128          * @return True if the item is set, false if it is unset.
129          */
130         bool Get(int number);
131
132         /** The Unset method unsets one value in the set
133          *
134          * @param number The number of the item to set. This must be between 0 and 7.
135          */
136         void Unset(int number);
137
138         /** The Unset method inverts (flips) one value in the set
139          *
140          * @param number The number of the item to invert. This must be between 0 and 7.
141          */
142         void Invert(int number);
143
144         /** Compare two BoolSets
145          */
146         bool operator==(BoolSet other);
147
148         /** OR two BoolSets together
149          */
150         BoolSet operator|(BoolSet other);
151         
152         /** AND two BoolSets together
153          */
154         BoolSet operator&(BoolSet other);
155
156         /** Assign one BoolSet to another
157          */
158         bool operator=(BoolSet other);
159 };
160
161
162 #endif
163