]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/base.cpp
c0bc40811a6ee0fd32a505471ee8e8f6cefe97a3
[user/henk/code/inspircd.git] / src / base.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd_config.h"
15 #include "base.h"
16 #include <time.h>
17 #include "inspircd.h"
18 #include <typeinfo>
19
20 classbase::classbase()
21 {
22         if (ServerInstance && ServerInstance->Logs)
23                 ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::+ @%p", (void*)this);
24 }
25
26 CullResult classbase::cull()
27 {
28         if (ServerInstance && ServerInstance->Logs)
29                 ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::-%s @%p",
30                         typeid(*this).name(), (void*)this);
31         return CullResult();
32 }
33
34 classbase::~classbase()
35 {
36         if (ServerInstance && ServerInstance->Logs)
37                 ServerInstance->Logs->Log("CULLLIST", DEBUG, "classbase::~ @%p", (void*)this);
38 }
39
40 CullResult::CullResult()
41 {
42 }
43
44 refcountbase::refcountbase() : refcount(0)
45 {
46 }
47
48 refcountbase::~refcountbase()
49 {
50         if (refcount && ServerInstance && ServerInstance->Logs)
51                 ServerInstance->Logs->Log("CULLLIST", DEBUG, "refcountbase::~ @%p with refcount %d",
52                         (void*)this, refcount);
53 }
54
55 ExtensionItem::ExtensionItem(const std::string& Key, Module* mod) : key(Key), owner(mod)
56 {
57 }
58
59 ExtensionItem::~ExtensionItem()
60 {
61 }
62
63 void* ExtensionItem::get_raw(const Extensible* container) const
64 {
65         Extensible::ExtensibleStore::const_iterator i =
66                 container->extensions.find(const_cast<ExtensionItem*>(this));
67         if (i == container->extensions.end())
68                 return NULL;
69         return i->second;
70 }
71
72 void* ExtensionItem::set_raw(Extensible* container, void* value)
73 {
74         std::pair<Extensible::ExtensibleStore::iterator,bool> rv =
75                 container->extensions.insert(std::make_pair(this, value));
76         if (rv.second)
77         {
78                 return NULL;
79         }
80         else
81         {
82                 void* old = rv.first->second;
83                 rv.first->second = value;
84                 return old;
85         }
86 }
87
88 void* ExtensionItem::unset_raw(Extensible* container)
89 {
90         Extensible::ExtensibleStore::iterator i = container->extensions.find(this);
91         if (i == container->extensions.end())
92                 return NULL;
93         void* rv = i->second;
94         container->extensions.erase(i);
95         return rv;
96 }
97
98 void ExtensionManager::Register(ExtensionItem* item)
99 {
100         types.insert(std::make_pair(item->key, item));
101 }
102
103 void ExtensionManager::BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list)
104 {
105         std::map<std::string, reference<ExtensionItem> >::iterator i = types.begin();
106         while (i != types.end())
107         {
108                 std::map<std::string, reference<ExtensionItem> >::iterator me = i++;
109                 ExtensionItem* item = me->second;
110                 if (item->owner == module)
111                 {
112                         list.push_back(item);
113                         types.erase(me);
114                 }
115         }
116 }
117
118 ExtensionItem* ExtensionManager::GetItem(const std::string& name)
119 {
120         std::map<std::string, reference<ExtensionItem> >::iterator i = types.find(name);
121         if (i == types.end())
122                 return NULL;
123         return i->second;
124 }
125
126 void Extensible::doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove)
127 {
128         for(std::vector<reference<ExtensionItem> >::const_iterator i = toRemove.begin(); i != toRemove.end(); ++i)
129         {
130                 ExtensionItem* item = *i;
131                 ExtensibleStore::iterator e = extensions.find(item);
132                 if (e != extensions.end())
133                 {
134                         item->free(e->second);
135                         extensions.erase(e);
136                 }
137         }
138 }
139
140 static struct DummyExtensionItem : LocalExtItem
141 {
142         DummyExtensionItem() : LocalExtItem("", NULL) {}
143         void free(void*) {}
144 } dummy;
145
146 Extensible::Extensible()
147 {
148         extensions[&dummy] = NULL;
149 }
150
151 CullResult Extensible::cull()
152 {
153         for(ExtensibleStore::iterator i = extensions.begin(); i != extensions.end(); ++i)
154         {
155                 i->first->free(i->second);
156         }
157         extensions.clear();
158         return classbase::cull();
159 }
160
161 Extensible::~Extensible()
162 {
163         if (!extensions.empty() && ServerInstance && ServerInstance->Logs)
164                 ServerInstance->Logs->Log("CULLLIST", DEBUG,
165                         "Extensible destructor called without cull @%p", (void*)this);
166 }
167
168 LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
169 {
170 }
171
172 LocalExtItem::~LocalExtItem()
173 {
174 }
175
176 std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
177 {
178         return "";
179 }
180
181 void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
182 {
183 }
184
185 LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner)
186         : SimpleExtItem<std::string>(Key, Owner) { }
187
188 LocalStringExt::~LocalStringExt()
189 {
190 }
191
192 std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
193 {
194         if (item && format == FORMAT_USER)
195                 return *static_cast<std::string*>(item);
196         return "";
197 }
198
199 LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod)
200 {
201 }
202
203 LocalIntExt::~LocalIntExt()
204 {
205 }
206
207 std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item) const
208 {
209         if (format != FORMAT_USER)
210                 return "";
211         return ConvToStr(reinterpret_cast<intptr_t>(item));
212 }
213
214 intptr_t LocalIntExt::get(const Extensible* container) const
215 {
216         return reinterpret_cast<intptr_t>(get_raw(container));
217 }
218
219 intptr_t LocalIntExt::set(Extensible* container, intptr_t value)
220 {
221         if (value)
222                 return reinterpret_cast<intptr_t>(set_raw(container, reinterpret_cast<void*>(value)));
223         else
224                 return reinterpret_cast<intptr_t>(unset_raw(container));
225 }
226
227 void LocalIntExt::free(void*)
228 {
229 }
230
231 StringExtItem::StringExtItem(const std::string& Key, Module* mod) : ExtensionItem(Key, mod)
232 {
233 }
234
235 StringExtItem::~StringExtItem()
236 {
237 }
238
239 std::string* StringExtItem::get(const Extensible* container) const
240 {
241         return static_cast<std::string*>(get_raw(container));
242 }
243
244 std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
245 {
246         return item ? *static_cast<std::string*>(item) : "";
247 }
248
249 void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
250 {
251         if (value.empty())
252                 unset(container);
253         else
254                 set(container, value);
255 }
256
257 void StringExtItem::set(Extensible* container, const std::string& value)
258 {
259         void* old = set_raw(container, new std::string(value));
260         delete static_cast<std::string*>(old);
261 }
262
263 void StringExtItem::unset(Extensible* container)
264 {
265         void* old = unset_raw(container);
266         delete static_cast<std::string*>(old);
267 }
268
269 void StringExtItem::free(void* item)
270 {
271         delete static_cast<std::string*>(item);
272 }
273
274 ModuleException::ModuleException(const std::string &message, Module* who)
275         : CoreException(message, who ? who->ModuleSourceFile : "A Module")
276 {
277 }
278
279 ModuleRef::ModuleRef(Module* v) : value(v)
280 {
281         if (value) inc(value);
282 }
283
284 ModuleRef::~ModuleRef()
285 {
286         if (value) dec(value);
287 }