2 * InspIRCd -- Internet Relay Chat Daemon
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, 2020 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>
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.
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
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/>.
34 /** Dummy class to help enforce culls being parent-called up to classbase */
38 friend class classbase;
41 /** The base class for all inspircd classes with a well-defined lifetime.
42 * Classes that inherit from this may be destroyed through GlobalCulls,
43 * and may rely on cull() being called prior to their deletion.
45 class CoreExport classbase
51 * Called just prior to destruction via cull list.
53 virtual CullResult cull();
57 classbase(const classbase&);
58 void operator=(const classbase&);
61 /** The base class for inspircd classes that provide a wrapping interface, and
62 * should only exist while being used. Prevents heap allocation.
64 class CoreExport interfacebase
68 static inline void* operator new(size_t, void* m) { return m; }
70 interfacebase(const interfacebase&);
71 void operator=(const interfacebase&);
72 static void* operator new(size_t);
73 static void operator delete(void*);
76 /** The base class for inspircd classes that support reference counting.
77 * Any objects that do not have a well-defined lifetime should inherit from
78 * this, and should be assigned to a reference<type> object to establish their
81 * Reference objects should not hold circular references back to themselves,
82 * even indirectly; this will cause a memory leak because the count will never
85 * Using a normal pointer for the object is recommended if you can assure that
86 * at least one reference<> will remain as long as that pointer is used; this
87 * will avoid the slight overhead of changing the reference count.
89 class CoreExport refcountbase
91 mutable unsigned int refcount;
94 virtual ~refcountbase();
95 inline unsigned int GetReferenceCount() const { return refcount; }
96 static inline void* operator new(size_t, void* m) { return m; }
97 static void* operator new(size_t);
98 static void operator delete(void*);
99 inline void refcount_inc() const { refcount++; }
100 inline bool refcount_dec() const { refcount--; return !refcount; }
103 refcountbase(const refcountbase&);
104 void operator=(const refcountbase&);
107 /** Base class for use count tracking. Uses reference<>, but does not
108 * cause object deletion when the last user is removed.
110 * Safe for use as a second parent class; will not add a second vtable.
112 class CoreExport usecountbase
114 mutable unsigned int usecount;
116 usecountbase() : usecount(0) { }
118 inline unsigned int GetUseCount() const { return usecount; }
119 inline void refcount_inc() const { usecount++; }
120 inline bool refcount_dec() const { usecount--; return false; }
123 usecountbase(const usecountbase&);
124 void operator=(const usecountbase&);
127 template <typename T>
132 reference() : value(0) { }
133 reference(T* v) : value(v) { if (value) value->refcount_inc(); }
134 reference(const reference<T>& v) : value(v.value) { if (value) value->refcount_inc(); }
135 reference<T>& operator=(const reference<T>& other)
138 other.value->refcount_inc();
139 this->reference::~reference();
146 if (value && value->refcount_dec())
150 inline reference<T>& operator=(T* other)
154 if (value && value->refcount_dec())
158 value->refcount_inc();
164 inline operator bool() const { return (value != NULL); }
165 inline operator T*() const { return value; }
166 inline T* operator->() const { return value; }
167 inline T& operator*() const { return *value; }
168 inline bool operator<(const reference<T>& other) const { return value < other.value; }
169 inline bool operator>(const reference<T>& other) const { return value > other.value; }
170 static inline void* operator new(size_t, void* m) { return m; }
173 static void* operator new(size_t);
174 static void operator delete(void*);
178 /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
179 * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
180 * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
181 * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
182 * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
184 class CoreExport CoreException : public std::exception
187 /** Holds the error message to be displayed
189 const std::string err;
190 /** Source of the exception
192 const std::string source;
195 /** This constructor can be used to specify an error message before throwing.
196 * @param message Human readable error message
198 CoreException(const std::string &message) : err(message), source("The core") {}
199 /** This constructor can be used to specify an error message before throwing,
200 * and to specify the source of the exception.
201 * @param message Human readable error message
202 * @param src Source of the exception
204 CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
205 /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
206 * Actually no, it does nothing. Never mind.
209 virtual ~CoreException() throw() {}
210 /** Returns the reason for the exception.
211 * @return Human readable description of the error
213 const std::string& GetReason() const { return err; }
215 /** Returns the source of the exception
216 * @return Source of the exception
218 const std::string& GetSource() const { return source; }
222 class CoreExport ModuleException : public CoreException
225 /** This constructor can be used to specify an error message before throwing.
227 ModuleException(const std::string &message, Module* me = NULL);
230 typedef const reference<Module> ModuleRef;
235 /** is a ModeHandler */
237 /** is a metadata descriptor */
239 /** is a data processing provider (MD5, SQL) */
241 /** is an I/O hook provider */
243 /** Service managed by a module */
247 /** A structure defining something that a module can provide */
248 class CoreExport ServiceProvider : public classbase
251 /** Module that is providing this service */
253 /** Name of the service being provided */
254 const std::string name;
255 /** Type of service (must match object type) */
256 const ServiceType service;
257 ServiceProvider(Module* Creator, const std::string& Name, ServiceType Type);
258 virtual ~ServiceProvider();
260 /** Retrieves a string that represents the type of this service. */
261 const char* GetTypeString() const;
263 /** Register this service in the appropriate registrar
265 virtual void RegisterService();
267 /** If called, this ServiceProvider won't be registered automatically
269 void DisableAutoRegister();