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()
32 #ifdef INSPIRCD_ENABLE_RTTI
34 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::+%s @%p", typeid(*this).name(), (void*)this);
38 CullResult classbase::cull()
40 #ifdef INSPIRCD_ENABLE_RTTI
42 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::-%s @%p", typeid(*this).name(), (void*)this);
47 classbase::~classbase()
49 #ifdef INSPIRCD_ENABLE_RTTI
51 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::~%s @%p", typeid(*this).name(), (void*)this);
55 CullResult::CullResult()
59 // This trick detects heap allocations of refcountbase objects
60 static void* last_heap = NULL;
62 void* refcountbase::operator new(size_t size)
64 last_heap = ::operator new(size);
68 void refcountbase::operator delete(void* obj)
72 ::operator delete(obj);
75 refcountbase::refcountbase() : refcount(0)
77 if (this != last_heap)
78 throw CoreException("Reference allocate on the stack!");
81 refcountbase::~refcountbase()
83 if (refcount && ServerInstance)
84 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "refcountbase::~ @%p with refcount %d",
85 (void*)this, refcount);
88 usecountbase::~usecountbase()
90 if (usecount && ServerInstance)
91 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "usecountbase::~ @%p with refcount %d",
92 (void*)this, usecount);
95 ServiceProvider::~ServiceProvider()
99 void ServiceProvider::RegisterService()
103 ExtensionItem::ExtensionItem(const std::string& Key, ExtensibleType exttype, Module* mod)
104 : ServiceProvider(mod, Key, SERVICE_METADATA)
109 ExtensionItem::~ExtensionItem()
113 void* ExtensionItem::get_raw(const Extensible* container) const
115 Extensible::ExtensibleStore::const_iterator i =
116 container->extensions.find(const_cast<ExtensionItem*>(this));
117 if (i == container->extensions.end())
122 void* ExtensionItem::set_raw(Extensible* container, void* value)
124 std::pair<Extensible::ExtensibleStore::iterator,bool> rv =
125 container->extensions.insert(std::make_pair(this, value));
132 void* old = rv.first->second;
133 rv.first->second = value;
138 void* ExtensionItem::unset_raw(Extensible* container)
140 Extensible::ExtensibleStore::iterator i = container->extensions.find(this);
141 if (i == container->extensions.end())
143 void* rv = i->second;
144 container->extensions.erase(i);
148 void ExtensionItem::RegisterService()
150 if (!ServerInstance->Extensions.Register(this))
151 throw ModuleException("Extension already exists: " + name);
154 bool ExtensionManager::Register(ExtensionItem* item)
156 return types.insert(std::make_pair(item->name, item)).second;
159 void ExtensionManager::BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list)
161 ExtMap::iterator i = types.begin();
162 while (i != types.end())
164 ExtMap::iterator me = i++;
165 ExtensionItem* item = me->second;
166 if (item->creator == module)
168 list.push_back(item);
174 ExtensionItem* ExtensionManager::GetItem(const std::string& name)
176 ExtMap::iterator i = types.find(name);
177 if (i == types.end())
182 void Extensible::doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove)
184 for(std::vector<reference<ExtensionItem> >::const_iterator i = toRemove.begin(); i != toRemove.end(); ++i)
186 ExtensionItem* item = *i;
187 ExtensibleStore::iterator e = extensions.find(item);
188 if (e != extensions.end())
190 item->free(this, e->second);
196 Extensible::Extensible()
201 CullResult Extensible::cull()
205 return classbase::cull();
208 void Extensible::FreeAllExtItems()
210 for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
212 i->first->free(this, i->second);
217 Extensible::~Extensible()
219 if ((!extensions.empty() || !culled) && ServerInstance)
220 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "Extensible destructor called without cull @%p", (void*)this);
223 void ExtensionItem::FromInternal(Extensible* container, const std::string& value)
225 FromNetwork(container, value);
228 void ExtensionItem::FromNetwork(Extensible* container, const std::string& value)
232 std::string ExtensionItem::ToHuman(const Extensible* container, void* item) const
234 // Try to use the network form by default.
235 std::string ret = ToNetwork(container, item);
237 // If there's no network form then fall back to the internal form.
239 ret = ToInternal(container, item);
244 std::string ExtensionItem::ToInternal(const Extensible* container, void* item) const
246 return ToNetwork(container, item);
249 std::string ExtensionItem::ToNetwork(const Extensible* container, void* item) const
251 return std::string();
254 std::string ExtensionItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
256 // Wrap the deprecated API with the new API.
260 return ToHuman(container, item);
261 case FORMAT_INTERNAL:
263 return ToInternal(container, item);
265 return ToNetwork(container, item);
271 void ExtensionItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
273 // Wrap the deprecated API with the new API.
278 case FORMAT_INTERNAL:
280 FromInternal(container, value);
283 FromNetwork(container, value);
288 LocalStringExt::LocalStringExt(const std::string& Key, ExtensibleType exttype, Module* Owner)
289 : SimpleExtItem<std::string>(Key, exttype, Owner)
293 LocalStringExt::~LocalStringExt()
297 std::string LocalStringExt::ToInternal(const Extensible* container, void* item) const
299 return item ? *static_cast<std::string*>(item) : std::string();
302 void LocalStringExt::FromInternal(Extensible* container, const std::string& value)
304 set(container, value);
307 LocalIntExt::LocalIntExt(const std::string& Key, ExtensibleType exttype, Module* mod)
308 : ExtensionItem(Key, exttype, mod)
312 LocalIntExt::~LocalIntExt()
316 std::string LocalIntExt::ToInternal(const Extensible* container, void* item) const
318 return ConvToStr(reinterpret_cast<intptr_t>(item));
321 void LocalIntExt::FromInternal(Extensible* container, const std::string& value)
323 set(container, ConvToNum<intptr_t>(value));
326 intptr_t LocalIntExt::get(const Extensible* container) const
328 return reinterpret_cast<intptr_t>(get_raw(container));
331 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
334 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
336 return reinterpret_cast<intptr_t>(unset_raw(container));
339 void LocalIntExt::free(Extensible* container, void* item)
343 StringExtItem::StringExtItem(const std::string& Key, ExtensibleType exttype, Module* mod)
344 : ExtensionItem(Key, exttype, mod)
348 StringExtItem::~StringExtItem()
352 std::string* StringExtItem::get(const Extensible* container) const
354 return static_cast<std::string*>(get_raw(container));
357 std::string StringExtItem::ToNetwork(const Extensible* container, void* item) const
359 return item ? *static_cast<std::string*>(item) : std::string();
362 void StringExtItem::FromNetwork(Extensible* container, const std::string& value)
367 set(container, value);
370 void StringExtItem::set(Extensible* container, const std::string& value)
372 void* old = set_raw(container, new std::string(value));
373 free(container, old);
376 void StringExtItem::unset(Extensible* container)
378 void* old = unset_raw(container);
379 free(container, old);
382 void StringExtItem::free(Extensible* container, void* item)
384 delete static_cast<std::string*>(item);
387 ModuleException::ModuleException(const std::string &message, Module* who)
388 : CoreException(message, who ? who->ModuleSourceFile : "A Module")