X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fbase.cpp;h=9307928540639b721cda966221dac65cdd06cb27;hb=6e6846b68a28d858275702e6662e79328e62c60e;hp=c18b1f49bd512a3bf1c3c303f37338b5608d6622;hpb=ab6a7318e36bd8e0a259cd9eef3694a0f0e8684a;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/base.cpp b/src/base.cpp index c18b1f49b..930792854 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -2,8 +2,8 @@ * | Inspire Internet Relay Chat Daemon | * +------------------------------------+ * - * InspIRCd: (C) 2002-2007 InspIRCd Development Team - * See: http://www.inspircd.org/wiki/index.php/Credits + * InspIRCd: (C) 2002-2009 InspIRCd Development Team + * See: http://wiki.inspircd.org/Credits * * This program is free but copyrighted software; see * the file COPYING for details. @@ -11,7 +11,7 @@ * --------------------------------------------------- */ -/* $Core: libIRCDbase */ +/* $Core */ #include "inspircd_config.h" #include "base.h" @@ -20,78 +20,224 @@ 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 Extensible::extension_types; classbase::classbase() { - this->age = time(NULL); } -bool Extensible::Shrink(const std::string &key) +bool classbase::cull() { - /* 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); + return true; +} + +classbase::~classbase() +{ +} + +refcountbase::refcountbase() : refcount(0) +{ +} + +bool refcountbase::cull() +{ + return (refcount == 0); } -void Extensible::GetExtList(std::deque &list) +refcountbase::~refcountbase() { - for (ExtensibleStore::iterator u = Extension_Items.begin(); u != Extension_Items.end(); u++) +} + +ExtensionItem::ExtensionItem(const std::string& Key, Module* mod) : key(Key), owner(mod) +{ +} + +ExtensionItem::~ExtensionItem() +{ +} + +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 rv = + container->extensions.insert(std::make_pair(key, value)); + if (rv.second) { - list.push_back(u->first); + return NULL; + } + else + { + void* old = rv.first->second; + rv.first->second = value; + return old; } } -void BoolSet::Set(int number) +void* ExtensionItem::unset_raw(Extensible* container) { - this->bits |= bitfields[number]; + ExtensibleStore::iterator i = container->extensions.find(key); + if (i == container->extensions.end()) + return NULL; + void* rv = i->second; + container->extensions.erase(i); + return rv; } -void BoolSet::Unset(int number) +bool Extensible::Register(ExtensionItem* item) { - this->bits &= inverted_bitfields[number]; + return Extensible::extension_types.insert(std::make_pair(item->key, item)).second; } -void BoolSet::Invert(int number) +std::vector Extensible::BeginUnregister(Module* module) { - this->bits ^= bitfields[number]; + std::vector rv; + ExtensibleTypes::iterator i = extension_types.begin(); + while (i != extension_types.end()) + { + ExtensibleTypes::iterator c = i++; + if (c->second->owner == module) + { + rv.push_back(c->second); + extension_types.erase(c); + } + } + return rv; } -bool BoolSet::Get(int number) +void Extensible::doUnhookExtensions(const std::vector& toRemove) { - return ((this->bits | bitfields[number]) > 0); + for(std::vector::const_iterator i = toRemove.begin(); i != toRemove.end(); i++) + { + ExtensibleStore::iterator e = extensions.find((**i).key); + if (e != extensions.end()) + { + (**i).free(e->second); + extensions.erase(e); + } + } } -bool BoolSet::operator==(BoolSet other) +Extensible::~Extensible() { - return (this->bits == other.bits); + for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i) + { + ExtensionItem* type = GetItem(i->first); + if (type) + type->free(i->second); + else if (ServerInstance && ServerInstance->Logs) + ServerInstance->Logs->Log("BASE", ERROR, "Extension type %s is not registered", i->first.c_str()); + } } -BoolSet BoolSet::operator|(BoolSet other) +LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod) { - BoolSet x(this->bits | other.bits); - return x; } -BoolSet BoolSet::operator&(BoolSet other) +LocalExtItem::~LocalExtItem() { - BoolSet x(this->bits & other.bits); - return x; } -BoolSet::BoolSet() +std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) { - this->bits = 0; + return ""; } -BoolSet::BoolSet(char bitmask) +void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value) { - this->bits = bitmask; } -bool BoolSet::operator=(BoolSet other) +LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner) + : SimpleExtItem(Key, Owner) { } + +LocalStringExt::~LocalStringExt() { - this->bits = other.bits; - return true; +} + +std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item) +{ + if (item && format == FORMAT_USER) + return *static_cast(item); + return ""; +} + +LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod) +{ +} + +LocalIntExt::~LocalIntExt() +{ +} + +std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item) +{ + if (format != FORMAT_USER) + return ""; + return ConvToStr(reinterpret_cast(item)); +} + +intptr_t LocalIntExt::get(const Extensible* container) +{ + return reinterpret_cast(get_raw(container)); +} + +intptr_t LocalIntExt::set(Extensible* container, intptr_t value) +{ + if (value) + return reinterpret_cast(set_raw(container, reinterpret_cast(value))); + else + return reinterpret_cast(unset_raw(container)); +} + +void LocalIntExt::free(void*) +{ +} + +StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod) +{ +} + +StringExtItem::~StringExtItem() +{ +} + +std::string* StringExtItem::get(const Extensible* container) +{ + return static_cast(get_raw(container)); +} + +std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) +{ + return item ? *static_cast(item) : ""; +} + +void StringExtItem::unserialize(SerializeFormat format, 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(old); +} + +void StringExtItem::unset(Extensible* container) +{ + void* old = unset_raw(container); + delete static_cast(old); +} + +void StringExtItem::free(void* item) +{ + delete static_cast(item); }