X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fbase.cpp;h=59196fec1d6242fb1ca1a3f6ffad4cacd915517f;hb=a8878569083bfa4753e9e118adee0ed1da6a0325;hp=eb6ea900ab1b12c2f7101491d53199a449b7fc98;hpb=5fb54ac29a36131d2e49991095ef9741abb5f94e;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/base.cpp b/src/base.cpp index eb6ea900a..59196fec1 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -2,12 +2,9 @@ * | Inspire Internet Relay Chat Daemon | * +------------------------------------+ * - * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. - * E-mail: - * - * - * - * Written by Craig Edwards, Craig McLure, and others. + * 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. * @@ -17,128 +14,258 @@ #include "inspircd_config.h" #include "base.h" #include -#include -#include -#include #include "inspircd.h" -#include "modules.h" -#include "helperfuncs.h" +#include -const int bitfields[] = {1,2,4,8,16,32,64,128}; -const int inverted_bitfields[] = {~1,~2,~4,~8,~16,~32,~64,~128}; +classbase::classbase() +{ + if (ServerInstance && ServerInstance->Logs) + ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::+%s @%p", + typeid(*this).name(), (void*)this); +} -extern time_t TIME; +CullResult classbase::cull() +{ + if (ServerInstance && ServerInstance->Logs) + ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::-%s @%p", + typeid(*this).name(), (void*)this); + return CullResult(); +} -/* This is now a template in base.h - * - * bool Extensible::Extend(const std::string &key, char* p) - * { - * // only add an item if it doesnt already exist - * if (this->Extension_Items.find(key) == this->Extension_Items.end()) - * { - * this->Extension_Items[key] = p; - * log(DEBUG,"Extending object with item %s",key.c_str()); - * return true; - * } - * // item already exists, return false - * return false; - * } - */ +classbase::~classbase() +{ + if (ServerInstance && ServerInstance->Logs) + ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::~%s @%p", + typeid(*this).name(), (void*)this); +} + +CullResult::CullResult() +{ +} + +refcountbase::refcountbase() : refcount(0) +{ +} -bool Extensible::Shrink(const std::string &key) +refcountbase::~refcountbase() { - /* 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 - */ - if(this->Extension_Items.erase(key)) +} + +ExtensionItem::ExtensionItem(const std::string& Key, Module* mod) : key(Key), owner(mod) +{ +} + +ExtensionItem::~ExtensionItem() +{ +} + +void* ExtensionItem::get_raw(const Extensible* container) const +{ + Extensible::ExtensibleStore::const_iterator i = + container->extensions.find(const_cast(this)); + 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(this, value)); + if (rv.second) { - log(DEBUG, "Shrinking object with item %s",key.c_str()); - return true; + return NULL; } else { - log(DEBUG, "Tried to shrink object with item %s but no items removed", key.c_str()); - return false; + void* old = rv.first->second; + rv.first->second = value; + return old; } } -char* Extensible::GetExt(const std::string &key) +void* ExtensionItem::unset_raw(Extensible* container) +{ + Extensible::ExtensibleStore::iterator i = container->extensions.find(this); + if (i == container->extensions.end()) + return NULL; + void* rv = i->second; + container->extensions.erase(i); + return rv; +} + +void ExtensionManager::Register(ExtensionItem* item) { - /* This was calling ExtensibleStore::find() twice, - * once to see if there was a value, and again to - * get that value if it was there. Now we store - * the iterator so we only have to search for it once. - */ - ExtensibleStore::iterator iter = this->Extension_Items.find(key); - - if(iter != this->Extension_Items.end()) + types.insert(std::make_pair(item->key, item)); +} + +void ExtensionManager::BeginUnregister(Module* module, std::vector& list) +{ + std::map::iterator i = types.begin(); + while (i != types.end()) { - return iter->second; + std::map::iterator me = i++; + ExtensionItem* item = me->second; + if (item->owner == module) + { + list.push_back(item); + types.erase(me); + } } - else - { +} + +ExtensionItem* ExtensionManager::GetItem(const std::string& name) +{ + std::map::iterator i = types.find(name); + if (i == types.end()) return NULL; + return i->second; +} + +void Extensible::doUnhookExtensions(const std::vector& toRemove) +{ + for(std::vector::const_iterator i = toRemove.begin(); i != toRemove.end(); ++i) + { + ExtensionItem* item = *i; + ExtensibleStore::iterator e = extensions.find(item); + if (e != extensions.end()) + { + item->free(e->second); + extensions.erase(e); + } } } -void Extensible::GetExtList(std::deque &list) +static struct DummyExtensionItem : LocalExtItem +{ + DummyExtensionItem() : LocalExtItem("", NULL) {} + void free(void*) {} +} dummy; + +Extensible::Extensible() { - for (ExtensibleStore::iterator u = Extension_Items.begin(); u != Extension_Items.end(); u++) + extensions[&dummy] = NULL; +} + +CullResult Extensible::cull() +{ + for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i) { - list.push_back(u->first); + i->first->free(i->second); } + extensions.clear(); + return classbase::cull(); +} + +Extensible::~Extensible() +{ + if (!extensions.empty() && ServerInstance && ServerInstance->Logs) + ServerInstance->Logs->Log("CULLLIST", DEBUG, + "Extensible destructor called without cull @%p", (void*)this); +} + +LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod) +{ +} + +LocalExtItem::~LocalExtItem() +{ } -void BoolSet::Set(int number) +std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const { - this->bits |= bitfields[number]; + return ""; } -void BoolSet::Unset(int number) +void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value) { - this->bits &= inverted_bitfields[number]; } -void BoolSet::Invert(int number) +LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner) + : SimpleExtItem(Key, Owner) { } + +LocalStringExt::~LocalStringExt() { - this->bits ^= bitfields[number]; } -bool BoolSet::Get(int number) +std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item) const { - return ((this->bits | bitfields[number]) > 0); + if (item && format == FORMAT_USER) + return *static_cast(item); + return ""; } -bool BoolSet::operator==(BoolSet other) +LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod) { - return (this->bits == other.bits); } -BoolSet BoolSet::operator|(BoolSet other) +LocalIntExt::~LocalIntExt() { - BoolSet x(this->bits | other.bits); - return x; } -BoolSet BoolSet::operator&(BoolSet other) +std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item) const { - BoolSet x(this->bits & other.bits); - return x; + if (format != FORMAT_USER) + return ""; + return ConvToStr(reinterpret_cast(item)); +} + +intptr_t LocalIntExt::get(const Extensible* container) const +{ + 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) const +{ + return static_cast(get_raw(container)); +} + +std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const +{ + 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); } -BoolSet::BoolSet() +void StringExtItem::set(Extensible* container, const std::string& value) { - this->bits = 0; + void* old = set_raw(container, new std::string(value)); + delete static_cast(old); } -BoolSet::BoolSet(char bitmask) +void StringExtItem::unset(Extensible* container) { - this->bits = bitmask; + void* old = unset_raw(container); + delete static_cast(old); } -bool BoolSet::operator=(BoolSet other) +void StringExtItem::free(void* item) { - this->bits = other.bits; - return true; + delete static_cast(item); }