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()
31 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::+ @%p", (void*)this);
34 CullResult classbase::cull()
37 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::-%s @%p",
38 typeid(*this).name(), (void*)this);
42 classbase::~classbase()
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)
77 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "refcountbase::~ @%p with refcount %d",
78 (void*)this, refcount);
81 usecountbase::~usecountbase()
83 if (usecount && ServerInstance)
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, ExtensibleType exttype, Module* mod)
93 : ServiceProvider(mod, Key, SERVICE_METADATA)
98 ExtensionItem::~ExtensionItem()
102 void* ExtensionItem::get_raw(const Extensible* container) const
104 Extensible::ExtensibleStore::const_iterator i =
105 container->extensions.find(const_cast<ExtensionItem*>(this));
106 if (i == container->extensions.end())
111 void* ExtensionItem::set_raw(Extensible* container, void* value)
113 std::pair<Extensible::ExtensibleStore::iterator,bool> rv =
114 container->extensions.insert(std::make_pair(this, value));
121 void* old = rv.first->second;
122 rv.first->second = value;
127 void* ExtensionItem::unset_raw(Extensible* container)
129 Extensible::ExtensibleStore::iterator i = container->extensions.find(this);
130 if (i == container->extensions.end())
132 void* rv = i->second;
133 container->extensions.erase(i);
137 bool ExtensionManager::Register(ExtensionItem* item)
139 return types.insert(std::make_pair(item->name, item)).second;
142 void ExtensionManager::BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list)
144 std::map<std::string, reference<ExtensionItem> >::iterator i = types.begin();
145 while (i != types.end())
147 std::map<std::string, reference<ExtensionItem> >::iterator me = i++;
148 ExtensionItem* item = me->second;
149 if (item->creator == module)
151 list.push_back(item);
157 ExtensionItem* ExtensionManager::GetItem(const std::string& name)
159 std::map<std::string, reference<ExtensionItem> >::iterator i = types.find(name);
160 if (i == types.end())
165 void Extensible::doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove)
167 for(std::vector<reference<ExtensionItem> >::const_iterator i = toRemove.begin(); i != toRemove.end(); ++i)
169 ExtensionItem* item = *i;
170 ExtensibleStore::iterator e = extensions.find(item);
171 if (e != extensions.end())
173 item->free(e->second);
179 Extensible::Extensible()
184 CullResult Extensible::cull()
188 return classbase::cull();
191 void Extensible::FreeAllExtItems()
193 for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
195 i->first->free(i->second);
200 Extensible::~Extensible()
202 if ((!extensions.empty() || !culled) && ServerInstance)
203 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "Extensible destructor called without cull @%p", (void*)this);
206 LocalExtItem::LocalExtItem(const std::string& Key, ExtensibleType exttype, Module* mod)
207 : ExtensionItem(Key, exttype, mod)
211 LocalExtItem::~LocalExtItem()
215 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
220 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
224 LocalStringExt::LocalStringExt(const std::string& Key, ExtensibleType exttype, Module* Owner)
225 : SimpleExtItem<std::string>(Key, exttype, 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, ExtensibleType exttype, Module* mod)
241 : LocalExtItem(Key, exttype, mod)
245 LocalIntExt::~LocalIntExt()
249 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
251 if (format != FORMAT_USER)
253 return ConvToStr(reinterpret_cast<intptr_t>(item));
256 intptr_t LocalIntExt::get(const Extensible* container) const
258 return reinterpret_cast<intptr_t>(get_raw(container));
261 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
264 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
266 return reinterpret_cast<intptr_t>(unset_raw(container));
269 void LocalIntExt::free(void*)
273 StringExtItem::StringExtItem(const std::string& Key, ExtensibleType exttype, Module* mod)
274 : ExtensionItem(Key, exttype, mod)
278 StringExtItem::~StringExtItem()
282 std::string* StringExtItem::get(const Extensible* container) const
284 return static_cast<std::string*>(get_raw(container));
287 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
289 return item ? *static_cast<std::string*>(item) : "";
292 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
297 set(container, value);
300 void StringExtItem::set(Extensible* container, const std::string& value)
302 void* old = set_raw(container, new std::string(value));
303 delete static_cast<std::string*>(old);
306 void StringExtItem::unset(Extensible* container)
308 void* old = unset_raw(container);
309 delete static_cast<std::string*>(old);
312 void StringExtItem::free(void* item)
314 delete static_cast<std::string*>(item);
317 ModuleException::ModuleException(const std::string &message, Module* who)
318 : CoreException(message, who ? who->ModuleSourceFile : "A Module")