2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
35 /** Dummy class to help enforce culls being parent-called up to classbase */
39 friend class classbase;
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.
46 class CoreExport classbase
52 * Called just prior to destruction via cull list.
54 virtual CullResult cull();
58 classbase(const classbase&);
59 void operator=(const classbase&);
62 /** The base class for inspircd classes that provide a wrapping interface, and
63 * should only exist while being used. Prevents heap allocation.
65 class CoreExport interfacebase
69 static inline void* operator new(size_t, void* m) { return m; }
71 interfacebase(const interfacebase&);
72 void operator=(const interfacebase&);
73 static void* operator new(size_t);
74 static void operator delete(void*);
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
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
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.
90 class CoreExport refcountbase
92 mutable unsigned int refcount;
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; }
104 refcountbase(const refcountbase&);
105 void operator=(const refcountbase&);
108 /** Base class for use count tracking. Uses reference<>, but does not
109 * cause object deletion when the last user is removed.
111 * Safe for use as a second parent class; will not add a second vtable.
113 class CoreExport usecountbase
115 mutable unsigned int usecount;
117 usecountbase() : usecount(0) { }
119 inline unsigned int GetUseCount() const { return usecount; }
120 inline void refcount_inc() const { usecount++; }
121 inline bool refcount_dec() const { usecount--; return false; }
124 usecountbase(const usecountbase&);
125 void operator=(const usecountbase&);
128 template <typename T>
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)
139 other.value->refcount_inc();
140 this->reference::~reference();
147 if (value && value->refcount_dec())
151 inline reference<T>& operator=(T* other)
155 if (value && value->refcount_dec())
159 value->refcount_inc();
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; }
174 static void* operator new(size_t);
175 static void operator delete(void*);
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.
185 class CoreExport CoreException : public std::exception
188 /** Holds the error message to be displayed
190 const std::string err;
191 /** Source of the exception
193 const std::string source;
196 /** This constructor can be used to specify an error message before throwing.
197 * @param message Human readable error message
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
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.
210 virtual ~CoreException() throw() {}
211 /** Returns the reason for the exception.
212 * @return Human readable description of the error
214 const std::string& GetReason() const { return err; }
216 /** Returns the source of the exception
217 * @return Source of the exception
219 const std::string& GetSource() const { return source; }
223 class CoreExport ModuleException : public CoreException
226 /** This constructor can be used to specify an error message before throwing.
228 ModuleException(const std::string &message, Module* me = NULL);
231 typedef const reference<Module> ModuleRef;
236 /** is a ModeHandler */
238 /** is a metadata descriptor */
240 /** is a data processing provider (MD5, SQL) */
242 /** is an I/O hook provider (SSL) */
244 /** Service managed by a module */
248 /** A structure defining something that a module can provide */
249 class CoreExport ServiceProvider : public classbase
252 /** Module that is providing this service */
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();
261 /** Register this service in the appropriate registrar
263 virtual void RegisterService();
265 /** If called, this ServiceProvider won't be registered automatically
267 void DisableAutoRegister();