InspIRCd  3.0
base.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2013, 2015 Attila Molnar <[email protected]>
5  * Copyright (C) 2012-2013, 2017 Sadie Powell <[email protected]>
6  * Copyright (C) 2012 Robby <[email protected]>
7  * Copyright (C) 2012 ChrisTX <[email protected]>
8  * Copyright (C) 2011-2012 Adam <[email protected]>
9  * Copyright (C) 2009-2010 Daniel De Graaf <[email protected]>
10  * Copyright (C) 2007 Oliver Lupton <[email protected]>
11  * Copyright (C) 2007 Dennis Friis <[email protected]>
12  * Copyright (C) 2006, 2010 Craig Edwards <[email protected]>
13  *
14  * This file is part of InspIRCd. InspIRCd is free software: you can
15  * redistribute it and/or modify it under the terms of the GNU General Public
16  * License as published by the Free Software Foundation, version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 
28 #pragma once
29 
30 #include <map>
31 #include <deque>
32 #include <string>
33 #include <list>
34 
37 {
38  CullResult();
39  friend class classbase;
40 };
41 
46 class CoreExport classbase
47 {
48  public:
49  classbase();
50 
54  virtual CullResult cull();
55  virtual ~classbase();
56  private:
57  // uncopyable
58  classbase(const classbase&);
59  void operator=(const classbase&);
60 };
61 
65 class CoreExport interfacebase
66 {
67  public:
68  interfacebase() {}
69  static inline void* operator new(size_t, void* m) { return m; }
70  private:
72  void operator=(const interfacebase&);
73  static void* operator new(size_t);
74  static void operator delete(void*);
75 };
76 
90 class CoreExport refcountbase
91 {
92  mutable unsigned int refcount;
93  public:
94  refcountbase();
95  virtual ~refcountbase();
96  inline unsigned int GetReferenceCount() const { return refcount; }
97  static inline void* operator new(size_t, void* m) { return m; }
98  static void* operator new(size_t);
99  static void operator delete(void*);
100  inline void refcount_inc() const { refcount++; }
101  inline bool refcount_dec() const { refcount--; return !refcount; }
102  private:
103  // uncopyable
104  refcountbase(const refcountbase&);
105  void operator=(const refcountbase&);
106 };
107 
113 class CoreExport usecountbase
114 {
115  mutable unsigned int usecount;
116  public:
117  usecountbase() : usecount(0) { }
118  ~usecountbase();
119  inline unsigned int GetUseCount() const { return usecount; }
120  inline void refcount_inc() const { usecount++; }
121  inline bool refcount_dec() const { usecount--; return false; }
122  private:
123  // uncopyable
124  usecountbase(const usecountbase&);
125  void operator=(const usecountbase&);
126 };
127 
128 template <typename T>
130 {
131  T* value;
132  public:
133  reference() : value(0) { }
134  reference(T* v) : value(v) { if (value) value->refcount_inc(); }
135  reference(const reference<T>& v) : value(v.value) { if (value) value->refcount_inc(); }
136  reference<T>& operator=(const reference<T>& other)
137  {
138  if (other.value)
139  other.value->refcount_inc();
140  this->reference::~reference();
141  value = other.value;
142  return *this;
143  }
144 
145  ~reference()
146  {
147  if (value && value->refcount_dec())
148  delete value;
149  }
150 
151  inline reference<T>& operator=(T* other)
152  {
153  if (value != other)
154  {
155  if (value && value->refcount_dec())
156  delete value;
157  value = other;
158  if (value)
159  value->refcount_inc();
160  }
161 
162  return *this;
163  }
164 
165  inline operator bool() const { return (value != NULL); }
166  inline operator T*() const { return value; }
167  inline T* operator->() const { return value; }
168  inline T& operator*() const { return *value; }
169  inline bool operator<(const reference<T>& other) const { return value < other.value; }
170  inline bool operator>(const reference<T>& other) const { return value > other.value; }
171  static inline void* operator new(size_t, void* m) { return m; }
172  private:
173 #ifndef _WIN32
174  static void* operator new(size_t);
175  static void operator delete(void*);
176 #endif
177 };
178 
185 class CoreExport CoreException : public std::exception
186 {
187  protected:
190  const std::string err;
193  const std::string source;
194 
195  public:
199  CoreException(const std::string &message) : err(message), source("The core") {}
205  CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
210  virtual ~CoreException() throw() {}
214  const std::string& GetReason() const { return err; }
215 
219  const std::string& GetSource() const { return source; }
220 };
221 
222 class Module;
223 class CoreExport ModuleException : public CoreException
224 {
225  public:
228  ModuleException(const std::string &message, Module* me = NULL);
229 };
230 
231 typedef const reference<Module> ModuleRef;
232 
233 enum ServiceType {
235  SERVICE_COMMAND,
237  SERVICE_MODE,
239  SERVICE_METADATA,
241  SERVICE_DATA,
243  SERVICE_IOHOOK,
245  SERVICE_CUSTOM
246 };
247 
249 class CoreExport ServiceProvider : public classbase
250 {
251  public:
253  ModuleRef creator;
255  const std::string name;
257  const ServiceType service;
258  ServiceProvider(Module* Creator, const std::string& Name, ServiceType Type);
259  virtual ~ServiceProvider();
260 
263  virtual void RegisterService();
264 
267  void DisableAutoRegister();
268 };
ServiceProvider::creator
ModuleRef creator
Definition: base.h:253
ServiceProvider
Definition: base.h:249
interfacebase
Definition: base.h:65
reference
Definition: base.h:129
CoreException::GetReason
const std::string & GetReason() const
Definition: base.h:214
refcountbase
Definition: base.h:90
CoreException::~CoreException
virtual ~CoreException()
Definition: base.h:210
CoreException::err
const std::string err
Definition: base.h:190
ModuleException
Definition: base.h:223
classbase
Definition: base.h:46
CoreException::CoreException
CoreException(const std::string &message)
Definition: base.h:199
CoreException::GetSource
const std::string & GetSource() const
Definition: base.h:219
CoreException::CoreException
CoreException(const std::string &message, const std::string &src)
Definition: base.h:205
CoreException
Definition: base.h:185
Module
Definition: modules.h:247
CoreException::source
const std::string source
Definition: base.h:193
CullResult
Definition: base.h:36
ServiceProvider::service
const ServiceType service
Definition: base.h:257
ServiceProvider::name
const std::string name
Definition: base.h:255
usecountbase
Definition: base.h:113