]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/base.h
Merge pull request #320 from ChrisTX/insp20+cleanupwin
[user/henk/code/inspircd.git] / include / base.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2006-2007 Oliver Lupton <oliverlupton@gmail.com>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2003-2005, 2007 Craig Edwards <craigedwards@brainbox.cc>
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 #ifndef BASE_H
24 #define BASE_H
25
26 #include <map>
27 #include <deque>
28 #include <string>
29
30 /** Dummy class to help enforce culls being parent-called up to classbase */
31 class CullResult
32 {
33         CullResult();
34         friend class classbase;
35 };
36
37 /** The base class for all inspircd classes with a well-defined lifetime.
38  * Classes that inherit from this may be destroyed through GlobalCulls,
39  * and may rely on cull() being called prior to their deletion.
40  */
41 class CoreExport classbase
42 {
43  public:
44         classbase();
45
46         /**
47          * Called just prior to destruction via cull list.
48          */
49         virtual CullResult cull();
50         virtual ~classbase();
51  private:
52         // uncopyable
53         classbase(const classbase&);
54         void operator=(const classbase&);
55 };
56
57 /** The base class for inspircd classes that provide a wrapping interface, and
58  * should only exist while being used. Prevents heap allocation.
59  */
60 class CoreExport interfacebase
61 {
62  public:
63         interfacebase() {}
64         static inline void* operator new(size_t, void* m) { return m; }
65  private:
66         interfacebase(const interfacebase&);
67         void operator=(const interfacebase&);
68         static void* operator new(size_t);
69         static void operator delete(void*);
70 };
71
72 /** The base class for inspircd classes that support reference counting.
73  * Any objects that do not have a well-defined lifetime should inherit from
74  * this, and should be assigned to a reference<type> object to establish their
75  * lifetime.
76  *
77  * Reference objects should not hold circular references back to themselves,
78  * even indirectly; this will cause a memory leak because the count will never
79  * drop to zero.
80  *
81  * Using a normal pointer for the object is recommended if you can assure that
82  * at least one reference<> will remain as long as that pointer is used; this
83  * will avoid the slight overhead of changing the reference count.
84  */
85 class CoreExport refcountbase
86 {
87         mutable unsigned int refcount;
88  public:
89         refcountbase();
90         virtual ~refcountbase();
91         inline unsigned int GetReferenceCount() const { return refcount; }
92         static inline void* operator new(size_t, void* m) { return m; }
93         static void* operator new(size_t);
94         static void operator delete(void*);
95         inline void refcount_inc() const { refcount++; }
96         inline bool refcount_dec() const { refcount--; return !refcount; }
97  private:
98         // uncopyable
99         refcountbase(const refcountbase&);
100         void operator=(const refcountbase&);
101 };
102
103 /** Base class for use count tracking. Uses reference<>, but does not
104  * cause object deletion when the last user is removed.
105  *
106  * Safe for use as a second parent class; will not add a second vtable.
107  */
108 class CoreExport usecountbase
109 {
110         mutable unsigned int usecount;
111  public:
112         usecountbase() : usecount(0) { }
113         ~usecountbase();
114         inline unsigned int GetUseCount() const { return usecount; }
115         inline void refcount_inc() const { usecount++; }
116         inline bool refcount_dec() const { usecount--; return false; }
117  private:
118         // uncopyable
119         usecountbase(const usecountbase&);
120         void operator=(const usecountbase&);
121 };
122
123 template <typename T>
124 class CoreExport reference
125 {
126         T* value;
127  public:
128         reference() : value(0) { }
129         reference(T* v) : value(v) { if (value) value->refcount_inc(); }
130         reference(const reference<T>& v) : value(v.value) { if (value) value->refcount_inc(); }
131         reference<T>& operator=(const reference<T>& other)
132         {
133                 if (other.value)
134                         other.value->refcount_inc();
135                 this->reference::~reference();
136                 value = other.value;
137                 return *this;
138         }
139
140         ~reference()
141         {
142                 if (value && value->refcount_dec())
143                         delete value;
144         }
145
146         inline reference<T>& operator=(T* other)
147         {
148                 if (value != other)
149                 {
150                         if (value && value->refcount_dec())
151                                 delete value;
152                         value = other;
153                         if (value)
154                                 value->refcount_inc();
155                 }
156
157                 return *this;
158         }
159
160         inline operator bool() const { return (value != NULL); }
161         inline operator T*() const { return value; }
162         inline T* operator->() const { return value; }
163         inline T& operator*() const { return *value; }
164         inline bool operator<(const reference<T>& other) const { return value < other.value; }
165         inline bool operator>(const reference<T>& other) const { return value > other.value; }
166         static inline void* operator new(size_t, void* m) { return m; }
167  private:
168 #ifndef _WIN32
169         static void* operator new(size_t);
170         static void operator delete(void*);
171 #endif
172 };
173
174 /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
175  * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
176  * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
177  * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
178  * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
179  */
180 class CoreExport CoreException : public std::exception
181 {
182  public:
183         /** Holds the error message to be displayed
184          */
185         const std::string err;
186         /** Source of the exception
187          */
188         const std::string source;
189         /** Default constructor, just uses the error mesage 'Core threw an exception'.
190          */
191         CoreException() : err("Core threw an exception"), source("The core") {}
192         /** This constructor can be used to specify an error message before throwing.
193          */
194         CoreException(const std::string &message) : err(message), source("The core") {}
195         /** This constructor can be used to specify an error message before throwing,
196          * and to specify the source of the exception.
197          */
198         CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
199         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
200          * Actually no, it does nothing. Never mind.
201          * @throws Nothing!
202          */
203         virtual ~CoreException() throw() {};
204         /** Returns the reason for the exception.
205          * The module should probably put something informative here as the user will see this upon failure.
206          */
207         virtual const char* GetReason()
208         {
209                 return err.c_str();
210         }
211
212         virtual const char* GetSource()
213         {
214                 return source.c_str();
215         }
216 };
217
218 class Module;
219 class CoreExport ModuleException : public CoreException
220 {
221  public:
222         /** This constructor can be used to specify an error message before throwing.
223          */
224         ModuleException(const std::string &message, Module* me = NULL);
225 };
226
227 typedef const reference<Module> ModuleRef;
228
229 enum ServiceType {
230         /** is a Command */
231         SERVICE_COMMAND,
232         /** is a ModeHandler */
233         SERVICE_MODE,
234         /** is a metadata descriptor */
235         SERVICE_METADATA,
236         /** is a data processing provider (MD5, SQL) */
237         SERVICE_DATA,
238         /** is an I/O hook provider (SSL) */
239         SERVICE_IOHOOK
240 };
241
242 /** A structure defining something that a module can provide */
243 class CoreExport ServiceProvider : public classbase
244 {
245  public:
246         /** Module that is providing this service */
247         ModuleRef creator;
248         /** Name of the service being provided */
249         const std::string name;
250         /** Type of service (must match object type) */
251         const ServiceType service;
252         ServiceProvider(Module* Creator, const std::string& Name, ServiceType Type)
253                 : creator(Creator), name(Name), service(Type) {}
254         virtual ~ServiceProvider();
255 };
256
257
258 #endif