* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
- * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- * E-mail:
- * <brain@chatspike.net>
- * <Craig@chatspike.net>
- *
- * 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.
*
* ---------------------------------------------------
*/
+/* $Core */
+
#include "inspircd_config.h"
#include "base.h"
#include <time.h>
-#include <map>
-#include <deque>
-#include <string>
#include "inspircd.h"
-#include "modules.h"
-#include "helperfuncs.h"
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;
-extern time_t TIME;
-
-/* 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;
- * }
- */
-
-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
- */
- if(this->Extension_Items.erase(key))
- {
- log(DEBUG, "Shrinking object with item %s",key.c_str());
- return true;
- }
- else
- {
- log(DEBUG, "Tried to shrink object with item %s but no items removed", key.c_str());
- return false;
- }
-}
-
-char* Extensible::GetExt(const std::string &key)
+classbase::classbase()
{
- /* 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())
- {
- return iter->second;
- }
- else
- {
- return NULL;
- }
-}
-
-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 = 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);
+}