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/>.
28 classbase::classbase()
30 if (ServerInstance && ServerInstance->Logs)
31 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::+ @%p", (void*)this);
34 CullResult classbase::cull()
36 if (ServerInstance && ServerInstance->Logs)
37 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::-%s @%p",
38 typeid(*this).name(), (void*)this);
42 classbase::~classbase()
44 if (ServerInstance && ServerInstance->Logs)
45 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::~ @%p", (void*)this);
48 CullResult::CullResult()
52 // This trick detects heap allocations of refcountbase objects
53 static void* last_heap = NULL;
55 void* refcountbase::operator new(size_t size)
57 last_heap = ::operator new(size);
61 void refcountbase::operator delete(void* obj)
65 ::operator delete(obj);
68 refcountbase::refcountbase() : refcount(0)
70 if (this != last_heap)
71 throw CoreException("Reference allocate on the stack!");
74 refcountbase::~refcountbase()
76 if (refcount && ServerInstance && ServerInstance->Logs)
77 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "refcountbase::~ @%p with refcount %d",
78 (void*)this, refcount);
81 usecountbase::~usecountbase()
83 if (usecount && ServerInstance && ServerInstance->Logs)
84 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "usecountbase::~ @%p with refcount %d",
85 (void*)this, usecount);
88 ServiceProvider::~ServiceProvider()
92 ExtensionItem::ExtensionItem(const std::string& Key, Module* mod) : ServiceProvider(mod, Key, SERVICE_METADATA)
96 ExtensionItem::~ExtensionItem()
100 void* ExtensionItem::get_raw(const Extensible* container) const
102 Extensible::ExtensibleStore::const_iterator i =
103 container->extensions.find(const_cast<ExtensionItem*>(this));
104 if (i == container->extensions.end())
109 void* ExtensionItem::set_raw(Extensible* container, void* value)
111 std::pair<Extensible::ExtensibleStore::iterator,bool> rv =
112 container->extensions.insert(std::make_pair(this, value));
119 void* old = rv.first->second;
120 rv.first->second = value;
125 void* ExtensionItem::unset_raw(Extensible* container)
127 Extensible::ExtensibleStore::iterator i = container->extensions.find(this);
128 if (i == container->extensions.end())
130 void* rv = i->second;
131 container->extensions.erase(i);
135 bool ExtensionManager::Register(ExtensionItem* item)
137 return types.insert(std::make_pair(item->name, item)).second;
140 void ExtensionManager::BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list)
142 std::map<std::string, reference<ExtensionItem> >::iterator i = types.begin();
143 while (i != types.end())
145 std::map<std::string, reference<ExtensionItem> >::iterator me = i++;
146 ExtensionItem* item = me->second;
147 if (item->creator == module)
149 list.push_back(item);
155 ExtensionItem* ExtensionManager::GetItem(const std::string& name)
157 std::map<std::string, reference<ExtensionItem> >::iterator i = types.find(name);
158 if (i == types.end())
163 void Extensible::doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove)
165 for(std::vector<reference<ExtensionItem> >::const_iterator i = toRemove.begin(); i != toRemove.end(); ++i)
167 ExtensionItem* item = *i;
168 ExtensibleStore::iterator e = extensions.find(item);
169 if (e != extensions.end())
171 item->free(e->second);
177 static struct DummyExtensionItem : LocalExtItem
179 DummyExtensionItem() : LocalExtItem("", NULL) {}
183 Extensible::Extensible()
185 extensions[&dummy] = NULL;
188 CullResult Extensible::cull()
191 return classbase::cull();
194 void Extensible::FreeAllExtItems()
196 for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
198 i->first->free(i->second);
203 Extensible::~Extensible()
205 if (!extensions.empty() && ServerInstance && ServerInstance->Logs)
206 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "Extensible destructor called without cull @%p", (void*)this);
209 LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
213 LocalExtItem::~LocalExtItem()
217 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
222 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
226 LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner)
227 : SimpleExtItem<std::string>(Key, Owner) { }
229 LocalStringExt::~LocalStringExt()
233 std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
235 if (item && format == FORMAT_USER)
236 return *static_cast<std::string*>(item);
240 LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod)
244 LocalIntExt::~LocalIntExt()
248 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
250 if (format != FORMAT_USER)
252 return ConvToStr(reinterpret_cast<intptr_t>(item));
255 intptr_t LocalIntExt::get(const Extensible* container) const
257 return reinterpret_cast<intptr_t>(get_raw(container));
260 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
263 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
265 return reinterpret_cast<intptr_t>(unset_raw(container));
268 void LocalIntExt::free(void*)
272 StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
276 StringExtItem::~StringExtItem()
280 std::string* StringExtItem::get(const Extensible* container) const
282 return static_cast<std::string*>(get_raw(container));
285 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
287 return item ? *static_cast<std::string*>(item) : "";
290 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
295 set(container, value);
298 void StringExtItem::set(Extensible* container, const std::string& value)
300 void* old = set_raw(container, new std::string(value));
301 delete static_cast<std::string*>(old);
304 void StringExtItem::unset(Extensible* container)
306 void* old = unset_raw(container);
307 delete static_cast<std::string*>(old);
310 void StringExtItem::free(void* item)
312 delete static_cast<std::string*>(item);
315 ModuleException::ModuleException(const std::string &message, Module* who)
316 : CoreException(message, who ? who->ModuleSourceFile : "A Module")