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 provide a wrapping interface, and
51 * should only exist while being used. Prevents heap allocation.
53 class CoreExport interfacebase
58 interfacebase(const interfacebase&);
59 void operator=(const interfacebase&);
60 void* operator new(size_t);
61 void operator delete(void*);
64 /** The base class for inspircd classes that support reference counting.
65 * Any objects that do not have a well-defined lifetime should inherit from
66 * this, and should be assigned to a reference<type> object to establish their
69 * Reference objects should not hold circular references back to themselves,
70 * even indirectly; this will cause a memory leak because the count will never
73 * Using a normal pointer for the object is recommended if you can assure that
74 * at least one reference<> will remain as long as that pointer is used; this
75 * will avoid the slight overhead of changing the reference count.
77 class CoreExport refcountbase
79 mutable unsigned int refcount;
82 virtual ~refcountbase();
83 inline unsigned int GetReferenceCount() const { return refcount; }
84 friend class reference_base;
87 refcountbase(const refcountbase&);
88 void operator=(const refcountbase&);
91 class CoreExport reference_base
94 template<typename T> static inline unsigned int inc(T* v) { return ++(v->refcount); }
95 template<typename T> static inline unsigned int dec(T* v) { return --(v->refcount); }
99 class reference : public reference_base
103 reference() : value(0) { }
104 reference(T* v) : value(v) { if (value) inc(value); }
105 reference(const reference<T>& v) : value(v.value) { if (value) inc(value); }
106 reference<T>& operator=(const reference<T>& other)
110 this->reference::~reference();
124 inline operator bool() const { return value; }
125 inline operator T*() const { return value; }
126 inline T* operator->() const { return value; }
127 inline T& operator*() const { return *value; }
128 inline bool operator<(const reference<T>& other) const { return value < other.value; }
129 inline bool operator>(const reference<T>& other) const { return value > other.value; }
130 inline bool operator==(const reference<T>& other) const { return value == other.value; }
131 inline bool operator!=(const reference<T>& other) const { return value != other.value; }
133 void* operator new(size_t);
134 void operator delete(void*);
137 /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
138 * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
139 * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
140 * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
141 * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
143 class CoreExport CoreException : public std::exception
146 /** Holds the error message to be displayed
148 const std::string err;
149 /** Source of the exception
151 const std::string source;
153 /** Default constructor, just uses the error mesage 'Core threw an exception'.
155 CoreException() : err("Core threw an exception"), source("The core") {}
156 /** This constructor can be used to specify an error message before throwing.
158 CoreException(const std::string &message) : err(message), source("The core") {}
159 /** This constructor can be used to specify an error message before throwing,
160 * and to specify the source of the exception.
162 CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
163 /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
164 * Actually no, it does nothing. Never mind.
167 virtual ~CoreException() throw() {};
168 /** Returns the reason for the exception.
169 * The module should probably put something informative here as the user will see this upon failure.
171 virtual const char* GetReason()
176 virtual const char* GetSource()
178 return source.c_str();
182 class CoreExport ModuleException : public CoreException
185 /** Default constructor, just uses the error mesage 'Module threw an exception'.
187 ModuleException() : CoreException("Module threw an exception", "A Module") {}
189 /** This constructor can be used to specify an error message before throwing.
191 ModuleException(const std::string &message) : CoreException(message, "A Module") {}
192 /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
193 * Actually no, it does nothing. Never mind.
196 virtual ~ModuleException() throw() {};