1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 #include "inspircd_config.h"
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;
25 classbase::classbase()
29 void classbase::cull()
33 classbase::~classbase()
37 void BoolSet::Set(int number)
39 this->bits |= bitfields[number];
42 void BoolSet::Unset(int number)
44 this->bits &= inverted_bitfields[number];
47 void BoolSet::Invert(int number)
49 this->bits ^= bitfields[number];
52 bool BoolSet::Get(int number)
54 return ((this->bits | bitfields[number]) > 0);
57 bool BoolSet::operator==(BoolSet other)
59 return (this->bits == other.bits);
62 BoolSet BoolSet::operator|(BoolSet other)
64 BoolSet x(this->bits | other.bits);
68 BoolSet BoolSet::operator&(BoolSet other)
70 BoolSet x(this->bits & other.bits);
79 BoolSet::BoolSet(char bitmask)
84 bool BoolSet::operator=(BoolSet other)
86 this->bits = other.bits;
90 ExtensionItem::ExtensionItem(const std::string& Key, Module* mod) : key(Key), owner(mod)
94 ExtensionItem::~ExtensionItem()
98 void* ExtensionItem::get_raw(const Extensible* container)
100 ExtensibleStore::const_iterator i = container->extensions.find(key);
101 if (i == container->extensions.end())
106 void* ExtensionItem::set_raw(Extensible* container, void* value)
108 std::pair<ExtensibleStore::iterator,bool> rv =
109 container->extensions.insert(std::make_pair(key, value));
116 void* old = rv.first->second;
117 rv.first->second = value;
122 void* ExtensionItem::unset_raw(Extensible* container)
124 ExtensibleStore::iterator i = container->extensions.find(key);
125 if (i == container->extensions.end())
127 void* rv = i->second;
128 container->extensions.erase(i);
132 bool Extensible::Register(ExtensionItem* item)
134 return Extensible::extension_types.insert(std::make_pair(item->key, item)).second;
137 std::vector<ExtensionItem*> Extensible::BeginUnregister(Module* module)
139 std::vector<ExtensionItem*> rv;
140 ExtensibleTypes::iterator i = extension_types.begin();
141 while (i != extension_types.end())
143 ExtensibleTypes::iterator c = i++;
144 if (c->second->owner == module)
146 rv.push_back(c->second);
147 extension_types.erase(c);
153 void Extensible::doUnhookExtensions(const std::vector<ExtensionItem*>& toRemove)
155 for(std::vector<ExtensionItem*>::const_iterator i = toRemove.begin(); i != toRemove.end(); i++)
157 ExtensibleStore::iterator e = extensions.find((**i).key);
158 if (e != extensions.end())
160 (**i).free(e->second);
166 Extensible::~Extensible()
168 for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
170 ExtensionItem* type = GetItem(i->first);
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());
178 LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
182 LocalExtItem::~LocalExtItem()
186 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item)
191 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
195 LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner)
196 : SimpleExtItem<std::string>(Key, Owner) { }
198 LocalStringExt::~LocalStringExt()
202 std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item)
204 if (item && format == FORMAT_USER)
205 return *static_cast<std::string*>(item);
209 LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod)
213 LocalIntExt::~LocalIntExt()
217 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item)
219 if (format != FORMAT_USER)
221 return ConvToStr(reinterpret_cast<intptr_t>(item));
224 intptr_t LocalIntExt::get(const Extensible* container)
226 return reinterpret_cast<intptr_t>(get_raw(container));
229 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
232 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
234 return reinterpret_cast<intptr_t>(unset_raw(container));
237 void LocalIntExt::free(void*)
241 StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
245 StringExtItem::~StringExtItem()
249 std::string* StringExtItem::get(const Extensible* container)
251 return static_cast<std::string*>(get_raw(container));
254 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item)
256 return item ? *static_cast<std::string*>(item) : "";
259 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
264 set(container, value);
267 void StringExtItem::set(Extensible* container, const std::string& value)
269 void* old = set_raw(container, new std::string(value));
270 delete static_cast<std::string*>(old);
273 void StringExtItem::unset(Extensible* container)
275 void* old = unset_raw(container);
276 delete static_cast<std::string*>(old);
279 void StringExtItem::free(void* item)
281 delete static_cast<std::string*>(item);