2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2004-2006 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "inspircd_config.h"
29 classbase::classbase()
31 if (ServerInstance && ServerInstance->Logs)
32 ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::+ @%p", (void*)this);
35 CullResult classbase::cull()
37 if (ServerInstance && ServerInstance->Logs)
38 ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::-%s @%p",
39 typeid(*this).name(), (void*)this);
43 classbase::~classbase()
45 if (ServerInstance && ServerInstance->Logs)
46 ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::~ @%p", (void*)this);
49 CullResult::CullResult()
53 // This trick detects heap allocations of refcountbase objects
54 static void* last_heap = NULL;
56 void* refcountbase::operator new(size_t size)
58 last_heap = ::operator new(size);
62 void refcountbase::operator delete(void* obj)
66 ::operator delete(obj);
69 refcountbase::refcountbase() : refcount(0)
71 if (this != last_heap)
72 throw CoreException("Reference allocate on the stack!");
75 refcountbase::~refcountbase()
77 if (refcount && ServerInstance && ServerInstance->Logs)
78 ServerInstance->Logs->Log("CULLLIST", DEBUG, "refcountbase::~ @%p with refcount %d",
79 (void*)this, refcount);
82 usecountbase::~usecountbase()
84 if (usecount && ServerInstance && ServerInstance->Logs)
85 ServerInstance->Logs->Log("CULLLIST", DEBUG, "usecountbase::~ @%p with refcount %d",
86 (void*)this, usecount);
89 ServiceProvider::~ServiceProvider()
93 ExtensionItem::ExtensionItem(const std::string& Key, Module* mod) : ServiceProvider(mod, Key, SERVICE_METADATA)
97 ExtensionItem::~ExtensionItem()
101 void* ExtensionItem::get_raw(const Extensible* container) const
103 Extensible::ExtensibleStore::const_iterator i =
104 container->extensions.find(const_cast<ExtensionItem*>(this));
105 if (i == container->extensions.end())
110 void* ExtensionItem::set_raw(Extensible* container, void* value)
112 std::pair<Extensible::ExtensibleStore::iterator,bool> rv =
113 container->extensions.insert(std::make_pair(this, value));
120 void* old = rv.first->second;
121 rv.first->second = value;
126 void* ExtensionItem::unset_raw(Extensible* container)
128 Extensible::ExtensibleStore::iterator i = container->extensions.find(this);
129 if (i == container->extensions.end())
131 void* rv = i->second;
132 container->extensions.erase(i);
136 void ExtensionManager::Register(ExtensionItem* item)
138 types.insert(std::make_pair(item->name, item));
141 void ExtensionManager::BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list)
143 std::map<std::string, reference<ExtensionItem> >::iterator i = types.begin();
144 while (i != types.end())
146 std::map<std::string, reference<ExtensionItem> >::iterator me = i++;
147 ExtensionItem* item = me->second;
148 if (item->creator == module)
150 list.push_back(item);
156 ExtensionItem* ExtensionManager::GetItem(const std::string& name)
158 std::map<std::string, reference<ExtensionItem> >::iterator i = types.find(name);
159 if (i == types.end())
164 void Extensible::doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove)
166 for(std::vector<reference<ExtensionItem> >::const_iterator i = toRemove.begin(); i != toRemove.end(); ++i)
168 ExtensionItem* item = *i;
169 ExtensibleStore::iterator e = extensions.find(item);
170 if (e != extensions.end())
172 item->free(e->second);
178 static struct DummyExtensionItem : LocalExtItem
180 DummyExtensionItem() : LocalExtItem("", NULL) {}
184 Extensible::Extensible()
186 extensions[&dummy] = NULL;
189 CullResult Extensible::cull()
191 for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
193 i->first->free(i->second);
196 return classbase::cull();
199 Extensible::~Extensible()
201 if (!extensions.empty() && ServerInstance && ServerInstance->Logs)
202 ServerInstance->Logs->Log("CULLLIST", DEBUG,
203 "Extensible destructor called without cull @%p", (void*)this);
206 LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
210 LocalExtItem::~LocalExtItem()
214 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
219 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
223 LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner)
224 : SimpleExtItem<std::string>(Key, Owner) { }
226 LocalStringExt::~LocalStringExt()
230 std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
232 if (item && format == FORMAT_USER)
233 return *static_cast<std::string*>(item);
237 LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod)
241 LocalIntExt::~LocalIntExt()
245 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
247 if (format != FORMAT_USER)
249 return ConvToStr(reinterpret_cast<intptr_t>(item));
252 intptr_t LocalIntExt::get(const Extensible* container) const
254 return reinterpret_cast<intptr_t>(get_raw(container));
257 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
260 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
262 return reinterpret_cast<intptr_t>(unset_raw(container));
265 void LocalIntExt::free(void*)
269 StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
273 StringExtItem::~StringExtItem()
277 std::string* StringExtItem::get(const Extensible* container) const
279 return static_cast<std::string*>(get_raw(container));
282 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
284 return item ? *static_cast<std::string*>(item) : "";
287 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
292 set(container, value);
295 void StringExtItem::set(Extensible* container, const std::string& value)
297 void* old = set_raw(container, new std::string(value));
298 delete static_cast<std::string*>(old);
301 void StringExtItem::unset(Extensible* container)
303 void* old = unset_raw(container);
304 delete static_cast<std::string*>(old);
307 void StringExtItem::free(void* item)
309 delete static_cast<std::string*>(item);
312 ModuleException::ModuleException(const std::string &message, Module* who)
313 : CoreException(message, who ? who->ModuleSourceFile : "A Module")