1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #include "inspircd_config.h"
20 classbase::classbase()
22 if (ServerInstance && ServerInstance->Logs)
23 ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::+ @%p", (void*)this);
26 CullResult classbase::cull()
28 if (ServerInstance && ServerInstance->Logs)
29 ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::-%s @%p",
30 typeid(*this).name(), (void*)this);
34 classbase::~classbase()
36 if (ServerInstance && ServerInstance->Logs)
37 ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::~ @%p", (void*)this);
40 CullResult::CullResult()
44 refcountbase::refcountbase() : refcount(0)
48 refcountbase::~refcountbase()
50 if (refcount && ServerInstance && ServerInstance->Logs)
51 ServerInstance->Logs->Log("CULLLIST", DEBUG, "refcountbase::~ @%p with refcount %d",
52 (void*)this, refcount);
55 ExtensionItem::ExtensionItem(const std::string& Key, Module* mod) : key(Key), owner(mod)
59 ExtensionItem::~ExtensionItem()
63 void* ExtensionItem::get_raw(const Extensible* container) const
65 Extensible::ExtensibleStore::const_iterator i =
66 container->extensions.find(const_cast<ExtensionItem*>(this));
67 if (i == container->extensions.end())
72 void* ExtensionItem::set_raw(Extensible* container, void* value)
74 std::pair<Extensible::ExtensibleStore::iterator,bool> rv =
75 container->extensions.insert(std::make_pair(this, value));
82 void* old = rv.first->second;
83 rv.first->second = value;
88 void* ExtensionItem::unset_raw(Extensible* container)
90 Extensible::ExtensibleStore::iterator i = container->extensions.find(this);
91 if (i == container->extensions.end())
94 container->extensions.erase(i);
98 void ExtensionManager::Register(ExtensionItem* item)
100 types.insert(std::make_pair(item->key, item));
103 void ExtensionManager::BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list)
105 std::map<std::string, reference<ExtensionItem> >::iterator i = types.begin();
106 while (i != types.end())
108 std::map<std::string, reference<ExtensionItem> >::iterator me = i++;
109 ExtensionItem* item = me->second;
110 if (item->owner == module)
112 list.push_back(item);
118 ExtensionItem* ExtensionManager::GetItem(const std::string& name)
120 std::map<std::string, reference<ExtensionItem> >::iterator i = types.find(name);
121 if (i == types.end())
126 void Extensible::doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove)
128 for(std::vector<reference<ExtensionItem> >::const_iterator i = toRemove.begin(); i != toRemove.end(); ++i)
130 ExtensionItem* item = *i;
131 ExtensibleStore::iterator e = extensions.find(item);
132 if (e != extensions.end())
134 item->free(e->second);
140 static struct DummyExtensionItem : LocalExtItem
142 DummyExtensionItem() : LocalExtItem("", NULL) {}
146 Extensible::Extensible()
148 extensions[&dummy] = NULL;
151 CullResult Extensible::cull()
153 for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
155 i->first->free(i->second);
158 return classbase::cull();
161 Extensible::~Extensible()
163 if (!extensions.empty() && ServerInstance && ServerInstance->Logs)
164 ServerInstance->Logs->Log("CULLLIST", DEBUG,
165 "Extensible destructor called without cull @%p", (void*)this);
168 LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
172 LocalExtItem::~LocalExtItem()
176 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
181 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
185 LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner)
186 : SimpleExtItem<std::string>(Key, Owner) { }
188 LocalStringExt::~LocalStringExt()
192 std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
194 if (item && format == FORMAT_USER)
195 return *static_cast<std::string*>(item);
199 LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod)
203 LocalIntExt::~LocalIntExt()
207 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
209 if (format != FORMAT_USER)
211 return ConvToStr(reinterpret_cast<intptr_t>(item));
214 intptr_t LocalIntExt::get(const Extensible* container) const
216 return reinterpret_cast<intptr_t>(get_raw(container));
219 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
222 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
224 return reinterpret_cast<intptr_t>(unset_raw(container));
227 void LocalIntExt::free(void*)
231 StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
235 StringExtItem::~StringExtItem()
239 std::string* StringExtItem::get(const Extensible* container) const
241 return static_cast<std::string*>(get_raw(container));
244 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
246 return item ? *static_cast<std::string*>(item) : "";
249 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
254 set(container, value);
257 void StringExtItem::set(Extensible* container, const std::string& value)
259 void* old = set_raw(container, new std::string(value));
260 delete static_cast<std::string*>(old);
263 void StringExtItem::unset(Extensible* container)
265 void* old = unset_raw(container);
266 delete static_cast<std::string*>(old);
269 void StringExtItem::free(void* item)
271 delete static_cast<std::string*>(item);
274 ModuleException::ModuleException(const std::string &message, Module* who)
275 : CoreException(message, who ? who->ModuleSourceFile : "A Module")
279 ModuleRef::ModuleRef(Module* v) : value(v)
281 if (value) inc(value);
284 ModuleRef::~ModuleRef()
286 if (value) dec(value);