diff options
Diffstat (limited to 'src/base.cpp')
-rw-r--r-- | src/base.cpp | 155 |
1 files changed, 138 insertions, 17 deletions
diff --git a/src/base.cpp b/src/base.cpp index 7e0cf482a..4d0c17077 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -20,28 +20,12 @@ const int bitfields[] = {1,2,4,8,16,32,64,128}; const int inverted_bitfields[] = {~1,~2,~4,~8,~16,~32,~64,~128}; +std::map<std::string, ExtensionItem*> Extensible::extension_types; classbase::classbase() { } -bool Extensible::Shrink(const std::string &key) -{ - /* map::size_type map::erase( const key_type& key ); - * returns the number of elements removed, std::map - * is single-associative so this should only be 0 or 1 - */ - return this->Extension_Items.erase(key); -} - -void Extensible::GetExtList(std::deque<std::string> &list) -{ - for (ExtensibleStore::iterator u = Extension_Items.begin(); u != Extension_Items.end(); u++) - { - list.push_back(u->first); - } -} - void BoolSet::Set(int number) { this->bits |= bitfields[number]; @@ -94,3 +78,140 @@ bool BoolSet::operator=(BoolSet other) this->bits = other.bits; return true; } + +ExtensionItem::ExtensionItem(const std::string& Key, Module* mod) : key(Key), owner(mod) +{ +} + +void* ExtensionItem::get_raw(const Extensible* container) +{ + ExtensibleStore::const_iterator i = container->extensions.find(key); + if (i == container->extensions.end()) + return NULL; + return i->second; +} + +void* ExtensionItem::set_raw(Extensible* container, void* value) +{ + std::pair<ExtensibleStore::iterator,bool> rv = + container->extensions.insert(std::make_pair(key, value)); + if (rv.second) + { + return NULL; + } + else + { + void* old = rv.first->second; + rv.first->second = value; + return old; + } +} + +void* ExtensionItem::unset_raw(Extensible* container) +{ + ExtensibleStore::iterator i = container->extensions.find(key); + if (i == container->extensions.end()) + return NULL; + void* rv = i->second; + container->extensions.erase(i); + return rv; +} + +bool Extensible::Register(ExtensionItem* item) +{ + return Extensible::extension_types.insert(std::make_pair(item->key, item)).second; +} + +void Extensible::UnRegister(Module* module) +{ + ExtensibleTypes::iterator i = extension_types.begin(); + while (i != extension_types.end()) + { + ExtensibleTypes::iterator c = i++; + if (c->second->owner == module) + extension_types.erase(c); + } +} + +Extensible::~Extensible() +{ + for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i) + { + ExtensionItem* type = extension_types[i->first]; + if (type) + type->free(i->second); + } +} + +LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod) +{ +} + +std::string LocalExtItem::serialize(Module* requestor, const Extensible* container, void* item) +{ + return ""; +} + +void LocalExtItem::unserialize(Module* requestor, Extensible* container, const std::string& value) +{ +} + +LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod) +{ +} + +intptr_t LocalIntExt::get(const Extensible* container) +{ + return reinterpret_cast<intptr_t>(get_raw(container)); +} + +intptr_t LocalIntExt::set(Extensible* container, intptr_t value) +{ + if (value) + return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value))); + else + return reinterpret_cast<intptr_t>(unset_raw(container)); +} + +void LocalIntExt::free(void*) +{ +} + +StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod) +{ +} + +std::string* StringExtItem::get(const Extensible* container) +{ + return static_cast<std::string*>(get_raw(container)); +} + +std::string StringExtItem::serialize(Module* requestor, const Extensible* container, void* item) +{ + return item ? *static_cast<std::string*>(item) : ""; +} + +void StringExtItem::unserialize(Module* requestor, Extensible* container, const std::string& value) +{ + if (value.empty()) + unset(container); + else + set(container, value); +} + +void StringExtItem::set(Extensible* container, const std::string& value) +{ + void* old = set_raw(container, new std::string(value)); + delete static_cast<std::string*>(old); +} + +void StringExtItem::unset(Extensible* container) +{ + void* old = unset_raw(container); + delete static_cast<std::string*>(old); +} + +void StringExtItem::free(void* item) +{ + delete static_cast<std::string*>(item); +} |