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 BoolSet::Set(int number)
31 this->bits |= bitfields[number];
34 void BoolSet::Unset(int number)
36 this->bits &= inverted_bitfields[number];
39 void BoolSet::Invert(int number)
41 this->bits ^= bitfields[number];
44 bool BoolSet::Get(int number)
46 return ((this->bits | bitfields[number]) > 0);
49 bool BoolSet::operator==(BoolSet other)
51 return (this->bits == other.bits);
54 BoolSet BoolSet::operator|(BoolSet other)
56 BoolSet x(this->bits | other.bits);
60 BoolSet BoolSet::operator&(BoolSet other)
62 BoolSet x(this->bits & other.bits);
71 BoolSet::BoolSet(char bitmask)
76 bool BoolSet::operator=(BoolSet other)
78 this->bits = other.bits;
82 ExtensionItem::ExtensionItem(const std::string& Key, Module* mod) : key(Key), owner(mod)
86 void* ExtensionItem::get_raw(const Extensible* container)
88 ExtensibleStore::const_iterator i = container->extensions.find(key);
89 if (i == container->extensions.end())
94 void* ExtensionItem::set_raw(Extensible* container, void* value)
96 std::pair<ExtensibleStore::iterator,bool> rv =
97 container->extensions.insert(std::make_pair(key, value));
104 void* old = rv.first->second;
105 rv.first->second = value;
110 void* ExtensionItem::unset_raw(Extensible* container)
112 ExtensibleStore::iterator i = container->extensions.find(key);
113 if (i == container->extensions.end())
115 void* rv = i->second;
116 container->extensions.erase(i);
120 bool Extensible::Register(ExtensionItem* item)
122 return Extensible::extension_types.insert(std::make_pair(item->key, item)).second;
125 void Extensible::UnRegister(Module* module)
127 ExtensibleTypes::iterator i = extension_types.begin();
128 while (i != extension_types.end())
130 ExtensibleTypes::iterator c = i++;
131 if (c->second->owner == module)
132 extension_types.erase(c);
136 Extensible::~Extensible()
138 for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
140 ExtensionItem* type = extension_types[i->first];
142 type->free(i->second);
146 LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
150 std::string LocalExtItem::serialize(Module* requestor, const Extensible* container, void* item)
155 void LocalExtItem::unserialize(Module* requestor, Extensible* container, const std::string& value)
159 LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod)
163 intptr_t LocalIntExt::get(const Extensible* container)
165 return reinterpret_cast<intptr_t>(get_raw(container));
168 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
171 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
173 return reinterpret_cast<intptr_t>(unset_raw(container));
176 void LocalIntExt::free(void*)
180 StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
184 std::string* StringExtItem::get(const Extensible* container)
186 return static_cast<std::string*>(get_raw(container));
189 std::string StringExtItem::serialize(Module* requestor, const Extensible* container, void* item)
191 return item ? *static_cast<std::string*>(item) : "";
194 void StringExtItem::unserialize(Module* requestor, Extensible* container, const std::string& value)
199 set(container, value);
202 void StringExtItem::set(Extensible* container, const std::string& value)
204 void* old = set_raw(container, new std::string(value));
205 delete static_cast<std::string*>(old);
208 void StringExtItem::unset(Extensible* container)
210 void* old = unset_raw(container);
211 delete static_cast<std::string*>(old);
214 void StringExtItem::free(void* item)
216 delete static_cast<std::string*>(item);