]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/base.cpp
Remove InspIRCd* parameters and fields
[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 void* ExtensionItem::get_raw(const Extensible* container)
95 {
96         ExtensibleStore::const_iterator i = container->extensions.find(key);
97         if (i == container->extensions.end())
98                 return NULL;
99         return i->second;
100 }
101
102 void* ExtensionItem::set_raw(Extensible* container, void* value)
103 {
104         std::pair<ExtensibleStore::iterator,bool> rv = 
105                 container->extensions.insert(std::make_pair(key, value));
106         if (rv.second)
107         {
108                 return NULL;
109         }
110         else
111         {
112                 void* old = rv.first->second;
113                 rv.first->second = value;
114                 return old;
115         }
116 }
117
118 void* ExtensionItem::unset_raw(Extensible* container)
119 {
120         ExtensibleStore::iterator i = container->extensions.find(key);
121         if (i == container->extensions.end())
122                 return NULL;
123         void* rv = i->second;
124         container->extensions.erase(i);
125         return rv;
126 }
127
128 bool Extensible::Register(ExtensionItem* item)
129 {
130         return Extensible::extension_types.insert(std::make_pair(item->key, item)).second;
131 }
132
133 std::vector<ExtensionItem*> Extensible::BeginUnregister(Module* module)
134 {
135         std::vector<ExtensionItem*> rv;
136         ExtensibleTypes::iterator i = extension_types.begin();
137         while (i != extension_types.end())
138         {
139                 ExtensibleTypes::iterator c = i++;
140                 if (c->second->owner == module)
141                 {
142                         rv.push_back(c->second);
143                         extension_types.erase(c);
144                 }
145         }
146         return rv;
147 }
148
149 void Extensible::doUnhookExtensions(const std::vector<ExtensionItem*>& toRemove)
150 {
151         for(std::vector<ExtensionItem*>::const_iterator i = toRemove.begin(); i != toRemove.end(); i++)
152         {
153                 ExtensibleStore::iterator e = extensions.find((**i).key);
154                 if (e != extensions.end())
155                 {
156                         (**i).free(e->second);
157                         extensions.erase(e);
158                 }
159         }
160 }
161
162 Extensible::~Extensible()
163 {
164         for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
165         {
166                 ExtensionItem* type = GetItem(i->first);
167                 if (type)
168                         type->free(i->second);  
169                 else if (ServerInstance && ServerInstance->Logs)
170                         ServerInstance->Logs->Log("BASE", ERROR, "Extension type %s is not registered", i->first.c_str());
171         }
172 }
173
174 LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
175 {
176 }
177
178 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item)
179 {
180         return "";
181 }
182
183 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
184 {
185 }
186
187 LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner)
188         : SimpleExtItem<std::string>(Key, Owner) { }
189
190 std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item)
191 {
192         if (item && format == FORMAT_USER)
193                 return *static_cast<std::string*>(item);
194         return "";
195 }
196
197 LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod)
198 {
199 }
200
201 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item)
202 {
203         if (format != FORMAT_USER)
204                 return "";
205         return ConvToStr(reinterpret_cast<intptr_t>(item));
206 }
207
208 intptr_t LocalIntExt::get(const Extensible* container)
209 {
210         return reinterpret_cast<intptr_t>(get_raw(container));
211 }
212
213 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
214 {
215         if (value)
216                 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
217         else
218                 return reinterpret_cast<intptr_t>(unset_raw(container));
219 }
220
221 void LocalIntExt::free(void*)
222 {
223 }
224
225 StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
226 {
227 }
228
229 std::string* StringExtItem::get(const Extensible* container)
230 {
231         return static_cast<std::string*>(get_raw(container));
232 }
233
234 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item)
235 {
236         return item ? *static_cast<std::string*>(item) : "";
237 }
238
239 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
240 {
241         if (value.empty())
242                 unset(container);
243         else
244                 set(container, value);
245 }
246
247 void StringExtItem::set(Extensible* container, const std::string& value)
248 {
249         void* old = set_raw(container, new std::string(value));
250         delete static_cast<std::string*>(old);
251 }
252
253 void StringExtItem::unset(Extensible* container)
254 {
255         void* old = unset_raw(container);
256         delete static_cast<std::string*>(old);
257 }
258
259 void StringExtItem::free(void* item)
260 {
261         delete static_cast<std::string*>(item);
262 }