InspIRCd  3.0
extensible.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
5  *
6  * This file is part of InspIRCd. InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 #pragma once
21 
23 enum SerializeFormat
24 {
25  FORMAT_USER,
26  FORMAT_INTERNAL,
27  FORMAT_NETWORK,
28  FORMAT_PERSIST
29 };
30 
32 class CoreExport ExtensionItem : public ServiceProvider, public usecountbase
33 {
34  public:
37  {
40 
43 
45  EXT_MEMBERSHIP
46  };
47 
50 
56  ExtensionItem(const std::string& key, ExtensibleType exttype, Module* owner);
57 
59  virtual ~ExtensionItem();
60 
65  virtual void FromInternal(Extensible* container, const std::string& value);
66 
71  virtual void FromNetwork(Extensible* container, const std::string& value);
72 
78  virtual std::string ToHuman(const Extensible* container, void* item) const;
84  virtual std::string ToInternal(const Extensible* container, void* item) const ;
85 
91  virtual std::string ToNetwork(const Extensible* container, void* item) const;
92 
97  virtual void free(Extensible* container, void* item) = 0;
98 
100  void RegisterService() CXX11_OVERRIDE;
101 
103  DEPRECATED_METHOD(virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const);
104 
106  DEPRECATED_METHOD(virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value));
107 
108  protected:
113  void* get_raw(const Extensible* container) const;
114 
120  void* set_raw(Extensible* container, void* value);
121 
126  void* unset_raw(Extensible* container);
127 };
128 
136 class CoreExport Extensible
137  : public classbase
138  , public Serializable
139 {
140  public:
142 
143  // Friend access for the protected getter/setter
144  friend class ExtensionItem;
145  private:
149  ExtensibleStore extensions;
150 
154  unsigned int culled:1;
155  public:
159  inline const ExtensibleStore& GetExtList() const { return extensions; }
160 
161  Extensible();
162  CullResult cull() CXX11_OVERRIDE;
163  virtual ~Extensible();
164  void doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove);
165 
169  void FreeAllExtItems();
170 
172  bool Deserialize(Data& data) CXX11_OVERRIDE;
173 
175  bool Serialize(Serializable::Data& data) CXX11_OVERRIDE;
176 };
177 
178 class CoreExport ExtensionManager
179 {
180  public:
181  typedef std::map<std::string, reference<ExtensionItem> > ExtMap;
182 
183  bool Register(ExtensionItem* item);
184  void BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list);
185  ExtensionItem* GetItem(const std::string& name);
186 
190  const ExtMap& GetExts() const { return types; }
191 
192  private:
193  ExtMap types;
194 };
195 
198 
199 template <typename T, typename Del = stdalgo::defaultdeleter<T> >
201 {
202  public:
203  SimpleExtItem(const std::string& Key, ExtensibleType exttype, Module* parent)
204  : ExtensionItem(Key, exttype, parent)
205  {
206  }
207 
208  virtual ~SimpleExtItem()
209  {
210  }
211 
212  inline T* get(const Extensible* container) const
213  {
214  return static_cast<T*>(get_raw(container));
215  }
216 
217  inline void set(Extensible* container, const T& value)
218  {
219  T* ptr = new T(value);
220  T* old = static_cast<T*>(set_raw(container, ptr));
221  free(container, old);
222  }
223 
224  inline void set(Extensible* container, T* value)
225  {
226  T* old = static_cast<T*>(set_raw(container, value));
227  free(container, old);
228  }
229 
230  inline void unset(Extensible* container)
231  {
232  T* old = static_cast<T*>(unset_raw(container));
233  free(container, old);
234  }
235 
236  void free(Extensible* container, void* item) CXX11_OVERRIDE
237  {
238  Del del;
239  del(static_cast<T*>(item));
240  }
241 };
242 
243 class CoreExport LocalStringExt : public SimpleExtItem<std::string>
244 {
245  public:
246  LocalStringExt(const std::string& key, ExtensibleType exttype, Module* owner);
247  virtual ~LocalStringExt();
248  std::string ToInternal(const Extensible* container, void* item) const CXX11_OVERRIDE;
249  void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE;
250 };
251 
252 class CoreExport LocalIntExt : public ExtensionItem
253 {
254  public:
255  LocalIntExt(const std::string& key, ExtensibleType exttype, Module* owner);
256  virtual ~LocalIntExt();
257  std::string ToInternal(const Extensible* container, void* item) const CXX11_OVERRIDE;
258  void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE;
259  intptr_t get(const Extensible* container) const;
260  intptr_t set(Extensible* container, intptr_t value);
261  void unset(Extensible* container) { set(container, 0); }
262  void free(Extensible* container, void* item) CXX11_OVERRIDE;
263 };
264 
265 class CoreExport StringExtItem : public ExtensionItem
266 {
267  public:
268  StringExtItem(const std::string& key, ExtensibleType exttype, Module* owner);
269  virtual ~StringExtItem();
270  std::string* get(const Extensible* container) const;
271  std::string ToNetwork(const Extensible* container, void* item) const CXX11_OVERRIDE;
272  void FromNetwork(Extensible* container, const std::string& value) CXX11_OVERRIDE;
273  void set(Extensible* container, const std::string& value);
274  void unset(Extensible* container);
275  void free(Extensible* container, void* item) CXX11_OVERRIDE;
276 };
SimpleExtItem::free
void free(Extensible *container, void *item) CXX11_OVERRIDE
Definition: extensible.h:236
Extensible
Definition: extensible.h:136
LocalIntExt
Definition: extensible.h:252
ExtensionItem::ToNetwork
virtual std::string ToNetwork(const Extensible *container, void *item) const
Definition: base.cpp:248
ExtensionItem::EXT_CHANNEL
Definition: extensible.h:42
ServiceProvider
Definition: base.h:244
Serializable
Definition: serialize.h:23
insp::flat_map
Definition: flat_map.h:284
ExtensionItem
Definition: extensible.h:32
classbase::cull
virtual CullResult cull()
Definition: base.cpp:36
reference
Definition: base.h:124
ExtensionItem::free
virtual void free(Extensible *container, void *item)=0
ServiceProvider::RegisterService
virtual void RegisterService()
Definition: base.cpp:98
ExtensionItem::FromInternal
virtual void FromInternal(Extensible *container, const std::string &value)
Definition: base.cpp:222
classbase
Definition: base.h:41
Extensible::GetExtList
const ExtensibleStore & GetExtList() const
Definition: extensible.h:159
ExtensionManager::GetExts
const ExtMap & GetExts() const
Definition: extensible.h:190
ExtensionItem::type
const ExtensibleType type
Definition: extensible.h:49
ExtensionItem::EXT_USER
Definition: extensible.h:39
SimpleExtItem
Definition: extensible.h:200
ExtensionItem::FromNetwork
virtual void FromNetwork(Extensible *container, const std::string &value)
Definition: base.cpp:227
Module
Definition: modules.h:242
CullResult
Definition: base.h:31
LocalStringExt
Definition: extensible.h:243
ExtensionItem::ToInternal
virtual std::string ToInternal(const Extensible *container, void *item) const
Definition: base.cpp:243
ExtensionItem::ExtensibleType
ExtensibleType
Definition: extensible.h:36
ServiceProvider::name
const std::string name
Definition: base.h:250
ExtensionManager
Definition: extensible.h:178
usecountbase
Definition: base.h:108
StringExtItem
Definition: extensible.h:265