InspIRCd  3.0
base.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
5  * Copyright (C) 2006-2007 Oliver Lupton <[email protected]>
6  * Copyright (C) 2007 Dennis Friis <[email protected]>
7  * Copyright (C) 2003-2005, 2007 Craig Edwards <[email protected]>
8  *
9  * This file is part of InspIRCd. InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #pragma once
24 
25 #include <map>
26 #include <deque>
27 #include <string>
28 #include <list>
29 
32 {
33  CullResult();
34  friend class classbase;
35 };
36 
41 class CoreExport classbase
42 {
43  public:
44  classbase();
45 
49  virtual CullResult cull();
50  virtual ~classbase();
51  private:
52  // uncopyable
53  classbase(const classbase&);
54  void operator=(const classbase&);
55 };
56 
60 class CoreExport interfacebase
61 {
62  public:
63  interfacebase() {}
64  static inline void* operator new(size_t, void* m) { return m; }
65  private:
67  void operator=(const interfacebase&);
68  static void* operator new(size_t);
69  static void operator delete(void*);
70 };
71 
85 class CoreExport refcountbase
86 {
87  mutable unsigned int refcount;
88  public:
89  refcountbase();
90  virtual ~refcountbase();
91  inline unsigned int GetReferenceCount() const { return refcount; }
92  static inline void* operator new(size_t, void* m) { return m; }
93  static void* operator new(size_t);
94  static void operator delete(void*);
95  inline void refcount_inc() const { refcount++; }
96  inline bool refcount_dec() const { refcount--; return !refcount; }
97  private:
98  // uncopyable
99  refcountbase(const refcountbase&);
100  void operator=(const refcountbase&);
101 };
102 
108 class CoreExport usecountbase
109 {
110  mutable unsigned int usecount;
111  public:
112  usecountbase() : usecount(0) { }
113  ~usecountbase();
114  inline unsigned int GetUseCount() const { return usecount; }
115  inline void refcount_inc() const { usecount++; }
116  inline bool refcount_dec() const { usecount--; return false; }
117  private:
118  // uncopyable
119  usecountbase(const usecountbase&);
120  void operator=(const usecountbase&);
121 };
122 
123 template <typename T>
125 {
126  T* value;
127  public:
128  reference() : value(0) { }
129  reference(T* v) : value(v) { if (value) value->refcount_inc(); }
130  reference(const reference<T>& v) : value(v.value) { if (value) value->refcount_inc(); }
131  reference<T>& operator=(const reference<T>& other)
132  {
133  if (other.value)
134  other.value->refcount_inc();
135  this->reference::~reference();
136  value = other.value;
137  return *this;
138  }
139 
140  ~reference()
141  {
142  if (value && value->refcount_dec())
143  delete value;
144  }
145 
146  inline reference<T>& operator=(T* other)
147  {
148  if (value != other)
149  {
150  if (value && value->refcount_dec())
151  delete value;
152  value = other;
153  if (value)
154  value->refcount_inc();
155  }
156 
157  return *this;
158  }
159 
160  inline operator bool() const { return (value != NULL); }
161  inline operator T*() const { return value; }
162  inline T* operator->() const { return value; }
163  inline T& operator*() const { return *value; }
164  inline bool operator<(const reference<T>& other) const { return value < other.value; }
165  inline bool operator>(const reference<T>& other) const { return value > other.value; }
166  static inline void* operator new(size_t, void* m) { return m; }
167  private:
168 #ifndef _WIN32
169  static void* operator new(size_t);
170  static void operator delete(void*);
171 #endif
172 };
173 
180 class CoreExport CoreException : public std::exception
181 {
182  protected:
185  const std::string err;
188  const std::string source;
189 
190  public:
194  CoreException(const std::string &message) : err(message), source("The core") {}
200  CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
205  virtual ~CoreException() throw() {}
209  const std::string& GetReason() const { return err; }
210 
214  const std::string& GetSource() const { return source; }
215 };
216 
217 class Module;
218 class CoreExport ModuleException : public CoreException
219 {
220  public:
223  ModuleException(const std::string &message, Module* me = NULL);
224 };
225 
226 typedef const reference<Module> ModuleRef;
227 
228 enum ServiceType {
230  SERVICE_COMMAND,
232  SERVICE_MODE,
234  SERVICE_METADATA,
236  SERVICE_DATA,
238  SERVICE_IOHOOK,
240  SERVICE_CUSTOM
241 };
242 
244 class CoreExport ServiceProvider : public classbase
245 {
246  public:
250  const std::string name;
252  const ServiceType service;
253  ServiceProvider(Module* Creator, const std::string& Name, ServiceType Type);
254  virtual ~ServiceProvider();
255 
258  virtual void RegisterService();
259 
262  void DisableAutoRegister();
263 };
const std::string & GetSource() const
Definition: base.h:214
const std::string name
Definition: base.h:250
Definition: base.h:108
const std::string & GetReason() const
Definition: base.h:209
Definition: base.h:41
Definition: base.h:218
Definition: base.h:124
Definition: base.h:85
CoreException(const std::string &message, const std::string &src)
Definition: base.h:200
const std::string err
Definition: base.h:185
Definition: modules.h:245
Definition: base.h:60
const ServiceType service
Definition: base.h:252
Definition: base.h:180
Definition: base.h:244
CoreException(const std::string &message)
Definition: base.h:194
const std::string source
Definition: base.h:188
ModuleRef creator
Definition: base.h:248
Definition: base.h:31
virtual ~CoreException()
Definition: base.h:205