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()
190 for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
192 i->first->free(i->second);
195 return classbase::cull();
198 Extensible::~Extensible()
200 if (!extensions.empty() && ServerInstance && ServerInstance->Logs)
201 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG,
202 "Extensible destructor called without cull @%p", (void*)this);
205 LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
209 LocalExtItem::~LocalExtItem()
213 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
218 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
222 LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner)
223 : SimpleExtItem<std::string>(Key, Owner) { }
225 LocalStringExt::~LocalStringExt()
229 std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
231 if (item && format == FORMAT_USER)
232 return *static_cast<std::string*>(item);
236 LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod)
240 LocalIntExt::~LocalIntExt()
244 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
246 if (format != FORMAT_USER)
248 return ConvToStr(reinterpret_cast<intptr_t>(item));
251 intptr_t LocalIntExt::get(const Extensible* container) const
253 return reinterpret_cast<intptr_t>(get_raw(container));
256 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
259 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
261 return reinterpret_cast<intptr_t>(unset_raw(container));
264 void LocalIntExt::free(void*)
268 StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
272 StringExtItem::~StringExtItem()
276 std::string* StringExtItem::get(const Extensible* container) const
278 return static_cast<std::string*>(get_raw(container));
281 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
283 return item ? *static_cast<std::string*>(item) : "";
286 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
291 set(container, value);
294 void StringExtItem::set(Extensible* container, const std::string& value)
296 void* old = set_raw(container, new std::string(value));
297 delete static_cast<std::string*>(old);
300 void StringExtItem::unset(Extensible* container)
302 void* old = unset_raw(container);
303 delete static_cast<std::string*>(old);
306 void StringExtItem::free(void* item)
308 delete static_cast<std::string*>(item);
311 ModuleException::ModuleException(const std::string &message, Module* who)
312 : CoreException(message, who ? who->ModuleSourceFile : "A Module")