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, 2017, 2020-2021 Sadie Powell <[email protected]>
6  * Copyright (C) 2013, 2015 Attila Molnar <[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 "compat.h"
32 #include <string>
33 
36 {
37  CullResult();
38  friend class classbase;
39 };
40 
45 class CoreExport classbase
46 {
47  public:
48  classbase();
49 
53  virtual CullResult cull();
54  virtual ~classbase();
55  private:
56  // uncopyable
57  classbase(const classbase&);
58  void operator=(const classbase&);
59 };
60 
64 class CoreExport interfacebase
65 {
66  public:
67  interfacebase() {}
68  static inline void* operator new(size_t, void* m) { return m; }
69  private:
71  void operator=(const interfacebase&);
72  static void* operator new(size_t);
73  static void operator delete(void*);
74 };
75 
89 class CoreExport refcountbase
90 {
91  mutable unsigned int refcount;
92  public:
93  refcountbase();
94  virtual ~refcountbase();
95  inline unsigned int GetReferenceCount() const { return refcount; }
96  static inline void* operator new(size_t, void* m) { return m; }
97  static void* operator new(size_t);
98  static void operator delete(void*);
99  inline void refcount_inc() const { refcount++; }
100  inline bool refcount_dec() const { refcount--; return !refcount; }
101  private:
102  // uncopyable
103  refcountbase(const refcountbase&);
104  void operator=(const refcountbase&);
105 };
106 
112 class CoreExport usecountbase
113 {
114  mutable unsigned int usecount;
115  public:
116  usecountbase() : usecount(0) { }
117  ~usecountbase();
118  inline unsigned int GetUseCount() const { return usecount; }
119  inline void refcount_inc() const { usecount++; }
120  inline bool refcount_dec() const { usecount--; return false; }
121  private:
122  // uncopyable
123  usecountbase(const usecountbase&);
124  void operator=(const usecountbase&);
125 };
126 
127 template <typename T>
129 {
130  T* value;
131  public:
132  reference() : value(0) { }
133  reference(T* v) : value(v) { if (value) value->refcount_inc(); }
134  reference(const reference<T>& v) : value(v.value) { if (value) value->refcount_inc(); }
135  reference<T>& operator=(const reference<T>& other)
136  {
137  if (other.value)
138  other.value->refcount_inc();
139  this->reference::~reference();
140  value = other.value;
141  return *this;
142  }
143 
144  ~reference()
145  {
146  if (value && value->refcount_dec())
147  delete value;
148  }
149 
150  inline reference<T>& operator=(T* other)
151  {
152  if (value != other)
153  {
154  if (value && value->refcount_dec())
155  delete value;
156  value = other;
157  if (value)
158  value->refcount_inc();
159  }
160 
161  return *this;
162  }
163 
164  inline operator bool() const { return (value != NULL); }
165  inline operator T*() const { return value; }
166  inline T* operator->() const { return value; }
167  inline T& operator*() const { return *value; }
168  inline bool operator<(const reference<T>& other) const { return value < other.value; }
169  inline bool operator>(const reference<T>& other) const { return value > other.value; }
170  static inline void* operator new(size_t, void* m) { return m; }
171  private:
172 #ifndef _WIN32
173  static void* operator new(size_t);
174  static void operator delete(void*);
175 #endif
176 };
177 
184 class CoreExport CoreException : public std::exception
185 {
186  protected:
189  const std::string err;
192  const std::string source;
193 
194  public:
198  CoreException(const std::string &message) : err(message), source("The core") {}
204  CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
209  virtual ~CoreException() throw() {}
213  const std::string& GetReason() const { return err; }
214 
218  const std::string& GetSource() const { return source; }
219 };
220 
221 class Module;
222 class CoreExport ModuleException : public CoreException
223 {
224  public:
227  ModuleException(const std::string &message, Module* me = NULL);
228 };
229 
230 typedef const reference<Module> ModuleRef;
231 
232 enum ServiceType {
234  SERVICE_COMMAND,
236  SERVICE_MODE,
238  SERVICE_METADATA,
240  SERVICE_DATA,
242  SERVICE_IOHOOK,
244  SERVICE_CUSTOM
245 };
246 
248 class CoreExport ServiceProvider : public classbase
249 {
250  public:
254  const std::string name;
256  const ServiceType service;
257  ServiceProvider(Module* Creator, const std::string& Name, ServiceType Type);
258  virtual ~ServiceProvider();
259 
261  const char* GetTypeString() const;
262 
265  virtual void RegisterService();
266 
269  void DisableAutoRegister();
270 };
Definition: base.h:185
CoreException(const std::string &message)
Definition: base.h:198
const std::string source
Definition: base.h:192
const std::string & GetReason() const
Definition: base.h:213
const std::string err
Definition: base.h:189
virtual ~CoreException()
Definition: base.h:209
CoreException(const std::string &message, const std::string &src)
Definition: base.h:204
const std::string & GetSource() const
Definition: base.h:218
Definition: base.h:36
Definition: base.h:223
Definition: modules.h:318
Definition: base.h:249
const std::string name
Definition: base.h:254
const ServiceType service
Definition: base.h:256
ModuleRef creator
Definition: base.h:252
Definition: base.h:46
Definition: base.h:65
Definition: base.h:90
Definition: base.h:129
Definition: base.h:113