InspIRCd  3.0
extensible.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2013, 2017-2019 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 enum SerializeFormat
27 {
28  FORMAT_USER,
29  FORMAT_INTERNAL,
30  FORMAT_NETWORK,
31  FORMAT_PERSIST
32 };
33 
35 class CoreExport ExtensionItem : public ServiceProvider, public usecountbase
36 {
37  public:
40  {
43 
46 
48  EXT_MEMBERSHIP
49  };
50 
53 
59  ExtensionItem(const std::string& key, ExtensibleType exttype, Module* owner);
60 
62  virtual ~ExtensionItem();
63 
68  virtual void FromInternal(Extensible* container, const std::string& value);
69 
74  virtual void FromNetwork(Extensible* container, const std::string& value);
75 
81  virtual std::string ToHuman(const Extensible* container, void* item) const;
87  virtual std::string ToInternal(const Extensible* container, void* item) const ;
88 
94  virtual std::string ToNetwork(const Extensible* container, void* item) const;
95 
100  virtual void free(Extensible* container, void* item) = 0;
101 
103  void RegisterService() CXX11_OVERRIDE;
104 
106  DEPRECATED_METHOD(virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const);
107 
109  DEPRECATED_METHOD(virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value));
110 
111  protected:
116  void* get_raw(const Extensible* container) const;
117 
123  void* set_raw(Extensible* container, void* value);
124 
129  void* unset_raw(Extensible* container);
130 };
131 
139 class CoreExport Extensible
140  : public classbase
141  , public Serializable
142 {
143  public:
145 
146  // Friend access for the protected getter/setter
147  friend class ExtensionItem;
148  private:
152  ExtensibleStore extensions;
153 
157  unsigned int culled:1;
158  public:
162  inline const ExtensibleStore& GetExtList() const { return extensions; }
163 
164  Extensible();
165  CullResult cull() CXX11_OVERRIDE;
166  virtual ~Extensible();
167  void doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove);
168 
172  void FreeAllExtItems();
173 
175  bool Deserialize(Data& data) CXX11_OVERRIDE;
176 
178  bool Serialize(Serializable::Data& data) CXX11_OVERRIDE;
179 };
180 
181 class CoreExport ExtensionManager
182 {
183  public:
184  typedef std::map<std::string, reference<ExtensionItem> > ExtMap;
185 
186  bool Register(ExtensionItem* item);
187  void BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list);
188  ExtensionItem* GetItem(const std::string& name);
189 
193  const ExtMap& GetExts() const { return types; }
194 
195  private:
196  ExtMap types;
197 };
198 
201 
202 template <typename T, typename Del = stdalgo::defaultdeleter<T> >
204 {
205  public:
206  SimpleExtItem(const std::string& Key, ExtensibleType exttype, Module* parent)
207  : ExtensionItem(Key, exttype, parent)
208  {
209  }
210 
211  virtual ~SimpleExtItem()
212  {
213  }
214 
215  inline T* get(const Extensible* container) const
216  {
217  return static_cast<T*>(get_raw(container));
218  }
219 
220  inline void set(Extensible* container, const T& value)
221  {
222  T* ptr = new T(value);
223  T* old = static_cast<T*>(set_raw(container, ptr));
224  free(container, old);
225  }
226 
227  inline void set(Extensible* container, T* value)
228  {
229  T* old = static_cast<T*>(set_raw(container, value));
230  free(container, old);
231  }
232 
233  inline void unset(Extensible* container)
234  {
235  T* old = static_cast<T*>(unset_raw(container));
236  free(container, old);
237  }
238 
239  void free(Extensible* container, void* item) CXX11_OVERRIDE
240  {
241  Del del;
242  del(static_cast<T*>(item));
243  }
244 };
245 
246 class CoreExport LocalStringExt : public SimpleExtItem<std::string>
247 {
248  public:
249  LocalStringExt(const std::string& key, ExtensibleType exttype, Module* owner);
250  virtual ~LocalStringExt();
251  std::string ToInternal(const Extensible* container, void* item) const CXX11_OVERRIDE;
252  void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE;
253 };
254 
255 class CoreExport LocalIntExt : public ExtensionItem
256 {
257  public:
258  LocalIntExt(const std::string& key, ExtensibleType exttype, Module* owner);
259  virtual ~LocalIntExt();
260  std::string ToInternal(const Extensible* container, void* item) const CXX11_OVERRIDE;
261  void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE;
262  intptr_t get(const Extensible* container) const;
263  intptr_t set(Extensible* container, intptr_t value);
264  void unset(Extensible* container) { set(container, 0); }
265  void free(Extensible* container, void* item) CXX11_OVERRIDE;
266 };
267 
268 class CoreExport StringExtItem : public ExtensionItem
269 {
270  public:
271  StringExtItem(const std::string& key, ExtensibleType exttype, Module* owner);
272  virtual ~StringExtItem();
273  std::string* get(const Extensible* container) const;
274  std::string ToNetwork(const Extensible* container, void* item) const CXX11_OVERRIDE;
275  void FromNetwork(Extensible* container, const std::string& value) CXX11_OVERRIDE;
276  void set(Extensible* container, const std::string& value);
277  void unset(Extensible* container);
278  void free(Extensible* container, void* item) CXX11_OVERRIDE;
279 };
SimpleExtItem::free
void free(Extensible *container, void *item) CXX11_OVERRIDE
Definition: extensible.h:239
Extensible
Definition: extensible.h:139
LocalIntExt
Definition: extensible.h:255
ExtensionItem::ToNetwork
virtual std::string ToNetwork(const Extensible *container, void *item) const
Definition: base.cpp:252
ExtensionItem::EXT_CHANNEL
@ EXT_CHANNEL
Definition: extensible.h:45
ServiceProvider
Definition: base.h:249
Serializable
Definition: serialize.h:23
insp::flat_map
Definition: flat_map.h:285
ExtensionItem
Definition: extensible.h:35
classbase::cull
virtual CullResult cull()
Definition: base.cpp:41
reference
Definition: base.h:129
ExtensionItem::free
virtual void free(Extensible *container, void *item)=0
ServiceProvider::RegisterService
virtual void RegisterService()
Definition: base.cpp:102
ExtensionItem::FromInternal
virtual void FromInternal(Extensible *container, const std::string &value)
Definition: base.cpp:226
classbase
Definition: base.h:46
Extensible::GetExtList
const ExtensibleStore & GetExtList() const
Definition: extensible.h:162
ExtensionManager::GetExts
const ExtMap & GetExts() const
Definition: extensible.h:193
ExtensionItem::type
const ExtensibleType type
Definition: extensible.h:52
ExtensionItem::EXT_USER
@ EXT_USER
Definition: extensible.h:42
SimpleExtItem
Definition: extensible.h:203
ExtensionItem::FromNetwork
virtual void FromNetwork(Extensible *container, const std::string &value)
Definition: base.cpp:231
Module
Definition: modules.h:247
CullResult
Definition: base.h:36
LocalStringExt
Definition: extensible.h:246
ExtensionItem::ToInternal
virtual std::string ToInternal(const Extensible *container, void *item) const
Definition: base.cpp:247
ExtensionItem::ExtensibleType
ExtensibleType
Definition: extensible.h:39
ServiceProvider::name
const std::string name
Definition: base.h:255
ExtensionManager
Definition: extensible.h:181
usecountbase
Definition: base.h:113
StringExtItem
Definition: extensible.h:268