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 
22 enum SerializeFormat
23 {
25  FORMAT_USER,
27  FORMAT_INTERNAL,
29  FORMAT_NETWORK,
31  FORMAT_PERSIST
32 };
33 
36 class CoreExport ExtensionItem : public ServiceProvider, public usecountbase
37 {
38  public:
42  {
43  EXT_USER,
44  EXT_CHANNEL,
45  EXT_MEMBERSHIP
46  };
47 
51 
52  ExtensionItem(const std::string& key, ExtensibleType exttype, Module* owner);
53  virtual ~ExtensionItem();
60  virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const = 0;
66  virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value) = 0;
68  virtual void free(void* item) = 0;
69 
72  void RegisterService() CXX11_OVERRIDE;
73 
74  protected:
76  void* get_raw(const Extensible* container) const;
78  void* set_raw(Extensible* container, void* value);
80  void* unset_raw(Extensible* container);
81 };
82 
90 class CoreExport Extensible : public classbase
91 {
92  public:
94 
95  // Friend access for the protected getter/setter
96  friend class ExtensionItem;
97  private:
101  ExtensibleStore extensions;
102 
106  unsigned int culled:1;
107  public:
111  inline const ExtensibleStore& GetExtList() const { return extensions; }
112 
113  Extensible();
114  virtual CullResult cull() CXX11_OVERRIDE;
115  virtual ~Extensible();
116  void doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove);
117 
121  void FreeAllExtItems();
122 };
123 
124 class CoreExport ExtensionManager
125 {
126  public:
127  typedef std::map<std::string, reference<ExtensionItem> > ExtMap;
128 
129  bool Register(ExtensionItem* item);
130  void BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list);
131  ExtensionItem* GetItem(const std::string& name);
132 
136  const ExtMap& GetExts() const { return types; }
137 
138  private:
139  ExtMap types;
140 };
141 
143 class CoreExport LocalExtItem : public ExtensionItem
144 {
145  public:
146  LocalExtItem(const std::string& key, ExtensibleType exttype, Module* owner);
147  virtual ~LocalExtItem();
148  virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
149  virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
150  virtual void free(void* item) = 0;
151 };
152 
153 template <typename T, typename Del = stdalgo::defaultdeleter<T> >
155 {
156  public:
157  SimpleExtItem(const std::string& Key, ExtensibleType exttype, Module* parent)
158  : LocalExtItem(Key, exttype, parent)
159  {
160  }
161 
162  virtual ~SimpleExtItem()
163  {
164  }
165 
166  inline T* get(const Extensible* container) const
167  {
168  return static_cast<T*>(get_raw(container));
169  }
170 
171  inline void set(Extensible* container, const T& value)
172  {
173  T* ptr = new T(value);
174  T* old = static_cast<T*>(set_raw(container, ptr));
175  Del del;
176  del(old);
177  }
178 
179  inline void set(Extensible* container, T* value)
180  {
181  T* old = static_cast<T*>(set_raw(container, value));
182  Del del;
183  del(old);
184  }
185 
186  inline void unset(Extensible* container)
187  {
188  T* old = static_cast<T*>(unset_raw(container));
189  Del del;
190  del(old);
191  }
192 
193  virtual void free(void* item)
194  {
195  Del del;
196  del(static_cast<T*>(item));
197  }
198 };
199 
200 class CoreExport LocalStringExt : public SimpleExtItem<std::string>
201 {
202  public:
203  LocalStringExt(const std::string& key, ExtensibleType exttype, Module* owner);
204  virtual ~LocalStringExt();
205  std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
206  void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
207 };
208 
209 class CoreExport LocalIntExt : public LocalExtItem
210 {
211  public:
212  LocalIntExt(const std::string& key, ExtensibleType exttype, Module* owner);
213  virtual ~LocalIntExt();
214  std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
215  void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
216  intptr_t get(const Extensible* container) const;
217  intptr_t set(Extensible* container, intptr_t value);
218  void unset(Extensible* container) { set(container, 0); }
219  void free(void* item);
220 };
221 
222 class CoreExport StringExtItem : public ExtensionItem
223 {
224  public:
225  StringExtItem(const std::string& key, ExtensibleType exttype, Module* owner);
226  virtual ~StringExtItem();
227  std::string* get(const Extensible* container) const;
228  std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
229  void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
230  void set(Extensible* container, const std::string& value);
231  void unset(Extensible* container);
232  void free(void* item);
233 };
virtual CullResult cull()
Definition: base.cpp:36
Definition: base.h:108
ExtensibleType
Definition: extensible.h:41
Definition: extensible.h:154
Definition: extensible.h:90
Definition: flat_map.h:284
Definition: base.h:41
Definition: extensible.h:36
virtual std::string serialize(SerializeFormat format, const Extensible *container, void *item) const =0
Definition: extensible.h:200
Definition: extensible.h:222
virtual void RegisterService()
Definition: base.cpp:98
Definition: modules.h:245
const ExtensibleStore & GetExtList() const
Definition: extensible.h:111
virtual void unserialize(SerializeFormat format, Extensible *container, const std::string &value)=0
Definition: extensible.h:143
Definition: base.h:244
Definition: extensible.h:209
Definition: extensible.h:124
virtual void free(void *item)=0
const ExtensibleType type
Definition: extensible.h:50
Definition: base.h:31
const ExtMap & GetExts() const
Definition: extensible.h:136
virtual void free(void *item)
Definition: extensible.h:193