InspIRCd  2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations
extensible.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
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 #ifndef EXTENSIBLE_H
21 #define EXTENSIBLE_H
22 
23 #include <stdint.h>
24 
25 enum SerializeFormat
26 {
28  FORMAT_USER,
30  FORMAT_INTERNAL,
32  FORMAT_NETWORK,
34  FORMAT_PERSIST
35 };
36 
39 class CoreExport ExtensionItem : public ServiceProvider, public usecountbase
40 {
41  public:
42  ExtensionItem(const std::string& key, Module* owner);
43  virtual ~ExtensionItem();
50  virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const = 0;
56  virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value) = 0;
58  virtual void free(void* item) = 0;
59 
60  protected:
62  void* get_raw(const Extensible* container) const;
64  void* set_raw(Extensible* container, void* value);
66  void* unset_raw(Extensible* container);
67 };
68 
76 class CoreExport Extensible : public classbase
77 {
78  public:
79  typedef std::map<reference<ExtensionItem>,void*> ExtensibleStore;
80 
81  // Friend access for the protected getter/setter
82  friend class ExtensionItem;
83  private:
87  ExtensibleStore extensions;
88  public:
92  inline const ExtensibleStore& GetExtList() const { return extensions; }
93 
94  Extensible();
95  virtual CullResult cull();
96  virtual ~Extensible();
97  void doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove);
98 };
99 
100 class CoreExport ExtensionManager
101 {
102  std::map<std::string, reference<ExtensionItem> > types;
103  public:
104  bool Register(ExtensionItem* item);
105  void BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list);
106  ExtensionItem* GetItem(const std::string& name);
107 };
108 
110 class CoreExport LocalExtItem : public ExtensionItem
111 {
112  public:
113  LocalExtItem(const std::string& key, Module* owner);
114  virtual ~LocalExtItem();
115  virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
116  virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
117  virtual void free(void* item) = 0;
118 };
119 
120 template<typename T>
122 {
123  public:
124  SimpleExtItem(const std::string& Key, Module* parent) : LocalExtItem(Key, parent)
125  {
126  }
127 
128  virtual ~SimpleExtItem()
129  {
130  }
131 
132  inline T* get(const Extensible* container) const
133  {
134  return static_cast<T*>(get_raw(container));
135  }
136 
137  inline void set(Extensible* container, const T& value)
138  {
139  T* ptr = new T(value);
140  T* old = static_cast<T*>(set_raw(container, ptr));
141  delete old;
142  }
143 
144  inline void set(Extensible* container, T* value)
145  {
146  T* old = static_cast<T*>(set_raw(container, value));
147  delete old;
148  }
149 
150  inline void unset(Extensible* container)
151  {
152  T* old = static_cast<T*>(unset_raw(container));
153  delete old;
154  }
155 
156  virtual void free(void* item)
157  {
158  delete static_cast<T*>(item);
159  }
160 };
161 
162 class CoreExport LocalStringExt : public SimpleExtItem<std::string>
163 {
164  public:
165  LocalStringExt(const std::string& key, Module* owner);
166  virtual ~LocalStringExt();
167  std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
168 };
169 
170 class CoreExport LocalIntExt : public LocalExtItem
171 {
172  public:
173  LocalIntExt(const std::string& key, Module* owner);
174  virtual ~LocalIntExt();
175  std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
176  intptr_t get(const Extensible* container) const;
177  intptr_t set(Extensible* container, intptr_t value);
178  void free(void* item);
179 };
180 
181 class CoreExport StringExtItem : public ExtensionItem
182 {
183  public:
184  StringExtItem(const std::string& key, Module* owner);
185  virtual ~StringExtItem();
186  std::string* get(const Extensible* container) const;
187  std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
188  void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
189  void set(Extensible* container, const std::string& value);
190  void unset(Extensible* container);
191  void free(void* item);
192 };
193 
194 #endif
virtual CullResult cull()
Definition: base.cpp:34
virtual void free(void *item)=0
Definition: base.h:109
Definition: extensible.h:121
Definition: extensible.h:76
Definition: base.h:42
Definition: extensible.h:39
void * set_raw(Extensible *container, void *value)
Definition: base.cpp:109
virtual std::string serialize(SerializeFormat format, const Extensible *container, void *item) const =0
Definition: extensible.h:162
virtual void free(void *item)
Definition: extensible.h:156
Definition: extensible.h:181
void * unset_raw(Extensible *container)
Definition: base.cpp:125
const ExtensibleStore & GetExtList() const
Definition: extensible.h:92
Definition: modules.h:350
void * get_raw(const Extensible *container) const
Definition: base.cpp:100
virtual void unserialize(SerializeFormat format, Extensible *container, const std::string &value)=0
Definition: extensible.h:110
Definition: base.h:244
Definition: extensible.h:170
Definition: extensible.h:100
virtual void free(void *item)=0
Definition: base.h:32
virtual std::string serialize(SerializeFormat format, const Extensible *container, void *item) const
Definition: base.cpp:213