]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/base.h
Create StreamSocket for IO hooking implementation
[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         // Called just prior to destruction via cull list
32         virtual void cull();
33         virtual ~classbase();
34 };
35
36 /** BoolSet is a utility class designed to hold eight bools in a bitmask.
37  * Use BoolSet::Set and BoolSet::Get to set and get bools in the bitmask,
38  * and Unset and Invert for special operations upon them.
39  */
40 class CoreExport BoolSet : public classbase
41 {
42         /** Actual bit values */
43         char bits;
44
45  public:
46
47         /** The default constructor initializes the BoolSet to all values unset.
48          */
49         BoolSet();
50
51         /** This constructor copies the default bitmask from a char
52          */
53         BoolSet(char bitmask);
54
55         /** The Set method sets one bool in the set.
56          *
57          * @param number The number of the item to set. This must be between 0 and 7.
58          */
59         void Set(int number);
60
61         /** The Get method returns the value of one bool in the set
62          *
63          * @param number The number of the item to retrieve. This must be between 0 and 7.
64          *
65          * @return True if the item is set, false if it is unset.
66          */
67         bool Get(int number);
68
69         /** The Unset method unsets one value in the set
70          *
71          * @param number The number of the item to set. This must be between 0 and 7.
72          */
73         void Unset(int number);
74
75         /** The Unset method inverts (flips) one value in the set
76          *
77          * @param number The number of the item to invert. This must be between 0 and 7.
78          */
79         void Invert(int number);
80
81         /** Compare two BoolSets
82          */
83         bool operator==(BoolSet other);
84
85         /** OR two BoolSets together
86          */
87         BoolSet operator|(BoolSet other);
88
89         /** AND two BoolSets together
90          */
91         BoolSet operator&(BoolSet other);
92
93         /** Assign one BoolSet to another
94          */
95         bool operator=(BoolSet other);
96 };
97
98 /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
99  * When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
100  * a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
101  * be loaded. If this happens, the error message returned by ModuleException::GetReason will be displayed to the user
102  * attempting to load the module, or dumped to the console if the ircd is currently loading for the first time.
103  */
104 class CoreExport CoreException : public std::exception
105 {
106  protected:
107         /** Holds the error message to be displayed
108          */
109         const std::string err;
110         /** Source of the exception
111          */
112         const std::string source;
113  public:
114         /** Default constructor, just uses the error mesage 'Core threw an exception'.
115          */
116         CoreException() : err("Core threw an exception"), source("The core") {}
117         /** This constructor can be used to specify an error message before throwing.
118          */
119         CoreException(const std::string &message) : err(message), source("The core") {}
120         /** This constructor can be used to specify an error message before throwing,
121          * and to specify the source of the exception.
122          */
123         CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
124         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
125          * Actually no, it does nothing. Never mind.
126          * @throws Nothing!
127          */
128         virtual ~CoreException() throw() {};
129         /** Returns the reason for the exception.
130          * The module should probably put something informative here as the user will see this upon failure.
131          */
132         virtual const char* GetReason()
133         {
134                 return err.c_str();
135         }
136
137         virtual const char* GetSource()
138         {
139                 return source.c_str();
140         }
141 };
142
143 class CoreExport ModuleException : public CoreException
144 {
145  public:
146         /** Default constructor, just uses the error mesage 'Module threw an exception'.
147          */
148         ModuleException() : CoreException("Module threw an exception", "A Module") {}
149
150         /** This constructor can be used to specify an error message before throwing.
151          */
152         ModuleException(const std::string &message) : CoreException(message, "A Module") {}
153         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
154          * Actually no, it does nothing. Never mind.
155          * @throws Nothing!
156          */
157         virtual ~ModuleException() throw() {};
158 };
159
160 #endif