1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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 ServiceProvider::~ServiceProvider()
84 ExtensionItem::ExtensionItem(const std::string& Key, Module* mod) : ServiceProvider(mod, Key, SERVICE_METADATA)
88 ExtensionItem::~ExtensionItem()
92 void* ExtensionItem::get_raw(const Extensible* container) const
94 Extensible::ExtensibleStore::const_iterator i =
95 container->extensions.find(const_cast<ExtensionItem*>(this));
96 if (i == container->extensions.end())
101 void* ExtensionItem::set_raw(Extensible* container, void* value)
103 std::pair<Extensible::ExtensibleStore::iterator,bool> rv =
104 container->extensions.insert(std::make_pair(this, value));
111 void* old = rv.first->second;
112 rv.first->second = value;
117 void* ExtensionItem::unset_raw(Extensible* container)
119 Extensible::ExtensibleStore::iterator i = container->extensions.find(this);
120 if (i == container->extensions.end())
122 void* rv = i->second;
123 container->extensions.erase(i);
127 void ExtensionManager::Register(ExtensionItem* item)
129 types.insert(std::make_pair(item->name, item));
132 void ExtensionManager::BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list)
134 std::map<std::string, reference<ExtensionItem> >::iterator i = types.begin();
135 while (i != types.end())
137 std::map<std::string, reference<ExtensionItem> >::iterator me = i++;
138 ExtensionItem* item = me->second;
139 if (item->creator == module)
141 list.push_back(item);
147 ExtensionItem* ExtensionManager::GetItem(const std::string& name)
149 std::map<std::string, reference<ExtensionItem> >::iterator i = types.find(name);
150 if (i == types.end())
155 void Extensible::doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove)
157 for(std::vector<reference<ExtensionItem> >::const_iterator i = toRemove.begin(); i != toRemove.end(); ++i)
159 ExtensionItem* item = *i;
160 ExtensibleStore::iterator e = extensions.find(item);
161 if (e != extensions.end())
163 item->free(e->second);
169 static struct DummyExtensionItem : LocalExtItem
171 DummyExtensionItem() : LocalExtItem("", NULL) {}
175 Extensible::Extensible()
177 extensions[&dummy] = NULL;
180 CullResult Extensible::cull()
182 for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
184 i->first->free(i->second);
187 return classbase::cull();
190 Extensible::~Extensible()
192 if (!extensions.empty() && ServerInstance && ServerInstance->Logs)
193 ServerInstance->Logs->Log("CULLLIST", DEBUG,
194 "Extensible destructor called without cull @%p", (void*)this);
197 LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
201 LocalExtItem::~LocalExtItem()
205 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
210 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
214 LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner)
215 : SimpleExtItem<std::string>(Key, Owner) { }
217 LocalStringExt::~LocalStringExt()
221 std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
223 if (item && format == FORMAT_USER)
224 return *static_cast<std::string*>(item);
228 LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod)
232 LocalIntExt::~LocalIntExt()
236 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
238 if (format != FORMAT_USER)
240 return ConvToStr(reinterpret_cast<intptr_t>(item));
243 intptr_t LocalIntExt::get(const Extensible* container) const
245 return reinterpret_cast<intptr_t>(get_raw(container));
248 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
251 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
253 return reinterpret_cast<intptr_t>(unset_raw(container));
256 void LocalIntExt::free(void*)
260 StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
264 StringExtItem::~StringExtItem()
268 std::string* StringExtItem::get(const Extensible* container) const
270 return static_cast<std::string*>(get_raw(container));
273 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
275 return item ? *static_cast<std::string*>(item) : "";
278 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
283 set(container, value);
286 void StringExtItem::set(Extensible* container, const std::string& value)
288 void* old = set_raw(container, new std::string(value));
289 delete static_cast<std::string*>(old);
292 void StringExtItem::unset(Extensible* container)
294 void* old = unset_raw(container);
295 delete static_cast<std::string*>(old);
298 void StringExtItem::free(void* item)
300 delete static_cast<std::string*>(item);
303 ModuleException::ModuleException(const std::string &message, Module* who)
304 : CoreException(message, who ? who->ModuleSourceFile : "A Module")