InspIRCd  4.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 
34 #include "utility/uncopiable.h"
35 #include "cull.h"
36 
50 class CoreExport refcountbase
51  : private insp::uncopiable
52 {
53  mutable unsigned int refcount = 0;
54  public:
55  refcountbase();
56  virtual ~refcountbase();
57  inline unsigned int GetReferenceCount() const { return refcount; }
58  static inline void* operator new(size_t, void* m) { return m; }
59  static void* operator new(size_t);
60  static void operator delete(void*);
61  inline void refcount_inc() const { refcount++; }
62  inline bool refcount_dec() const { refcount--; return !refcount; }
63 };
64 
70 class CoreExport usecountbase
71  : private insp::uncopiable
72 {
73  mutable unsigned int usecount = 0;
74  public:
75  usecountbase() = default;
76  ~usecountbase();
77  inline unsigned int GetUseCount() const { return usecount; }
78  inline void refcount_inc() const { usecount++; }
79  inline bool refcount_dec() const { usecount--; return false; }
80 };
81 
82 template <typename T>
83 class reference
84 {
85  T* value = nullptr;
86  public:
87  reference() = default;
88  reference(T* v) : value(v) { if (value) value->refcount_inc(); }
89  reference(const reference<T>& v) : value(v.value) { if (value) value->refcount_inc(); }
90  reference<T>& operator=(const reference<T>& other)
91  {
92  if (other.value)
93  other.value->refcount_inc();
94  this->reference::~reference();
95  value = other.value;
96  return *this;
97  }
98 
99  ~reference()
100  {
101  if (value && value->refcount_dec())
102  delete value;
103  }
104 
105  inline reference<T>& operator=(T* other)
106  {
107  if (value != other)
108  {
109  if (value && value->refcount_dec())
110  delete value;
111  value = other;
112  if (value)
113  value->refcount_inc();
114  }
115 
116  return *this;
117  }
118 
119  inline operator bool() const { return (value != NULL); }
120  inline operator T*() const { return value; }
121  inline T* operator->() const { return value; }
122  inline T& operator*() const { return *value; }
123  inline bool operator<(const reference<T>& other) const { return value < other.value; }
124  inline bool operator>(const reference<T>& other) const { return value > other.value; }
125  static inline void* operator new(size_t, void* m) { return m; }
126  private:
127 #ifndef _WIN32
128  static void* operator new(size_t);
129  static void operator delete(void*);
130 #endif
131 };
132 
139 class CoreExport CoreException : public std::exception
140 {
141  protected:
144  const std::string err;
147  const std::string source;
148 
149  public:
153  CoreException(const std::string &message) : err(message), source("The core") {}
159  CoreException(const std::string &message, const std::string &src) : err(message), source(src) {}
164  virtual ~CoreException() noexcept = default;
168  const std::string& GetReason() const { return err; }
169 
173  const std::string& GetSource() const { return source; }
174 };
175 
176 class Module;
177 class CoreExport ModuleException : public CoreException
178 {
179  public:
182  ModuleException(const std::string &message, Module* me = NULL);
183 };
184 
185 typedef const reference<Module> ModuleRef;
186 
187 enum ServiceType {
189  SERVICE_COMMAND,
191  SERVICE_MODE,
193  SERVICE_METADATA,
195  SERVICE_DATA,
197  SERVICE_IOHOOK,
199  SERVICE_CUSTOM
200 };
201 
203 class CoreExport ServiceProvider : public Cullable
204 {
205  public:
209  const std::string name;
211  const ServiceType service;
212  ServiceProvider(Module* Creator, const std::string& Name, ServiceType Type);
213 
215  const char* GetTypeString() const;
216 
219  virtual void RegisterService();
220 
223  void DisableAutoRegister();
224 };
Definition: base.h:140
CoreException(const std::string &message)
Definition: base.h:153
virtual ~CoreException() noexcept=default
const std::string source
Definition: base.h:147
const std::string err
Definition: base.h:144
CoreException(const std::string &message, const std::string &src)
Definition: base.h:159
const std::string & GetSource() const
Definition: base.h:173
Definition: cull.h:30
Definition: base.h:178
Definition: modules.h:286
Definition: base.h:204
const std::string name
Definition: base.h:209
const ServiceType service
Definition: base.h:211
ModuleRef creator
Definition: base.h:207
Definition: uncopiable.h:29
Definition: base.h:52
Definition: base.h:84
Definition: base.h:72