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 // This trick detects heap allocations of refcountbase objects
45 static void* last_heap = NULL;
47 void* refcountbase::operator new(size_t size)
49 last_heap = ::operator new(size);
53 void refcountbase::operator delete(void* obj)
57 ::operator delete(obj);
60 refcountbase::refcountbase() : refcount(0)
62 if (this != last_heap)
63 throw CoreException("Reference allocate on the stack!");
66 refcountbase::~refcountbase()
68 if (refcount && ServerInstance && ServerInstance->Logs)
69 ServerInstance->Logs->Log("CULLLIST", DEBUG, "refcountbase::~ @%p with refcount %d",
70 (void*)this, refcount);
73 usecountbase::~usecountbase()
75 if (usecount && ServerInstance && ServerInstance->Logs)
76 ServerInstance->Logs->Log("CULLLIST", DEBUG, "usecountbase::~ @%p with refcount %d",
77 (void*)this, usecount);
80 ExtensionItem::ExtensionItem(const std::string& Key, Module* mod) : key(Key), owner(mod)
84 ExtensionItem::~ExtensionItem()
88 void* ExtensionItem::get_raw(const Extensible* container) const
90 Extensible::ExtensibleStore::const_iterator i =
91 container->extensions.find(const_cast<ExtensionItem*>(this));
92 if (i == container->extensions.end())
97 void* ExtensionItem::set_raw(Extensible* container, void* value)
99 std::pair<Extensible::ExtensibleStore::iterator,bool> rv =
100 container->extensions.insert(std::make_pair(this, value));
107 void* old = rv.first->second;
108 rv.first->second = value;
113 void* ExtensionItem::unset_raw(Extensible* container)
115 Extensible::ExtensibleStore::iterator i = container->extensions.find(this);
116 if (i == container->extensions.end())
118 void* rv = i->second;
119 container->extensions.erase(i);
123 void ExtensionManager::Register(ExtensionItem* item)
125 types.insert(std::make_pair(item->key, item));
128 void ExtensionManager::BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list)
130 std::map<std::string, reference<ExtensionItem> >::iterator i = types.begin();
131 while (i != types.end())
133 std::map<std::string, reference<ExtensionItem> >::iterator me = i++;
134 ExtensionItem* item = me->second;
135 if (item->owner == module)
137 list.push_back(item);
143 ExtensionItem* ExtensionManager::GetItem(const std::string& name)
145 std::map<std::string, reference<ExtensionItem> >::iterator i = types.find(name);
146 if (i == types.end())
151 void Extensible::doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove)
153 for(std::vector<reference<ExtensionItem> >::const_iterator i = toRemove.begin(); i != toRemove.end(); ++i)
155 ExtensionItem* item = *i;
156 ExtensibleStore::iterator e = extensions.find(item);
157 if (e != extensions.end())
159 item->free(e->second);
165 static struct DummyExtensionItem : LocalExtItem
167 DummyExtensionItem() : LocalExtItem("", NULL) {}
171 Extensible::Extensible()
173 extensions[&dummy] = NULL;
176 CullResult Extensible::cull()
178 for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
180 i->first->free(i->second);
183 return classbase::cull();
186 Extensible::~Extensible()
188 if (!extensions.empty() && ServerInstance && ServerInstance->Logs)
189 ServerInstance->Logs->Log("CULLLIST", DEBUG,
190 "Extensible destructor called without cull @%p", (void*)this);
193 LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
197 LocalExtItem::~LocalExtItem()
201 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
206 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
210 LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner)
211 : SimpleExtItem<std::string>(Key, Owner) { }
213 LocalStringExt::~LocalStringExt()
217 std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
219 if (item && format == FORMAT_USER)
220 return *static_cast<std::string*>(item);
224 LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod)
228 LocalIntExt::~LocalIntExt()
232 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
234 if (format != FORMAT_USER)
236 return ConvToStr(reinterpret_cast<intptr_t>(item));
239 intptr_t LocalIntExt::get(const Extensible* container) const
241 return reinterpret_cast<intptr_t>(get_raw(container));
244 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
247 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
249 return reinterpret_cast<intptr_t>(unset_raw(container));
252 void LocalIntExt::free(void*)
256 StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
260 StringExtItem::~StringExtItem()
264 std::string* StringExtItem::get(const Extensible* container) const
266 return static_cast<std::string*>(get_raw(container));
269 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
271 return item ? *static_cast<std::string*>(item) : "";
274 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
279 set(container, value);
282 void StringExtItem::set(Extensible* container, const std::string& value)
284 void* old = set_raw(container, new std::string(value));
285 delete static_cast<std::string*>(old);
288 void StringExtItem::unset(Extensible* container)
290 void* old = unset_raw(container);
291 delete static_cast<std::string*>(old);
294 void StringExtItem::free(void* item)
296 delete static_cast<std::string*>(item);
299 ModuleException::ModuleException(const std::string &message, Module* who)
300 : CoreException(message, who ? who->ModuleSourceFile : "A Module")