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 : public classbase
137 {
138  public:
140 
141  // Friend access for the protected getter/setter
142  friend class ExtensionItem;
143  private:
147  ExtensibleStore extensions;
148 
152  unsigned int culled:1;
153  public:
157  inline const ExtensibleStore& GetExtList() const { return extensions; }
158 
159  Extensible();
160  CullResult cull() CXX11_OVERRIDE;
161  virtual ~Extensible();
162  void doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove);
163 
167  void FreeAllExtItems();
168 };
169 
170 class CoreExport ExtensionManager
171 {
172  public:
173  typedef std::map<std::string, reference<ExtensionItem> > ExtMap;
174 
175  bool Register(ExtensionItem* item);
176  void BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list);
177  ExtensionItem* GetItem(const std::string& name);
178 
182  const ExtMap& GetExts() const { return types; }
183 
184  private:
185  ExtMap types;
186 };
187 
190 
191 template <typename T, typename Del = stdalgo::defaultdeleter<T> >
193 {
194  public:
195  SimpleExtItem(const std::string& Key, ExtensibleType exttype, Module* parent)
196  : ExtensionItem(Key, exttype, parent)
197  {
198  }
199 
200  virtual ~SimpleExtItem()
201  {
202  }
203 
204  inline T* get(const Extensible* container) const
205  {
206  return static_cast<T*>(get_raw(container));
207  }
208 
209  inline void set(Extensible* container, const T& value)
210  {
211  T* ptr = new T(value);
212  T* old = static_cast<T*>(set_raw(container, ptr));
213  free(container, old);
214  }
215 
216  inline void set(Extensible* container, T* value)
217  {
218  T* old = static_cast<T*>(set_raw(container, value));
219  free(container, old);
220  }
221 
222  inline void unset(Extensible* container)
223  {
224  T* old = static_cast<T*>(unset_raw(container));
225  free(container, old);
226  }
227 
228  void free(Extensible* container, void* item) CXX11_OVERRIDE
229  {
230  Del del;
231  del(static_cast<T*>(item));
232  }
233 };
234 
235 class CoreExport LocalStringExt : public SimpleExtItem<std::string>
236 {
237  public:
238  LocalStringExt(const std::string& key, ExtensibleType exttype, Module* owner);
239  virtual ~LocalStringExt();
240  std::string ToInternal(const Extensible* container, void* item) const CXX11_OVERRIDE;
241  void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE;
242 };
243 
244 class CoreExport LocalIntExt : public ExtensionItem
245 {
246  public:
247  LocalIntExt(const std::string& key, ExtensibleType exttype, Module* owner);
248  virtual ~LocalIntExt();
249  std::string ToInternal(const Extensible* container, void* item) const CXX11_OVERRIDE;
250  void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE;
251  intptr_t get(const Extensible* container) const;
252  intptr_t set(Extensible* container, intptr_t value);
253  void unset(Extensible* container) { set(container, 0); }
254  void free(Extensible* container, void* item) CXX11_OVERRIDE;
255 };
256 
257 class CoreExport StringExtItem : public ExtensionItem
258 {
259  public:
260  StringExtItem(const std::string& key, ExtensibleType exttype, Module* owner);
261  virtual ~StringExtItem();
262  std::string* get(const Extensible* container) const;
263  std::string ToNetwork(const Extensible* container, void* item) const CXX11_OVERRIDE;
264  void FromNetwork(Extensible* container, const std::string& value) CXX11_OVERRIDE;
265  void set(Extensible* container, const std::string& value);
266  void unset(Extensible* container);
267  void free(Extensible* container, void* item) CXX11_OVERRIDE;
268 };
SimpleExtItem::free
void free(Extensible *container, void *item) CXX11_OVERRIDE
Definition: extensible.h:228
Extensible
Definition: extensible.h:136
LocalIntExt
Definition: extensible.h:244
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
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:157
ExtensionManager::GetExts
const ExtMap & GetExts() const
Definition: extensible.h:182
ExtensionItem::type
const ExtensibleType type
Definition: extensible.h:49
ExtensionItem::EXT_USER
Definition: extensible.h:39
SimpleExtItem
Definition: extensible.h:192
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:235
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:170
usecountbase
Definition: base.h:108
StringExtItem
Definition: extensible.h:257