]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/base.h
Remove duplicated function calls when using reference
[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 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
53  * lifetime.
54  *
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
57  * drop to zero.
58  *
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.
62  */
63 class CoreExport refcountbase
64 {
65         unsigned int refcount;
66  public:
67         refcountbase();
68         virtual ~refcountbase();
69         inline unsigned int GetReferenceCount() const { return refcount; }
70         friend class reference_base;
71  private:
72         // uncopyable
73         refcountbase(const refcountbase&);
74         void operator=(const refcountbase&);
75 };
76
77 class CoreExport reference_base
78 {
79  protected:
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); }
82 };
83
84 template <typename T>
85 class reference : public reference_base
86 {
87         T* value;
88  public:
89         reference() : value(0) { }
90         reference(T* v) : value(v) { if (value) inc(value); }
91         reference<T>& operator=(T* v)
92         {
93                 if (v)
94                         inc(v);
95                 this->reference::~reference();
96                 value = v;
97                 return *this;
98         }
99
100         ~reference()
101         {
102                 if (value)
103                 {
104                         int rc = dec(value);
105                         if (rc == 0)
106                                 delete value;
107                 }
108         }
109         inline const T* operator->() const { return value; }
110         inline const T& operator*() const { return *value; }
111         inline T* operator->() { return value; }
112         inline T& operator*() { return *value; }
113         inline operator bool() const { return value; }
114         inline operator T*() const { return value; }
115 };
116
117 /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
118  * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
119  * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
120  * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
121  * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
122  */
123 class CoreExport CoreException : public std::exception
124 {
125  protected:
126         /** Holds the error message to be displayed
127          */
128         const std::string err;
129         /** Source of the exception
130          */
131         const std::string source;
132  public:
133         /** Default constructor, just uses the error mesage 'Core threw an exception'.
134          */
135         CoreException() : err("Core threw an exception"), source("The core") {}
136         /** This constructor can be used to specify an error message before throwing.
137          */
138         CoreException(const std::string &message) : err(message), source("The core") {}
139         /** This constructor can be used to specify an error message before throwing,
140          * and to specify the source of the exception.
141          */
142         CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
143         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
144          * Actually no, it does nothing. Never mind.
145          * @throws Nothing!
146          */
147         virtual ~CoreException() throw() {};
148         /** Returns the reason for the exception.
149          * The module should probably put something informative here as the user will see this upon failure.
150          */
151         virtual const char* GetReason()
152         {
153                 return err.c_str();
154         }
155
156         virtual const char* GetSource()
157         {
158                 return source.c_str();
159         }
160 };
161
162 class CoreExport ModuleException : public CoreException
163 {
164  public:
165         /** Default constructor, just uses the error mesage 'Module threw an exception'.
166          */
167         ModuleException() : CoreException("Module threw an exception", "A Module") {}
168
169         /** This constructor can be used to specify an error message before throwing.
170          */
171         ModuleException(const std::string &message) : CoreException(message, "A Module") {}
172         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
173          * Actually no, it does nothing. Never mind.
174          * @throws Nothing!
175          */
176         virtual ~ModuleException() throw() {};
177 };
178
179 #endif