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 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/>.
36 /** Dummy class to help enforce culls being parent-called up to classbase */
40 friend class classbase;
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.
47 class CoreExport classbase
53 * Called just prior to destruction via cull list.
55 virtual CullResult cull();
59 classbase(const classbase&);
60 void operator=(const classbase&);
63 /** The base class for inspircd classes that provide a wrapping interface, and
64 * should only exist while being used. Prevents heap allocation.
66 class CoreExport interfacebase
70 static inline void* operator new(size_t, void* m) { return m; }
72 interfacebase(const interfacebase&);
73 void operator=(const interfacebase&);
74 static void* operator new(size_t);
75 static void operator delete(void*);
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
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
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.
91 class CoreExport refcountbase
93 mutable unsigned int refcount;
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; }
105 refcountbase(const refcountbase&);
106 void operator=(const refcountbase&);
109 /** Base class for use count tracking. Uses reference<>, but does not
110 * cause object deletion when the last user is removed.
112 * Safe for use as a second parent class; will not add a second vtable.
114 class CoreExport usecountbase
116 mutable unsigned int usecount;
118 usecountbase() : usecount(0) { }
120 inline unsigned int GetUseCount() const { return usecount; }
121 inline void refcount_inc() const { usecount++; }
122 inline bool refcount_dec() const { usecount--; return false; }
125 usecountbase(const usecountbase&);
126 void operator=(const usecountbase&);
129 template <typename T>
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)
140 other.value->refcount_inc();
141 this->reference::~reference();
148 if (value && value->refcount_dec())
152 inline reference<T>& operator=(T* other)
156 if (value && value->refcount_dec())
160 value->refcount_inc();
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; }
175 static void* operator new(size_t);
176 static void operator delete(void*);
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.
186 class CoreExport CoreException : public std::exception
189 /** Holds the error message to be displayed
191 const std::string err;
192 /** Source of the exception
194 const std::string source;
197 /** This constructor can be used to specify an error message before throwing.
198 * @param message Human readable error message
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
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.
211 virtual ~CoreException() throw() {}
212 /** Returns the reason for the exception.
213 * @return Human readable description of the error
215 const std::string& GetReason() const { return err; }
217 /** Returns the source of the exception
218 * @return Source of the exception
220 const std::string& GetSource() const { return source; }
224 class CoreExport ModuleException : public CoreException
227 /** This constructor can be used to specify an error message before throwing.
229 ModuleException(const std::string &message, Module* me = NULL);
232 typedef const reference<Module> ModuleRef;
237 /** is a ModeHandler */
239 /** is a metadata descriptor */
241 /** is a data processing provider (MD5, SQL) */
243 /** is an I/O hook provider */
245 /** Service managed by a module */
249 /** A structure defining something that a module can provide */
250 class CoreExport ServiceProvider : public classbase
253 /** Module that is providing this service */
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();
262 /** Retrieves a string that represents the type of this service. */
263 const char* GetTypeString() const;
265 /** Register this service in the appropriate registrar
267 virtual void RegisterService();
269 /** If called, this ServiceProvider won't be registered automatically
271 void DisableAutoRegister();