2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2018-2019, 2021 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2012, 2014-2015 Attila Molnar <attilamolnar@hush.com>
6 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
8 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
9 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10 * Copyright (C) 2006 Oliver Lupton <om@inspircd.org>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #ifdef INSPIRCD_ENABLE_RTTI
33 classbase::classbase()
35 #ifdef INSPIRCD_ENABLE_RTTI
37 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::+%s @%p", typeid(*this).name(), (void*)this);
41 CullResult classbase::cull()
43 #ifdef INSPIRCD_ENABLE_RTTI
45 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::-%s @%p", typeid(*this).name(), (void*)this);
50 classbase::~classbase()
52 #ifdef INSPIRCD_ENABLE_RTTI
54 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::~%s @%p", typeid(*this).name(), (void*)this);
58 CullResult::CullResult()
62 // This trick detects heap allocations of refcountbase objects
63 static void* last_heap = NULL;
65 void* refcountbase::operator new(size_t size)
67 last_heap = ::operator new(size);
71 void refcountbase::operator delete(void* obj)
75 ::operator delete(obj);
78 refcountbase::refcountbase() : refcount(0)
80 if (this != last_heap)
81 throw CoreException("Reference allocate on the stack!");
84 refcountbase::~refcountbase()
86 if (refcount && ServerInstance)
87 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "refcountbase::~ @%p with refcount %d",
88 (void*)this, refcount);
91 usecountbase::~usecountbase()
93 if (usecount && ServerInstance)
94 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "usecountbase::~ @%p with refcount %d",
95 (void*)this, usecount);
98 ServiceProvider::~ServiceProvider()
102 void ServiceProvider::RegisterService()
106 ExtensionItem::ExtensionItem(const std::string& Key, ExtensibleType exttype, Module* mod)
107 : ServiceProvider(mod, Key, SERVICE_METADATA)
112 ExtensionItem::~ExtensionItem()
116 void* ExtensionItem::get_raw(const Extensible* container) const
118 Extensible::ExtensibleStore::const_iterator i =
119 container->extensions.find(const_cast<ExtensionItem*>(this));
120 if (i == container->extensions.end())
125 void* ExtensionItem::set_raw(Extensible* container, void* value)
127 std::pair<Extensible::ExtensibleStore::iterator,bool> rv =
128 container->extensions.insert(std::make_pair(this, value));
135 void* old = rv.first->second;
136 rv.first->second = value;
141 void* ExtensionItem::unset_raw(Extensible* container)
143 Extensible::ExtensibleStore::iterator i = container->extensions.find(this);
144 if (i == container->extensions.end())
146 void* rv = i->second;
147 container->extensions.erase(i);
151 void ExtensionItem::RegisterService()
153 if (!ServerInstance->Extensions.Register(this))
154 throw ModuleException("Extension already exists: " + name);
157 bool ExtensionManager::Register(ExtensionItem* item)
159 return types.insert(std::make_pair(item->name, item)).second;
162 void ExtensionManager::BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list)
164 ExtMap::iterator i = types.begin();
165 while (i != types.end())
167 ExtMap::iterator me = i++;
168 ExtensionItem* item = me->second;
169 if (item->creator == module)
171 list.push_back(item);
177 ExtensionItem* ExtensionManager::GetItem(const std::string& name)
179 ExtMap::iterator i = types.find(name);
180 if (i == types.end())
185 void Extensible::doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove)
187 for(std::vector<reference<ExtensionItem> >::const_iterator i = toRemove.begin(); i != toRemove.end(); ++i)
189 ExtensionItem* item = *i;
190 ExtensibleStore::iterator e = extensions.find(item);
191 if (e != extensions.end())
193 item->free(this, e->second);
199 Extensible::Extensible()
204 CullResult Extensible::cull()
208 return classbase::cull();
211 void Extensible::FreeAllExtItems()
213 for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
215 i->first->free(this, i->second);
220 Extensible::~Extensible()
222 if ((!extensions.empty() || !culled) && ServerInstance)
223 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "Extensible destructor called without cull @%p", (void*)this);
226 void ExtensionItem::FromInternal(Extensible* container, const std::string& value)
228 FromNetwork(container, value);
231 void ExtensionItem::FromNetwork(Extensible* container, const std::string& value)
235 std::string ExtensionItem::ToHuman(const Extensible* container, void* item) const
237 // Try to use the network form by default.
238 std::string ret = ToNetwork(container, item);
240 // If there's no network form then fall back to the internal form.
242 ret = ToInternal(container, item);
247 std::string ExtensionItem::ToInternal(const Extensible* container, void* item) const
249 return ToNetwork(container, item);
252 std::string ExtensionItem::ToNetwork(const Extensible* container, void* item) const
254 return std::string();
257 std::string ExtensionItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
259 // Wrap the deprecated API with the new API.
263 return ToHuman(container, item);
264 case FORMAT_INTERNAL:
266 return ToInternal(container, item);
268 return ToNetwork(container, item);
274 void ExtensionItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
276 // Wrap the deprecated API with the new API.
281 case FORMAT_INTERNAL:
283 FromInternal(container, value);
286 FromNetwork(container, value);
291 LocalStringExt::LocalStringExt(const std::string& Key, ExtensibleType exttype, Module* Owner)
292 : SimpleExtItem<std::string>(Key, exttype, Owner)
296 LocalStringExt::~LocalStringExt()
300 std::string LocalStringExt::ToInternal(const Extensible* container, void* item) const
302 return item ? *static_cast<std::string*>(item) : std::string();
305 void LocalStringExt::FromInternal(Extensible* container, const std::string& value)
307 set(container, value);
310 LocalIntExt::LocalIntExt(const std::string& Key, ExtensibleType exttype, Module* mod)
311 : ExtensionItem(Key, exttype, mod)
315 LocalIntExt::~LocalIntExt()
319 std::string LocalIntExt::ToInternal(const Extensible* container, void* item) const
321 return ConvToStr(reinterpret_cast<intptr_t>(item));
324 void LocalIntExt::FromInternal(Extensible* container, const std::string& value)
326 set(container, ConvToNum<intptr_t>(value));
329 intptr_t LocalIntExt::get(const Extensible* container) const
331 return reinterpret_cast<intptr_t>(get_raw(container));
334 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
337 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
339 return reinterpret_cast<intptr_t>(unset_raw(container));
342 void LocalIntExt::free(Extensible* container, void* item)
346 StringExtItem::StringExtItem(const std::string& Key, ExtensibleType exttype, Module* mod)
347 : ExtensionItem(Key, exttype, mod)
351 StringExtItem::~StringExtItem()
355 std::string* StringExtItem::get(const Extensible* container) const
357 return static_cast<std::string*>(get_raw(container));
360 std::string StringExtItem::ToNetwork(const Extensible* container, void* item) const
362 return item ? *static_cast<std::string*>(item) : std::string();
365 void StringExtItem::FromNetwork(Extensible* container, const std::string& value)
370 set(container, value);
373 void StringExtItem::set(Extensible* container, const std::string& value)
375 void* old = set_raw(container, new std::string(value));
376 free(container, old);
379 void StringExtItem::unset(Extensible* container)
381 void* old = unset_raw(container);
382 free(container, old);
385 void StringExtItem::free(Extensible* container, void* item)
387 delete static_cast<std::string*>(item);
390 ModuleException::ModuleException(const std::string &message, Module* who)
391 : CoreException(message, who ? who->ModuleSourceFile : "A Module")