]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/base.h
Make OnChannelRestrictionApply take a User* instead of a Membership* [jackmcbarn]
[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 /** The base class for all inspircd classes.
22  * Wherever possible, all classes you create should inherit from this,
23  * giving them the ability to be passed to various core functions
24  * as 'anonymous' classes.
25 */
26 class CoreExport classbase
27 {
28  public:
29         classbase();
30
31         /**
32          * Called just prior to destruction via cull list.
33          *
34          * @return true to allow the delete, or false to halt the delete
35          */
36         virtual bool cull();
37         virtual ~classbase();
38 };
39
40 /** The base class for inspircd classes that support reference counting.
41  * Any objects that do not have a well-defined lifetime should inherit from
42  * this
43  */
44 class CoreExport refcountbase : public classbase
45 {
46         unsigned int refcount;
47  public:
48         refcountbase();
49         virtual bool cull();
50         virtual ~refcountbase();
51         inline unsigned int GetReferenceCount() const { return refcount; }
52         friend class reference_base;
53 };
54
55 class CoreExport reference_base
56 {
57  protected:
58         static inline unsigned int inc(refcountbase* v) { return ++(v->refcount); }
59         static inline unsigned int dec(refcountbase* v) { return --(v->refcount); }
60 };
61
62 template <typename T>
63 class CoreExport reference : public reference_base
64 {
65         T* value;
66  public:
67         reference() : value(0) { }
68         reference(T* v) : value(v) { if (value) inc(value); }
69         reference(const reference& v) : value(v.value) { if (value) inc(value); }
70         reference<T>& operator=(const reference<T>& other)
71         {
72                 if (other.value)
73                         inc(other.value);
74                 this->reference::~reference();
75                 value = other.value;
76                 return *this;
77         }
78
79         ~reference()
80         {
81                 if (value)
82                 {
83                         int rc = dec(value);
84                         if (rc == 0 && value->cull())
85                                 delete value;
86                 }
87         }
88         inline const T* operator->() const { return value; }
89         inline const T& operator*() const { return *value; }
90         inline T* operator->() { return value; }
91         inline T& operator*() { return *value; }
92         inline operator bool() const { return value; }
93         inline operator T*() const { return value; }
94 };
95
96 /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
97  * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
98  * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
99  * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
100  * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
101  */
102 class CoreExport CoreException : public std::exception
103 {
104  protected:
105         /** Holds the error message to be displayed
106          */
107         const std::string err;
108         /** Source of the exception
109          */
110         const std::string source;
111  public:
112         /** Default constructor, just uses the error mesage 'Core threw an exception'.
113          */
114         CoreException() : err("Core threw an exception"), source("The core") {}
115         /** This constructor can be used to specify an error message before throwing.
116          */
117         CoreException(const std::string &message) : err(message), source("The core") {}
118         /** This constructor can be used to specify an error message before throwing,
119          * and to specify the source of the exception.
120          */
121         CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
122         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
123          * Actually no, it does nothing. Never mind.
124          * @throws Nothing!
125          */
126         virtual ~CoreException() throw() {};
127         /** Returns the reason for the exception.
128          * The module should probably put something informative here as the user will see this upon failure.
129          */
130         virtual const char* GetReason()
131         {
132                 return err.c_str();
133         }
134
135         virtual const char* GetSource()
136         {
137                 return source.c_str();
138         }
139 };
140
141 class CoreExport ModuleException : public CoreException
142 {
143  public:
144         /** Default constructor, just uses the error mesage 'Module threw an exception'.
145          */
146         ModuleException() : CoreException("Module threw an exception", "A Module") {}
147
148         /** This constructor can be used to specify an error message before throwing.
149          */
150         ModuleException(const std::string &message) : CoreException(message, "A Module") {}
151         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
152          * Actually no, it does nothing. Never mind.
153          * @throws Nothing!
154          */
155         virtual ~ModuleException() throw() {};
156 };
157
158 #endif