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