]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/base.h
Update copyright headers.
[user/henk/code/inspircd.git] / include / base.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2015 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2012-2013, 2017 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
8  *   Copyright (C) 2011-2012 Adam <Adam@anope.org>
9  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10  *   Copyright (C) 2007 Oliver Lupton <om@inspircd.org>
11  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12  *   Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
13  *
14  * This file is part of InspIRCd.  InspIRCd is free software: you can
15  * redistribute it and/or modify it under the terms of the GNU General Public
16  * License as published by the Free Software Foundation, version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26
27
28 #pragma once
29
30 #include <map>
31 #include <deque>
32 #include <string>
33 #include <list>
34
35 /** Dummy class to help enforce culls being parent-called up to classbase */
36 class CullResult
37 {
38         CullResult();
39         friend class classbase;
40 };
41
42 /** The base class for all inspircd classes with a well-defined lifetime.
43  * Classes that inherit from this may be destroyed through GlobalCulls,
44  * and may rely on cull() being called prior to their deletion.
45  */
46 class CoreExport classbase
47 {
48  public:
49         classbase();
50
51         /**
52          * Called just prior to destruction via cull list.
53          */
54         virtual CullResult cull();
55         virtual ~classbase();
56  private:
57         // uncopyable
58         classbase(const classbase&);
59         void operator=(const classbase&);
60 };
61
62 /** The base class for inspircd classes that provide a wrapping interface, and
63  * should only exist while being used. Prevents heap allocation.
64  */
65 class CoreExport interfacebase
66 {
67  public:
68         interfacebase() {}
69         static inline void* operator new(size_t, void* m) { return m; }
70  private:
71         interfacebase(const interfacebase&);
72         void operator=(const interfacebase&);
73         static void* operator new(size_t);
74         static void operator delete(void*);
75 };
76
77 /** The base class for inspircd classes that support reference counting.
78  * Any objects that do not have a well-defined lifetime should inherit from
79  * this, and should be assigned to a reference<type> object to establish their
80  * lifetime.
81  *
82  * Reference objects should not hold circular references back to themselves,
83  * even indirectly; this will cause a memory leak because the count will never
84  * drop to zero.
85  *
86  * Using a normal pointer for the object is recommended if you can assure that
87  * at least one reference<> will remain as long as that pointer is used; this
88  * will avoid the slight overhead of changing the reference count.
89  */
90 class CoreExport refcountbase
91 {
92         mutable unsigned int refcount;
93  public:
94         refcountbase();
95         virtual ~refcountbase();
96         inline unsigned int GetReferenceCount() const { return refcount; }
97         static inline void* operator new(size_t, void* m) { return m; }
98         static void* operator new(size_t);
99         static void operator delete(void*);
100         inline void refcount_inc() const { refcount++; }
101         inline bool refcount_dec() const { refcount--; return !refcount; }
102  private:
103         // uncopyable
104         refcountbase(const refcountbase&);
105         void operator=(const refcountbase&);
106 };
107
108 /** Base class for use count tracking. Uses reference<>, but does not
109  * cause object deletion when the last user is removed.
110  *
111  * Safe for use as a second parent class; will not add a second vtable.
112  */
113 class CoreExport usecountbase
114 {
115         mutable unsigned int usecount;
116  public:
117         usecountbase() : usecount(0) { }
118         ~usecountbase();
119         inline unsigned int GetUseCount() const { return usecount; }
120         inline void refcount_inc() const { usecount++; }
121         inline bool refcount_dec() const { usecount--; return false; }
122  private:
123         // uncopyable
124         usecountbase(const usecountbase&);
125         void operator=(const usecountbase&);
126 };
127
128 template <typename T>
129 class reference
130 {
131         T* value;
132  public:
133         reference() : value(0) { }
134         reference(T* v) : value(v) { if (value) value->refcount_inc(); }
135         reference(const reference<T>& v) : value(v.value) { if (value) value->refcount_inc(); }
136         reference<T>& operator=(const reference<T>& other)
137         {
138                 if (other.value)
139                         other.value->refcount_inc();
140                 this->reference::~reference();
141                 value = other.value;
142                 return *this;
143         }
144
145         ~reference()
146         {
147                 if (value && value->refcount_dec())
148                         delete value;
149         }
150
151         inline reference<T>& operator=(T* other)
152         {
153                 if (value != other)
154                 {
155                         if (value && value->refcount_dec())
156                                 delete value;
157                         value = other;
158                         if (value)
159                                 value->refcount_inc();
160                 }
161
162                 return *this;
163         }
164
165         inline operator bool() const { return (value != NULL); }
166         inline operator T*() const { return value; }
167         inline T* operator->() const { return value; }
168         inline T& operator*() const { return *value; }
169         inline bool operator<(const reference<T>& other) const { return value < other.value; }
170         inline bool operator>(const reference<T>& other) const { return value > other.value; }
171         static inline void* operator new(size_t, void* m) { return m; }
172  private:
173 #ifndef _WIN32
174         static void* operator new(size_t);
175         static void operator delete(void*);
176 #endif
177 };
178
179 /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
180  * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
181  * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
182  * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
183  * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
184  */
185 class CoreExport CoreException : public std::exception
186 {
187  protected:
188         /** Holds the error message to be displayed
189          */
190         const std::string err;
191         /** Source of the exception
192          */
193         const std::string source;
194
195  public:
196         /** This constructor can be used to specify an error message before throwing.
197          * @param message Human readable error message
198          */
199         CoreException(const std::string &message) : err(message), source("The core") {}
200         /** This constructor can be used to specify an error message before throwing,
201          * and to specify the source of the exception.
202          * @param message Human readable error message
203          * @param src Source of the exception
204          */
205         CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
206         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
207          * Actually no, it does nothing. Never mind.
208          * @throws Nothing!
209          */
210         virtual ~CoreException() throw() {}
211         /** Returns the reason for the exception.
212          * @return Human readable description of the error
213          */
214         const std::string& GetReason() const { return err; }
215
216         /** Returns the source of the exception
217          * @return Source of the exception
218          */
219         const std::string& GetSource() const { return source; }
220 };
221
222 class Module;
223 class CoreExport ModuleException : public CoreException
224 {
225  public:
226         /** This constructor can be used to specify an error message before throwing.
227          */
228         ModuleException(const std::string &message, Module* me = NULL);
229 };
230
231 typedef const reference<Module> ModuleRef;
232
233 enum ServiceType {
234         /** is a Command */
235         SERVICE_COMMAND,
236         /** is a ModeHandler */
237         SERVICE_MODE,
238         /** is a metadata descriptor */
239         SERVICE_METADATA,
240         /** is a data processing provider (MD5, SQL) */
241         SERVICE_DATA,
242         /** is an I/O hook provider (SSL) */
243         SERVICE_IOHOOK,
244         /** Service managed by a module */
245         SERVICE_CUSTOM
246 };
247
248 /** A structure defining something that a module can provide */
249 class CoreExport ServiceProvider : public classbase
250 {
251  public:
252         /** Module that is providing this service */
253         ModuleRef creator;
254         /** Name of the service being provided */
255         const std::string name;
256         /** Type of service (must match object type) */
257         const ServiceType service;
258         ServiceProvider(Module* Creator, const std::string& Name, ServiceType Type);
259         virtual ~ServiceProvider();
260
261         /** Register this service in the appropriate registrar
262          */
263         virtual void RegisterService();
264
265         /** If called, this ServiceProvider won't be registered automatically
266          */
267         void DisableAutoRegister();
268 };