2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2006-2007 Oliver Lupton <oliverlupton@gmail.com>
6 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7 * Copyright (C) 2003-2005, 2007 Craig Edwards <craigedwards@brainbox.cc>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 /** Dummy class to help enforce culls being parent-called up to classbase */
35 friend class classbase;
38 /** The base class for all inspircd classes with a well-defined lifetime.
39 * Classes that inherit from this may be destroyed through GlobalCulls,
40 * and may rely on cull() being called prior to their deletion.
42 class CoreExport classbase
48 * Called just prior to destruction via cull list.
50 virtual CullResult cull();
54 classbase(const classbase&);
55 void operator=(const classbase&);
58 /** The base class for inspircd classes that provide a wrapping interface, and
59 * should only exist while being used. Prevents heap allocation.
61 class CoreExport interfacebase
65 static inline void* operator new(size_t, void* m) { return m; }
67 interfacebase(const interfacebase&);
68 void operator=(const interfacebase&);
69 static void* operator new(size_t);
70 static void operator delete(void*);
73 /** The base class for inspircd classes that support reference counting.
74 * Any objects that do not have a well-defined lifetime should inherit from
75 * this, and should be assigned to a reference<type> object to establish their
78 * Reference objects should not hold circular references back to themselves,
79 * even indirectly; this will cause a memory leak because the count will never
82 * Using a normal pointer for the object is recommended if you can assure that
83 * at least one reference<> will remain as long as that pointer is used; this
84 * will avoid the slight overhead of changing the reference count.
86 class CoreExport refcountbase
88 mutable unsigned int refcount;
91 virtual ~refcountbase();
92 inline unsigned int GetReferenceCount() const { return refcount; }
93 static inline void* operator new(size_t, void* m) { return m; }
94 static void* operator new(size_t);
95 static void operator delete(void*);
96 inline void refcount_inc() const { refcount++; }
97 inline bool refcount_dec() const { refcount--; return !refcount; }
100 refcountbase(const refcountbase&);
101 void operator=(const refcountbase&);
104 /** Base class for use count tracking. Uses reference<>, but does not
105 * cause object deletion when the last user is removed.
107 * Safe for use as a second parent class; will not add a second vtable.
109 class CoreExport usecountbase
111 mutable unsigned int usecount;
113 usecountbase() : usecount(0) { }
115 inline unsigned int GetUseCount() const { return usecount; }
116 inline void refcount_inc() const { usecount++; }
117 inline bool refcount_dec() const { usecount--; return false; }
120 usecountbase(const usecountbase&);
121 void operator=(const usecountbase&);
124 template <typename T>
125 class CoreExport reference
129 reference() : value(0) { }
130 reference(T* v) : value(v) { if (value) value->refcount_inc(); }
131 reference(const reference<T>& v) : value(v.value) { if (value) value->refcount_inc(); }
132 reference<T>& operator=(const reference<T>& other)
135 other.value->refcount_inc();
136 this->reference::~reference();
143 if (value && value->refcount_dec())
147 inline reference<T>& operator=(T* other)
151 if (value && value->refcount_dec())
155 value->refcount_inc();
161 inline operator bool() const { return (value != NULL); }
162 inline operator T*() const { return value; }
163 inline T* operator->() const { return value; }
164 inline T& operator*() const { return *value; }
165 inline bool operator<(const reference<T>& other) const { return value < other.value; }
166 inline bool operator>(const reference<T>& other) const { return value > other.value; }
167 static inline void* operator new(size_t, void* m) { return m; }
170 static void* operator new(size_t);
171 static void operator delete(void*);
175 /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
176 * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
177 * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
178 * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
179 * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
181 class CoreExport CoreException : public std::exception
184 /** Holds the error message to be displayed
186 const std::string err;
187 /** Source of the exception
189 const std::string source;
190 /** Default constructor, just uses the error mesage 'Core threw an exception'.
192 CoreException() : err("Core threw an exception"), source("The core") {}
193 /** This constructor can be used to specify an error message before throwing.
195 CoreException(const std::string &message) : err(message), source("The core") {}
196 /** This constructor can be used to specify an error message before throwing,
197 * and to specify the source of the exception.
199 CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
200 /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
201 * Actually no, it does nothing. Never mind.
204 virtual ~CoreException() throw() {};
205 /** Returns the reason for the exception.
206 * The module should probably put something informative here as the user will see this upon failure.
208 virtual const char* GetReason()
213 virtual const char* GetSource()
215 return source.c_str();
220 class CoreExport ModuleException : public CoreException
223 /** This constructor can be used to specify an error message before throwing.
225 ModuleException(const std::string &message, Module* me = NULL);
228 typedef const reference<Module> ModuleRef;
233 /** is a ModeHandler */
235 /** is a metadata descriptor */
237 /** is a data processing provider (MD5, SQL) */
239 /** is an I/O hook provider (SSL) */
243 /** A structure defining something that a module can provide */
244 class CoreExport ServiceProvider : public classbase
247 /** Module that is providing this service */
249 /** Name of the service being provided */
250 const std::string name;
251 /** Type of service (must match object type) */
252 const ServiceType service;
253 ServiceProvider(Module* Creator, const std::string& Name, ServiceType Type)
254 : creator(Creator), name(Name), service(Type) {}
255 virtual ~ServiceProvider();