]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/base.h
Move forward declarations to typedefs.h
[user/henk/code/inspircd.git] / include / base.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef __BASE_H__
15 #define __BASE_H__
16
17 #include <map>
18 #include <deque>
19 #include <string>
20
21 /** Dummy class to help enforce culls being parent-called up to classbase */
22 class CullResult
23 {
24         CullResult();
25         friend class classbase;
26 };
27
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.
31  */
32 class CoreExport classbase
33 {
34  public:
35         classbase();
36
37         /**
38          * Called just prior to destruction via cull list.
39          *
40          * @return true to allow the delete, or false to halt the delete
41          */
42         virtual CullResult cull();
43         virtual ~classbase();
44  private:
45         // uncopyable
46         classbase(const classbase&);
47         void operator=(const classbase&);
48 };
49
50 /** The base class for inspircd classes that provide a wrapping interface, and
51  * should only exist while being used. Prevents heap allocation.
52  */
53 class CoreExport interfacebase
54 {
55  public:
56         interfacebase() {}
57  private:
58         interfacebase(const interfacebase&);
59         void operator=(const interfacebase&);
60         void* operator new(size_t);
61         void operator delete(void*);
62 };
63
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
67  * lifetime.
68  *
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
71  * drop to zero.
72  *
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.
76  */
77 class CoreExport refcountbase
78 {
79         mutable unsigned int refcount;
80  public:
81         refcountbase();
82         virtual ~refcountbase();
83         inline unsigned int GetReferenceCount() const { return refcount; }
84         friend class reference_base;
85         void* operator new(size_t);
86         void operator delete(void*);
87  private:
88         // uncopyable
89         refcountbase(const refcountbase&);
90         void operator=(const refcountbase&);
91 };
92
93 class CoreExport reference_base
94 {
95  protected:
96         template<typename T> static inline unsigned int inc(T* v) { return ++(v->refcount); }
97         template<typename T> static inline unsigned int dec(T* v) { return --(v->refcount); }
98
99 };
100
101 template <typename T>
102 class reference : public reference_base
103 {
104         T* value;
105  public:
106         reference() : value(0) { }
107         reference(T* v) : value(v) { if (value) inc(value); }
108         reference(const reference<T>& v) : value(v.value) { if (value) inc(value); }
109         reference<T>& operator=(const reference<T>& other)
110         {
111                 if (other.value)
112                         inc(other.value);
113                 this->reference::~reference();
114                 value = other.value;
115                 return *this;
116         }
117
118         ~reference()
119         {
120                 if (value)
121                 {
122                         int rc = dec(value);
123                         if (rc == 0)
124                                 delete value;
125                 }
126         }
127         inline operator bool() const { return value; }
128         inline operator T*() const { return value; }
129         inline T* operator->() const { return value; }
130         inline T& operator*() const { return *value; }
131         inline bool operator<(const reference<T>& other) const { return value < other.value; }
132         inline bool operator>(const reference<T>& other) const { return value > other.value; }
133         inline bool operator==(const reference<T>& other) const { return value == other.value; }
134         inline bool operator!=(const reference<T>& other) const { return value != other.value; }
135  private:
136         void* operator new(size_t);
137         void operator delete(void*);
138 };
139
140 /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
141  * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
142  * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
143  * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
144  * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
145  */
146 class CoreExport CoreException : public std::exception
147 {
148  protected:
149         /** Holds the error message to be displayed
150          */
151         const std::string err;
152         /** Source of the exception
153          */
154         const std::string source;
155  public:
156         /** Default constructor, just uses the error mesage 'Core threw an exception'.
157          */
158         CoreException() : err("Core threw an exception"), source("The core") {}
159         /** This constructor can be used to specify an error message before throwing.
160          */
161         CoreException(const std::string &message) : err(message), source("The core") {}
162         /** This constructor can be used to specify an error message before throwing,
163          * and to specify the source of the exception.
164          */
165         CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
166         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
167          * Actually no, it does nothing. Never mind.
168          * @throws Nothing!
169          */
170         virtual ~CoreException() throw() {};
171         /** Returns the reason for the exception.
172          * The module should probably put something informative here as the user will see this upon failure.
173          */
174         virtual const char* GetReason()
175         {
176                 return err.c_str();
177         }
178
179         virtual const char* GetSource()
180         {
181                 return source.c_str();
182         }
183 };
184
185 class CoreExport ModuleException : public CoreException
186 {
187  public:
188         /** This constructor can be used to specify an error message before throwing.
189          */
190         ModuleException(const std::string &message, Module* me = NULL);
191 };
192
193 /** Module reference, similar to reference<Module>
194  */
195 class CoreExport ModuleRef : public reference_base
196 {
197         Module* const value;
198  public:
199         ModuleRef(Module* v);
200         ~ModuleRef();
201         inline operator Module*() const { return value; }
202         inline Module* operator->() const { return value; }
203         inline Module& operator*() const { return *value; }
204  private:
205         ModuleRef(const ModuleRef&);
206         void operator=(const ModuleRef&);
207         void* operator new(size_t);
208         void operator delete(void*);
209 };
210
211 #endif