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/>.
26 #ifdef INSPIRCD_ENABLE_RTTI
30 classbase::classbase()
33 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::+ @%p", (void*)this);
36 CullResult classbase::cull()
39 #ifdef INSPIRCD_ENABLE_RTTI
40 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::-%s @%p",
41 typeid(*this).name(), (void*)this);
43 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::- @%p", (void*)this);
48 classbase::~classbase()
51 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::~ @%p", (void*)this);
54 CullResult::CullResult()
58 // This trick detects heap allocations of refcountbase objects
59 static void* last_heap = NULL;
61 void* refcountbase::operator new(size_t size)
63 last_heap = ::operator new(size);
67 void refcountbase::operator delete(void* obj)
71 ::operator delete(obj);
74 refcountbase::refcountbase() : refcount(0)
76 if (this != last_heap)
77 throw CoreException("Reference allocate on the stack!");
80 refcountbase::~refcountbase()
82 if (refcount && ServerInstance)
83 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "refcountbase::~ @%p with refcount %d",
84 (void*)this, refcount);
87 usecountbase::~usecountbase()
89 if (usecount && ServerInstance)
90 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "usecountbase::~ @%p with refcount %d",
91 (void*)this, usecount);
94 ServiceProvider::~ServiceProvider()
98 void ServiceProvider::RegisterService()
102 ExtensionItem::ExtensionItem(const std::string& Key, ExtensibleType exttype, Module* mod)
103 : ServiceProvider(mod, Key, SERVICE_METADATA)
108 ExtensionItem::~ExtensionItem()
112 void* ExtensionItem::get_raw(const Extensible* container) const
114 Extensible::ExtensibleStore::const_iterator i =
115 container->extensions.find(const_cast<ExtensionItem*>(this));
116 if (i == container->extensions.end())
121 void* ExtensionItem::set_raw(Extensible* container, void* value)
123 std::pair<Extensible::ExtensibleStore::iterator,bool> rv =
124 container->extensions.insert(std::make_pair(this, value));
131 void* old = rv.first->second;
132 rv.first->second = value;
137 void* ExtensionItem::unset_raw(Extensible* container)
139 Extensible::ExtensibleStore::iterator i = container->extensions.find(this);
140 if (i == container->extensions.end())
142 void* rv = i->second;
143 container->extensions.erase(i);
147 void ExtensionItem::RegisterService()
149 if (!ServerInstance->Extensions.Register(this))
150 throw ModuleException("Extension already exists: " + name);
153 bool ExtensionManager::Register(ExtensionItem* item)
155 return types.insert(std::make_pair(item->name, item)).second;
158 void ExtensionManager::BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list)
160 ExtMap::iterator i = types.begin();
161 while (i != types.end())
163 ExtMap::iterator me = i++;
164 ExtensionItem* item = me->second;
165 if (item->creator == module)
167 list.push_back(item);
173 ExtensionItem* ExtensionManager::GetItem(const std::string& name)
175 ExtMap::iterator i = types.find(name);
176 if (i == types.end())
181 void Extensible::doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove)
183 for(std::vector<reference<ExtensionItem> >::const_iterator i = toRemove.begin(); i != toRemove.end(); ++i)
185 ExtensionItem* item = *i;
186 ExtensibleStore::iterator e = extensions.find(item);
187 if (e != extensions.end())
189 item->free(e->second);
195 Extensible::Extensible()
200 CullResult Extensible::cull()
204 return classbase::cull();
207 void Extensible::FreeAllExtItems()
209 for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
211 i->first->free(i->second);
216 Extensible::~Extensible()
218 if ((!extensions.empty() || !culled) && ServerInstance)
219 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "Extensible destructor called without cull @%p", (void*)this);
222 LocalExtItem::LocalExtItem(const std::string& Key, ExtensibleType exttype, Module* mod)
223 : ExtensionItem(Key, exttype, mod)
227 LocalExtItem::~LocalExtItem()
231 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
236 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
240 LocalStringExt::LocalStringExt(const std::string& Key, ExtensibleType exttype, Module* Owner)
241 : SimpleExtItem<std::string>(Key, exttype, Owner)
245 LocalStringExt::~LocalStringExt()
249 std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
251 if ((item) && (format != FORMAT_NETWORK))
252 return *static_cast<std::string*>(item);
256 void LocalStringExt::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
258 if (format != FORMAT_NETWORK)
259 set(container, value);
262 LocalIntExt::LocalIntExt(const std::string& Key, ExtensibleType exttype, Module* mod)
263 : LocalExtItem(Key, exttype, mod)
267 LocalIntExt::~LocalIntExt()
271 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
273 if (format == FORMAT_NETWORK)
275 return ConvToStr(reinterpret_cast<intptr_t>(item));
278 void LocalIntExt::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
280 if (format != FORMAT_NETWORK)
281 set(container, ConvToInt(value));
284 intptr_t LocalIntExt::get(const Extensible* container) const
286 return reinterpret_cast<intptr_t>(get_raw(container));
289 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
292 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
294 return reinterpret_cast<intptr_t>(unset_raw(container));
297 void LocalIntExt::free(void*)
301 StringExtItem::StringExtItem(const std::string& Key, ExtensibleType exttype, Module* mod)
302 : ExtensionItem(Key, exttype, mod)
306 StringExtItem::~StringExtItem()
310 std::string* StringExtItem::get(const Extensible* container) const
312 return static_cast<std::string*>(get_raw(container));
315 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
317 return item ? *static_cast<std::string*>(item) : "";
320 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
325 set(container, value);
328 void StringExtItem::set(Extensible* container, const std::string& value)
330 void* old = set_raw(container, new std::string(value));
331 delete static_cast<std::string*>(old);
334 void StringExtItem::unset(Extensible* container)
336 void* old = unset_raw(container);
337 delete static_cast<std::string*>(old);
340 void StringExtItem::free(void* item)
342 delete static_cast<std::string*>(item);
345 ModuleException::ModuleException(const std::string &message, Module* who)
346 : CoreException(message, who ? who->ModuleSourceFile : "A Module")