InspIRCd  4.0
extensible.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2013, 2017-2020 Sadie Powell <[email protected]>
5  * Copyright (C) 2012, 2014-2015 Attila Molnar <[email protected]>
6  * Copyright (C) 2012 Robby <[email protected]>
7  * Copyright (C) 2009 Daniel De Graaf <[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 
26 class CoreExport ExtensionItem
27  : public ServiceProvider
28 {
29  public:
32  {
35 
38 
40  EXT_MEMBERSHIP
41  };
42 
45 
51  ExtensionItem(Module* owner, const std::string& key, ExtensibleType exttype);
52 
57  virtual void FromInternal(Extensible* container, const std::string& value) noexcept;
58 
63  virtual void FromNetwork(Extensible* container, const std::string& value) noexcept;
64 
70  virtual std::string ToHuman(const Extensible* container, void* item) const noexcept;
71 
77  virtual std::string ToInternal(const Extensible* container, void* item) const noexcept;
78 
84  virtual std::string ToNetwork(const Extensible* container, void* item) const noexcept;
85 
90  virtual void Delete(Extensible* container, void* item) = 0;
91 
93  void RegisterService() override;
94 
95  protected:
100  void* GetRaw(const Extensible* container) const;
101 
107  void* SetRaw(Extensible* container, void* value);
108 
113  void* UnsetRaw(Extensible* container);
114 
119  void Sync(const Extensible* container, void* item);
120 };
121 
129 class CoreExport Extensible
130  : public Cullable
131  , public Serializable
132 {
133  public:
135 
136  // Friend access for the protected getter/setter
137  friend class ExtensionItem;
138  private:
142  ExtensibleStore extensions;
143 
147  unsigned int culled:1;
148  public:
152  inline const ExtensibleStore& GetExtList() const { return extensions; }
153 
154  Extensible();
155  Cullable::Result Cull() override;
156  ~Extensible() override;
157  void UnhookExtensions(const std::vector<ExtensionItem*>& toRemove);
158 
162  void FreeAllExtItems();
163 
165  bool Deserialize(Data& data) override;
166 
168  bool Serialize(Serializable::Data& data) override;
169 };
170 
171 class CoreExport ExtensionManager
172 {
173  public:
174  typedef std::map<std::string, ExtensionItem*> ExtMap;
175 
176  bool Register(ExtensionItem* item);
177  void BeginUnregister(Module* module, std::vector<ExtensionItem*>& list);
178  ExtensionItem* GetItem(const std::string& name);
179 
183  const ExtMap& GetExts() const { return types; }
184 
185  private:
186  ExtMap types;
187 };
188 
190 template <typename T, typename Del = std::default_delete<T>>
192 {
193  public:
199  SimpleExtItem(Module* parent, const std::string& Key, ExtensibleType exttype)
200  : ExtensionItem(parent, Key, exttype)
201  {
202  }
203 
204  inline T* Get(const Extensible* container) const
205  {
206  return static_cast<T*>(GetRaw(container));
207  }
208 
209  inline void Set(Extensible* container, T* value, bool sync = true)
210  {
211  T* old = static_cast<T*>(SetRaw(container, value));
212  Delete(container, old);
213  if (sync)
214  Sync(container, value);
215  }
216 
217  template <typename... Args>
218  inline void Set(Extensible* container, Args&&... args)
219  {
220  Set(container, new T(std::forward<Args>(args)...));
221  }
222 
223  inline void Unset(Extensible* container, bool sync = true)
224  {
225  Delete(container, UnsetRaw(container));
226  if (sync)
227  Sync(container, nullptr);
228  }
229 
230  void Delete(Extensible* container, void* item) override
231  {
232  Del del;
233  del(static_cast<T*>(item));
234  }
235 };
236 
238 class CoreExport StringExtItem : public SimpleExtItem<std::string>
239 {
240  protected:
242  bool synced;
243 
244  public:
251  StringExtItem(Module* owner, const std::string& key, ExtensibleType exttype, bool sync = false);
252 
254  void FromInternal(Extensible* container, const std::string& value) noexcept override;
255 
257  void FromNetwork(Extensible* container, const std::string& value) noexcept override;
258 
260  std::string ToInternal(const Extensible* container, void* item) const noexcept override;
261 
263  std::string ToNetwork(const Extensible* container, void* item) const noexcept override;
264 };
265 
267 class CoreExport IntExtItem : public ExtensionItem
268 {
269  protected:
271  bool synced;
272 
273  public:
280  IntExtItem(Module* owner, const std::string& key, ExtensibleType exttype, bool sync = false);
281 
283  void Delete(Extensible* container, void* item) override;
284 
289  intptr_t Get(const Extensible* container) const;
290 
292  void FromInternal(Extensible* container, const std::string& value) noexcept override;
293 
295  void FromNetwork(Extensible* container, const std::string& value) noexcept override;
296 
302  void Set(Extensible* container, intptr_t value, bool sync = true);
303 
305  std::string ToInternal(const Extensible* container, void* item) const noexcept override;
306 
308  std::string ToNetwork(const Extensible* container, void* item) const noexcept override;
309 
314  void Unset(Extensible* container, bool sync = true);
315 };
316 
318 class CoreExport BoolExtItem : public ExtensionItem
319 {
320  protected:
322  bool synced;
323 
324  public:
331  BoolExtItem(Module* owner, const std::string& key, ExtensibleType exttype, bool sync = false);
332 
334  void Delete(Extensible* container, void* item) override;
335 
337  void FromInternal(Extensible* container, const std::string& value) noexcept override;
338 
340  void FromNetwork(Extensible* container, const std::string& value) noexcept override;
341 
343  std::string ToHuman(const Extensible* container, void* item) const noexcept override;
344 
346  std::string ToInternal(const Extensible* container, void* item) const noexcept override;
347 
349  std::string ToNetwork(const Extensible* container, void* item) const noexcept override;
350 
355  bool Get(const Extensible* container) const;
356 
361  void Set(Extensible* container, bool sync = true);
362 
367  void Unset(Extensible* container, bool sync = true);
368 };
Definition: extensible.h:319
bool synced
Definition: extensible.h:322
Definition: cull.h:38
Definition: cull.h:30
virtual Result Cull()
Definition: cull.cpp:54
Definition: extensible.h:132
const ExtensibleStore & GetExtList() const
Definition: extensible.h:152
Definition: extensible.h:28
ExtensibleType
Definition: extensible.h:32
@ EXT_CHANNEL
Definition: extensible.h:37
@ EXT_USER
Definition: extensible.h:34
void * UnsetRaw(Extensible *container)
Definition: extensible.cpp:123
virtual void Delete(Extensible *container, void *item)=0
void Sync(const Extensible *container, void *item)
Definition: extensible.cpp:134
const ExtensibleType type
Definition: extensible.h:44
void * SetRaw(Extensible *container, void *value)
Definition: extensible.cpp:112
void * GetRaw(const Extensible *container) const
Definition: extensible.cpp:103
Definition: extensible.h:172
const ExtMap & GetExts() const
Definition: extensible.h:183
Definition: extensible.h:268
bool synced
Definition: extensible.h:271
Definition: modules.h:286
Definition: serialize.h:31
Definition: serialize.h:24
Definition: base.h:204
virtual void RegisterService()
Definition: base.cpp:65
Definition: extensible.h:192
SimpleExtItem(Module *parent, const std::string &Key, ExtensibleType exttype)
Definition: extensible.h:199
void Delete(Extensible *container, void *item) override
Definition: extensible.h:230
Definition: extensible.h:239
bool synced
Definition: extensible.h:242