]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/base.cpp
Free StringExtItem and SimpleExtItem values correctly.
[user/henk/code/inspircd.git] / src / base.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
8  *
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.
12  *
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
16  * details.
17  *
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/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "base.h"
25 #include <time.h>
26 #ifdef INSPIRCD_ENABLE_RTTI
27 #include <typeinfo>
28 #endif
29
30 classbase::classbase()
31 {
32         if (ServerInstance)
33                 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::+ @%p", (void*)this);
34 }
35
36 CullResult classbase::cull()
37 {
38         if (ServerInstance)
39 #ifdef INSPIRCD_ENABLE_RTTI
40                 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::-%s @%p",
41                         typeid(*this).name(), (void*)this);
42 #else
43                 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::- @%p", (void*)this);
44 #endif
45         return CullResult();
46 }
47
48 classbase::~classbase()
49 {
50         if (ServerInstance)
51                 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "classbase::~ @%p", (void*)this);
52 }
53
54 CullResult::CullResult()
55 {
56 }
57
58 // This trick detects heap allocations of refcountbase objects
59 static void* last_heap = NULL;
60
61 void* refcountbase::operator new(size_t size)
62 {
63         last_heap = ::operator new(size);
64         return last_heap;
65 }
66
67 void refcountbase::operator delete(void* obj)
68 {
69         if (last_heap == obj)
70                 last_heap = NULL;
71         ::operator delete(obj);
72 }
73
74 refcountbase::refcountbase() : refcount(0)
75 {
76         if (this != last_heap)
77                 throw CoreException("Reference allocate on the stack!");
78 }
79
80 refcountbase::~refcountbase()
81 {
82         if (refcount && ServerInstance)
83                 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "refcountbase::~ @%p with refcount %d",
84                         (void*)this, refcount);
85 }
86
87 usecountbase::~usecountbase()
88 {
89         if (usecount && ServerInstance)
90                 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "usecountbase::~ @%p with refcount %d",
91                         (void*)this, usecount);
92 }
93
94 ServiceProvider::~ServiceProvider()
95 {
96 }
97
98 void ServiceProvider::RegisterService()
99 {
100 }
101
102 ExtensionItem::ExtensionItem(const std::string& Key, ExtensibleType exttype, Module* mod)
103         : ServiceProvider(mod, Key, SERVICE_METADATA)
104         , type(exttype)
105 {
106 }
107
108 ExtensionItem::~ExtensionItem()
109 {
110 }
111
112 void* ExtensionItem::get_raw(const Extensible* container) const
113 {
114         Extensible::ExtensibleStore::const_iterator i =
115                 container->extensions.find(const_cast<ExtensionItem*>(this));
116         if (i == container->extensions.end())
117                 return NULL;
118         return i->second;
119 }
120
121 void* ExtensionItem::set_raw(Extensible* container, void* value)
122 {
123         std::pair<Extensible::ExtensibleStore::iterator,bool> rv =
124                 container->extensions.insert(std::make_pair(this, value));
125         if (rv.second)
126         {
127                 return NULL;
128         }
129         else
130         {
131                 void* old = rv.first->second;
132                 rv.first->second = value;
133                 return old;
134         }
135 }
136
137 void* ExtensionItem::unset_raw(Extensible* container)
138 {
139         Extensible::ExtensibleStore::iterator i = container->extensions.find(this);
140         if (i == container->extensions.end())
141                 return NULL;
142         void* rv = i->second;
143         container->extensions.erase(i);
144         return rv;
145 }
146
147 void ExtensionItem::RegisterService()
148 {
149         if (!ServerInstance->Extensions.Register(this))
150                 throw ModuleException("Extension already exists: " + name);
151 }
152
153 bool ExtensionManager::Register(ExtensionItem* item)
154 {
155         return types.insert(std::make_pair(item->name, item)).second;
156 }
157
158 void ExtensionManager::BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list)
159 {
160         ExtMap::iterator i = types.begin();
161         while (i != types.end())
162         {
163                 ExtMap::iterator me = i++;
164                 ExtensionItem* item = me->second;
165                 if (item->creator == module)
166                 {
167                         list.push_back(item);
168                         types.erase(me);
169                 }
170         }
171 }
172
173 ExtensionItem* ExtensionManager::GetItem(const std::string& name)
174 {
175         ExtMap::iterator i = types.find(name);
176         if (i == types.end())
177                 return NULL;
178         return i->second;
179 }
180
181 void Extensible::doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove)
182 {
183         for(std::vector<reference<ExtensionItem> >::const_iterator i = toRemove.begin(); i != toRemove.end(); ++i)
184         {
185                 ExtensionItem* item = *i;
186                 ExtensibleStore::iterator e = extensions.find(item);
187                 if (e != extensions.end())
188                 {
189                         item->free(this, e->second);
190                         extensions.erase(e);
191                 }
192         }
193 }
194
195 Extensible::Extensible()
196         : culled(false)
197 {
198 }
199
200 CullResult Extensible::cull()
201 {
202         FreeAllExtItems();
203         culled = true;
204         return classbase::cull();
205 }
206
207 void Extensible::FreeAllExtItems()
208 {
209         for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
210         {
211                 i->first->free(this, i->second);
212         }
213         extensions.clear();
214 }
215
216 Extensible::~Extensible()
217 {
218         if ((!extensions.empty() || !culled) && ServerInstance)
219                 ServerInstance->Logs->Log("CULLLIST", LOG_DEBUG, "Extensible destructor called without cull @%p", (void*)this);
220 }
221
222 void ExtensionItem::FromInternal(Extensible* container, const std::string& value)
223 {
224         FromNetwork(container, value);
225 }
226
227 void ExtensionItem::FromNetwork(Extensible* container, const std::string& value)
228 {
229 }
230
231 std::string ExtensionItem::ToHuman(const Extensible* container, void* item) const
232 {
233         // Try to use the network form by default.
234         std::string ret = ToNetwork(container, item);
235
236         // If there's no network form then fall back to the internal form.
237         if (ret.empty())
238                 ret = ToInternal(container, item);
239
240         return ret;
241 }
242
243 std::string ExtensionItem::ToInternal(const Extensible* container, void* item) const
244 {
245         return ToNetwork(container, item);
246 }
247
248 std::string ExtensionItem::ToNetwork(const Extensible* container, void* item) const
249 {
250         return std::string();
251 }
252
253 std::string ExtensionItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
254 {
255         // Wrap the deprecated API with the new API.
256         switch (format)
257         {
258                 case FORMAT_USER:
259                         return ToHuman(container, item);
260                 case FORMAT_INTERNAL:
261                 case FORMAT_PERSIST:
262                         return ToInternal(container, item);
263                 case FORMAT_NETWORK:
264                         return ToNetwork(container, item);
265         }
266         return "";
267 }
268
269
270 void ExtensionItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
271 {
272         // Wrap the deprecated API with the new API.
273         switch (format)
274         {
275                 case FORMAT_USER:
276                         break;
277                 case FORMAT_INTERNAL:
278                 case FORMAT_PERSIST:
279                         FromInternal(container, value);
280                         break;
281                 case FORMAT_NETWORK:
282                         FromNetwork(container, value);
283                         break;
284         }
285 }
286
287 LocalStringExt::LocalStringExt(const std::string& Key, ExtensibleType exttype, Module* Owner)
288         : SimpleExtItem<std::string>(Key, exttype, Owner)
289 {
290 }
291
292 LocalStringExt::~LocalStringExt()
293 {
294 }
295
296 std::string LocalStringExt::ToInternal(const Extensible* container, void* item) const
297 {
298         return item ? *static_cast<std::string*>(item) : std::string(); 
299 }
300
301 void LocalStringExt::FromInternal(Extensible* container, const std::string& value)
302 {
303         set(container, value);
304 }
305
306 LocalIntExt::LocalIntExt(const std::string& Key, ExtensibleType exttype, Module* mod)
307         : ExtensionItem(Key, exttype, mod)
308 {
309 }
310
311 LocalIntExt::~LocalIntExt()
312 {
313 }
314
315 std::string LocalIntExt::ToInternal(const Extensible* container, void* item) const
316 {
317         return ConvToStr(reinterpret_cast<intptr_t>(item));
318 }
319
320 void LocalIntExt::FromInternal(Extensible* container, const std::string& value)
321 {
322         set(container, ConvToNum<intptr_t>(value));
323 }
324
325 intptr_t LocalIntExt::get(const Extensible* container) const
326 {
327         return reinterpret_cast<intptr_t>(get_raw(container));
328 }
329
330 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
331 {
332         if (value)
333                 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
334         else
335                 return reinterpret_cast<intptr_t>(unset_raw(container));
336 }
337
338 void LocalIntExt::free(Extensible* container, void* item)
339 {
340 }
341
342 StringExtItem::StringExtItem(const std::string& Key, ExtensibleType exttype, Module* mod)
343         : ExtensionItem(Key, exttype, mod)
344 {
345 }
346
347 StringExtItem::~StringExtItem()
348 {
349 }
350
351 std::string* StringExtItem::get(const Extensible* container) const
352 {
353         return static_cast<std::string*>(get_raw(container));
354 }
355
356 std::string StringExtItem::ToNetwork(const Extensible* container, void* item) const
357 {
358         return item ? *static_cast<std::string*>(item) : std::string();
359 }
360
361 void StringExtItem::FromNetwork(Extensible* container, const std::string& value)
362 {
363         if (value.empty())
364                 unset(container);
365         else
366                 set(container, value);
367 }
368
369 void StringExtItem::set(Extensible* container, const std::string& value)
370 {
371         void* old = set_raw(container, new std::string(value));
372         free(container, old);
373 }
374
375 void StringExtItem::unset(Extensible* container)
376 {
377         void* old = unset_raw(container);
378         free(container, old);
379 }
380
381 void StringExtItem::free(Extensible* container, void* item)
382 {
383         delete static_cast<std::string*>(item);
384 }
385
386 ModuleException::ModuleException(const std::string &message, Module* who)
387         : CoreException(message, who ? who->ModuleSourceFile : "A Module")
388 {
389 }