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 void* ExtensionItem::get_raw(const Extensible* container)
96 ExtensibleStore::const_iterator i = container->extensions.find(key);
97 if (i == container->extensions.end())
102 void* ExtensionItem::set_raw(Extensible* container, void* value)
104 std::pair<ExtensibleStore::iterator,bool> rv =
105 container->extensions.insert(std::make_pair(key, value));
112 void* old = rv.first->second;
113 rv.first->second = value;
118 void* ExtensionItem::unset_raw(Extensible* container)
120 ExtensibleStore::iterator i = container->extensions.find(key);
121 if (i == container->extensions.end())
123 void* rv = i->second;
124 container->extensions.erase(i);
128 bool Extensible::Register(ExtensionItem* item)
130 return Extensible::extension_types.insert(std::make_pair(item->key, item)).second;
133 std::vector<ExtensionItem*> Extensible::BeginUnregister(Module* module)
135 std::vector<ExtensionItem*> rv;
136 ExtensibleTypes::iterator i = extension_types.begin();
137 while (i != extension_types.end())
139 ExtensibleTypes::iterator c = i++;
140 if (c->second->owner == module)
142 rv.push_back(c->second);
143 extension_types.erase(c);
149 void Extensible::doUnhookExtensions(const std::vector<ExtensionItem*>& toRemove)
151 for(std::vector<ExtensionItem*>::const_iterator i = toRemove.begin(); i != toRemove.end(); i++)
153 ExtensibleStore::iterator e = extensions.find((**i).key);
154 if (e != extensions.end())
156 (**i).free(e->second);
162 Extensible::~Extensible()
164 for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
166 ExtensionItem* type = GetItem(i->first);
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());
174 LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
178 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item)
183 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
187 LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner)
188 : SimpleExtItem<std::string>(Key, Owner) { }
190 std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item)
192 if (item && format == FORMAT_USER)
193 return *static_cast<std::string*>(item);
197 LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod)
201 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item)
203 if (format != FORMAT_USER)
205 return ConvToStr(reinterpret_cast<intptr_t>(item));
208 intptr_t LocalIntExt::get(const Extensible* container)
210 return reinterpret_cast<intptr_t>(get_raw(container));
213 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
216 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
218 return reinterpret_cast<intptr_t>(unset_raw(container));
221 void LocalIntExt::free(void*)
225 StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
229 std::string* StringExtItem::get(const Extensible* container)
231 return static_cast<std::string*>(get_raw(container));
234 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item)
236 return item ? *static_cast<std::string*>(item) : "";
239 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
244 set(container, value);
247 void StringExtItem::set(Extensible* container, const std::string& value)
249 void* old = set_raw(container, new std::string(value));
250 delete static_cast<std::string*>(old);
253 void StringExtItem::unset(Extensible* container)
255 void* old = unset_raw(container);
256 delete static_cast<std::string*>(old);
259 void StringExtItem::free(void* item)
261 delete static_cast<std::string*>(item);