]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/base.cpp
Add SerializeFormat for easier metadata formatting
[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 BoolSet::Set(int number)
30 {
31         this->bits |= bitfields[number];
32 }
33
34 void BoolSet::Unset(int number)
35 {
36         this->bits &= inverted_bitfields[number];
37 }
38
39 void BoolSet::Invert(int number)
40 {
41         this->bits ^= bitfields[number];
42 }
43
44 bool BoolSet::Get(int number)
45 {
46         return ((this->bits | bitfields[number]) > 0);
47 }
48
49 bool BoolSet::operator==(BoolSet other)
50 {
51         return (this->bits == other.bits);
52 }
53
54 BoolSet BoolSet::operator|(BoolSet other)
55 {
56         BoolSet x(this->bits | other.bits);
57         return x;
58 }
59
60 BoolSet BoolSet::operator&(BoolSet other)
61 {
62         BoolSet x(this->bits & other.bits);
63         return x;
64 }
65
66 BoolSet::BoolSet()
67 {
68         this->bits = 0;
69 }
70
71 BoolSet::BoolSet(char bitmask)
72 {
73         this->bits = bitmask;
74 }
75
76 bool BoolSet::operator=(BoolSet other)
77 {
78         this->bits = other.bits;
79         return true;
80 }
81
82 ExtensionItem::ExtensionItem(const std::string& Key, Module* mod) : key(Key), owner(mod)
83 {
84 }
85
86 void* ExtensionItem::get_raw(const Extensible* container)
87 {
88         ExtensibleStore::const_iterator i = container->extensions.find(key);
89         if (i == container->extensions.end())
90                 return NULL;
91         return i->second;
92 }
93
94 void* ExtensionItem::set_raw(Extensible* container, void* value)
95 {
96         std::pair<ExtensibleStore::iterator,bool> rv = 
97                 container->extensions.insert(std::make_pair(key, value));
98         if (rv.second)
99         {
100                 return NULL;
101         }
102         else
103         {
104                 void* old = rv.first->second;
105                 rv.first->second = value;
106                 return old;
107         }
108 }
109
110 void* ExtensionItem::unset_raw(Extensible* container)
111 {
112         ExtensibleStore::iterator i = container->extensions.find(key);
113         if (i == container->extensions.end())
114                 return NULL;
115         void* rv = i->second;
116         container->extensions.erase(i);
117         return rv;
118 }
119
120 bool Extensible::Register(ExtensionItem* item)
121 {
122         return Extensible::extension_types.insert(std::make_pair(item->key, item)).second;
123 }
124
125 void Extensible::UnRegister(Module* module)
126 {
127         ExtensibleTypes::iterator i = extension_types.begin();
128         while (i != extension_types.end())
129         {
130                 ExtensibleTypes::iterator c = i++;
131                 if (c->second->owner == module)
132                         extension_types.erase(c);
133         }
134 }
135
136 Extensible::~Extensible()
137 {
138         for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
139         {
140                 ExtensionItem* type = extension_types[i->first];
141                 if (type)
142                         type->free(i->second);  
143         }
144 }
145
146 LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
147 {
148 }
149
150 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item)
151 {
152         return "";
153 }
154
155 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
156 {
157 }
158
159 LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner)
160         : SimpleExtItem<std::string>(Key, Owner) { }
161
162 std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item)
163 {
164         if (item && format == FORMAT_USER)
165                 return *static_cast<std::string*>(item);
166         return "";
167 }
168
169 LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod)
170 {
171 }
172
173 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item)
174 {
175         if (format != FORMAT_USER)
176                 return "";
177         return ConvToStr(reinterpret_cast<intptr_t>(item));
178 }
179
180 intptr_t LocalIntExt::get(const Extensible* container)
181 {
182         return reinterpret_cast<intptr_t>(get_raw(container));
183 }
184
185 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
186 {
187         if (value)
188                 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
189         else
190                 return reinterpret_cast<intptr_t>(unset_raw(container));
191 }
192
193 void LocalIntExt::free(void*)
194 {
195 }
196
197 StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
198 {
199 }
200
201 std::string* StringExtItem::get(const Extensible* container)
202 {
203         return static_cast<std::string*>(get_raw(container));
204 }
205
206 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item)
207 {
208         return item ? *static_cast<std::string*>(item) : "";
209 }
210
211 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
212 {
213         if (value.empty())
214                 unset(container);
215         else
216                 set(container, value);
217 }
218
219 void StringExtItem::set(Extensible* container, const std::string& value)
220 {
221         void* old = set_raw(container, new std::string(value));
222         delete static_cast<std::string*>(old);
223 }
224
225 void StringExtItem::unset(Extensible* container)
226 {
227         void* old = unset_raw(container);
228         delete static_cast<std::string*>(old);
229 }
230
231 void StringExtItem::free(void* item)
232 {
233         delete static_cast<std::string*>(item);
234 }