]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/base.cpp
76e469482d56759798f86860b02653691d9a2d32
[user/henk/code/inspircd.git] / src / base.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd_config.h"
17 #include "base.h"
18 #include <time.h>
19 #include "inspircd.h"
20
21 const int bitfields[]           =       {1,2,4,8,16,32,64,128};
22 const int inverted_bitfields[]  =       {~1,~2,~4,~8,~16,~32,~64,~128};
23 std::map<std::string, ExtensionItem*> Extensible::extension_types;
24
25 classbase::classbase()
26 {
27 }
28
29 void classbase::cull()
30 {
31 }
32
33 classbase::~classbase()
34 {
35 }
36
37 void BoolSet::Set(int number)
38 {
39         this->bits |= bitfields[number];
40 }
41
42 void BoolSet::Unset(int number)
43 {
44         this->bits &= inverted_bitfields[number];
45 }
46
47 void BoolSet::Invert(int number)
48 {
49         this->bits ^= bitfields[number];
50 }
51
52 bool BoolSet::Get(int number)
53 {
54         return ((this->bits | bitfields[number]) > 0);
55 }
56
57 bool BoolSet::operator==(BoolSet other)
58 {
59         return (this->bits == other.bits);
60 }
61
62 BoolSet BoolSet::operator|(BoolSet other)
63 {
64         BoolSet x(this->bits | other.bits);
65         return x;
66 }
67
68 BoolSet BoolSet::operator&(BoolSet other)
69 {
70         BoolSet x(this->bits & other.bits);
71         return x;
72 }
73
74 BoolSet::BoolSet()
75 {
76         this->bits = 0;
77 }
78
79 BoolSet::BoolSet(char bitmask)
80 {
81         this->bits = bitmask;
82 }
83
84 bool BoolSet::operator=(BoolSet other)
85 {
86         this->bits = other.bits;
87         return true;
88 }
89
90 ExtensionItem::ExtensionItem(const std::string& Key, Module* mod) : key(Key), owner(mod)
91 {
92 }
93
94 ExtensionItem::~ExtensionItem()
95 {
96 }
97
98 void* ExtensionItem::get_raw(const Extensible* container)
99 {
100         ExtensibleStore::const_iterator i = container->extensions.find(key);
101         if (i == container->extensions.end())
102                 return NULL;
103         return i->second;
104 }
105
106 void* ExtensionItem::set_raw(Extensible* container, void* value)
107 {
108         std::pair<ExtensibleStore::iterator,bool> rv = 
109                 container->extensions.insert(std::make_pair(key, value));
110         if (rv.second)
111         {
112                 return NULL;
113         }
114         else
115         {
116                 void* old = rv.first->second;
117                 rv.first->second = value;
118                 return old;
119         }
120 }
121
122 void* ExtensionItem::unset_raw(Extensible* container)
123 {
124         ExtensibleStore::iterator i = container->extensions.find(key);
125         if (i == container->extensions.end())
126                 return NULL;
127         void* rv = i->second;
128         container->extensions.erase(i);
129         return rv;
130 }
131
132 bool Extensible::Register(ExtensionItem* item)
133 {
134         return Extensible::extension_types.insert(std::make_pair(item->key, item)).second;
135 }
136
137 std::vector<ExtensionItem*> Extensible::BeginUnregister(Module* module)
138 {
139         std::vector<ExtensionItem*> rv;
140         ExtensibleTypes::iterator i = extension_types.begin();
141         while (i != extension_types.end())
142         {
143                 ExtensibleTypes::iterator c = i++;
144                 if (c->second->owner == module)
145                 {
146                         rv.push_back(c->second);
147                         extension_types.erase(c);
148                 }
149         }
150         return rv;
151 }
152
153 void Extensible::doUnhookExtensions(const std::vector<ExtensionItem*>& toRemove)
154 {
155         for(std::vector<ExtensionItem*>::const_iterator i = toRemove.begin(); i != toRemove.end(); i++)
156         {
157                 ExtensibleStore::iterator e = extensions.find((**i).key);
158                 if (e != extensions.end())
159                 {
160                         (**i).free(e->second);
161                         extensions.erase(e);
162                 }
163         }
164 }
165
166 Extensible::~Extensible()
167 {
168         for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
169         {
170                 ExtensionItem* type = GetItem(i->first);
171                 if (type)
172                         type->free(i->second);  
173                 else if (ServerInstance && ServerInstance->Logs)
174                         ServerInstance->Logs->Log("BASE", ERROR, "Extension type %s is not registered", i->first.c_str());
175         }
176 }
177
178 LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
179 {
180 }
181
182 LocalExtItem::~LocalExtItem()
183 {
184 }
185
186 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item)
187 {
188         return "";
189 }
190
191 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
192 {
193 }
194
195 LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner)
196         : SimpleExtItem<std::string>(Key, Owner) { }
197
198 LocalStringExt::~LocalStringExt()
199 {
200 }
201
202 std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item)
203 {
204         if (item && format == FORMAT_USER)
205                 return *static_cast<std::string*>(item);
206         return "";
207 }
208
209 LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod)
210 {
211 }
212
213 LocalIntExt::~LocalIntExt()
214 {
215 }
216
217 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item)
218 {
219         if (format != FORMAT_USER)
220                 return "";
221         return ConvToStr(reinterpret_cast<intptr_t>(item));
222 }
223
224 intptr_t LocalIntExt::get(const Extensible* container)
225 {
226         return reinterpret_cast<intptr_t>(get_raw(container));
227 }
228
229 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
230 {
231         if (value)
232                 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
233         else
234                 return reinterpret_cast<intptr_t>(unset_raw(container));
235 }
236
237 void LocalIntExt::free(void*)
238 {
239 }
240
241 StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
242 {
243 }
244
245 StringExtItem::~StringExtItem()
246 {
247 }
248
249 std::string* StringExtItem::get(const Extensible* container)
250 {
251         return static_cast<std::string*>(get_raw(container));
252 }
253
254 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item)
255 {
256         return item ? *static_cast<std::string*>(item) : "";
257 }
258
259 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
260 {
261         if (value.empty())
262                 unset(container);
263         else
264                 set(container, value);
265 }
266
267 void StringExtItem::set(Extensible* container, const std::string& value)
268 {
269         void* old = set_raw(container, new std::string(value));
270         delete static_cast<std::string*>(old);
271 }
272
273 void StringExtItem::unset(Extensible* container)
274 {
275         void* old = unset_raw(container);
276         delete static_cast<std::string*>(old);
277 }
278
279 void StringExtItem::free(void* item)
280 {
281         delete static_cast<std::string*>(item);
282 }