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