X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fbase.cpp;h=59196fec1d6242fb1ca1a3f6ffad4cacd915517f;hb=a8878569083bfa4753e9e118adee0ed1da6a0325;hp=ef6ff5ccf6f3e601df5199f4ef5b9d74163b0e4e;hpb=bab14f0dd2345c9d7dcbc47c918563709e1ac094;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/base.cpp b/src/base.cpp index ef6ff5ccf..59196fec1 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -1 +1,271 @@ -/* +------------------------------------+ * | Inspire Internet Relay Chat Daemon | * +------------------------------------+ * * InspIRCd: (C) 2002-2007 InspIRCd Development Team * See: http://www.inspircd.org/wiki/index.php/Credits * * This program is free but copyrighted software; see * the file COPYING for details. * * --------------------------------------------------- */ #include "inspircd_config.h" #include "base.h" #include #include "inspircd.h" 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() { this->age = time(NULL); } 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 &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]; } void BoolSet::Unset(int number) { this->bits &= inverted_bitfields[number]; } void BoolSet::Invert(int number) { this->bits ^= bitfields[number]; } bool BoolSet::Get(int number) { return ((this->bits | bitfields[number]) > 0); } bool BoolSet::operator==(BoolSet other) { return (this->bits == other.bits); } BoolSet BoolSet::operator|(BoolSet other) { BoolSet x(this->bits | other.bits); return x; } BoolSet BoolSet::operator&(BoolSet other) { BoolSet x(this->bits & other.bits); return x; } BoolSet::BoolSet() { this->bits = 0; } BoolSet::BoolSet(char bitmask) { this->bits = bitmask; } bool BoolSet::operator=(BoolSet other) { this->bits = other.bits; return true; } \ No newline at end of file +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * 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. + * + * --------------------------------------------------- + */ + +#include "inspircd_config.h" +#include "base.h" +#include +#include "inspircd.h" +#include + +classbase::classbase() +{ + if (ServerInstance && ServerInstance->Logs) + ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::+%s @%p", + typeid(*this).name(), (void*)this); +} + +CullResult classbase::cull() +{ + if (ServerInstance && ServerInstance->Logs) + ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::-%s @%p", + typeid(*this).name(), (void*)this); + return CullResult(); +} + +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) +{ +} + +refcountbase::~refcountbase() +{ +} + +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) + { + return NULL; + } + else + { + void* old = rv.first->second; + rv.first->second = value; + return old; + } +} + +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) +{ + 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()) + { + std::map::iterator me = i++; + ExtensionItem* item = me->second; + if (item->owner == module) + { + list.push_back(item); + types.erase(me); + } + } +} + +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); + } + } +} + +static struct DummyExtensionItem : LocalExtItem +{ + DummyExtensionItem() : LocalExtItem("", NULL) {} + void free(void*) {} +} dummy; + +Extensible::Extensible() +{ + extensions[&dummy] = NULL; +} + +CullResult Extensible::cull() +{ + for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i) + { + 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() +{ +} + +std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const +{ + return ""; +} + +void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value) +{ +} + +LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner) + : SimpleExtItem(Key, Owner) { } + +LocalStringExt::~LocalStringExt() +{ +} + +std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item) const +{ + 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) const +{ + 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); +} + +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); +}