1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
21 /** Dummy class to help enforce culls being parent-called up to classbase */
25 friend class classbase;
28 /** The base class for all inspircd classes with a well-defined lifetime.
29 * Classes that inherit from this may be destroyed through GlobalCulls,
30 * and may rely on cull() being called prior to their deletion.
32 class CoreExport classbase
38 * Called just prior to destruction via cull list.
40 * @return true to allow the delete, or false to halt the delete
42 virtual CullResult cull();
46 classbase(const classbase&);
47 void operator=(const classbase&);
50 /** The base class for inspircd classes that support reference counting.
51 * Any objects that do not have a well-defined lifetime should inherit from
52 * this, and should be assigned to a reference<type> object to establish their
55 * Reference objects should not hold circular references back to themselves,
56 * even indirectly; this will cause a memory leak because the count will never
59 * Using a normal pointer for the object is recommended if you can assure that
60 * at least one reference<> will remain as long as that pointer is used; this
61 * will avoid the slight overhead of changing the reference count.
63 class CoreExport refcountbase
65 unsigned int refcount;
68 virtual ~refcountbase();
69 inline unsigned int GetReferenceCount() const { return refcount; }
70 friend class reference_base;
73 refcountbase(const refcountbase&);
74 void operator=(const refcountbase&);
77 class CoreExport reference_base
80 template<typename T> static inline unsigned int inc(T* v) { return ++(v->refcount); }
81 template<typename T> static inline unsigned int dec(T* v) { return --(v->refcount); }
85 class reference : public reference_base
89 reference() : value(0) { }
90 reference(T* v) : value(v) { if (value) inc(value); }
91 reference(const reference& v) : value(v.value) { if (value) inc(value); }
92 reference<T>& operator=(const reference<T>& other)
96 this->reference::~reference();
110 inline const T* operator->() const { return value; }
111 inline const T& operator*() const { return *value; }
112 inline T* operator->() { return value; }
113 inline T& operator*() { return *value; }
114 inline operator bool() const { return value; }
115 inline operator T*() const { return value; }
118 /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
119 * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
120 * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
121 * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
122 * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
124 class CoreExport CoreException : public std::exception
127 /** Holds the error message to be displayed
129 const std::string err;
130 /** Source of the exception
132 const std::string source;
134 /** Default constructor, just uses the error mesage 'Core threw an exception'.
136 CoreException() : err("Core threw an exception"), source("The core") {}
137 /** This constructor can be used to specify an error message before throwing.
139 CoreException(const std::string &message) : err(message), source("The core") {}
140 /** This constructor can be used to specify an error message before throwing,
141 * and to specify the source of the exception.
143 CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
144 /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
145 * Actually no, it does nothing. Never mind.
148 virtual ~CoreException() throw() {};
149 /** Returns the reason for the exception.
150 * The module should probably put something informative here as the user will see this upon failure.
152 virtual const char* GetReason()
157 virtual const char* GetSource()
159 return source.c_str();
163 class CoreExport ModuleException : public CoreException
166 /** Default constructor, just uses the error mesage 'Module threw an exception'.
168 ModuleException() : CoreException("Module threw an exception", "A Module") {}
170 /** This constructor can be used to specify an error message before throwing.
172 ModuleException(const std::string &message) : CoreException(message, "A Module") {}
173 /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
174 * Actually no, it does nothing. Never mind.
177 virtual ~ModuleException() throw() {};