2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
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.
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
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/>.
24 /** Shown to a human (does not need to be unserializable) */
26 /** Passed internally to this process (i.e. for /RELOADMODULE) */
28 /** Passed to other servers on the network (i.e. METADATA s2s command) */
30 /** Stored on disk (i.e. permchannel database) */
34 /** Class represnting an extension of some object
36 class CoreExport ExtensionItem : public ServiceProvider, public usecountbase
39 /** Extensible subclasses
48 /** Type (subclass) of Extensible that this ExtensionItem is valid for
50 const ExtensibleType type;
52 ExtensionItem(const std::string& key, ExtensibleType exttype, Module* owner);
53 virtual ~ExtensionItem();
54 /** Serialize this item into a string
56 * @param format The format to serialize to
57 * @param container The object containing this item
58 * @param item The item itself
60 virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const = 0;
61 /** Convert the string form back into an item
62 * @param format The format to serialize from (not FORMAT_USER)
63 * @param container The object that this item applies to
64 * @param value The return from a serialize() call that was run elsewhere with this key
66 virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value) = 0;
68 virtual void free(Extensible* container, void* item) = 0;
70 /** Register this object in the ExtensionManager
72 void RegisterService() CXX11_OVERRIDE;
75 /** Get the item from the internal map */
76 void* get_raw(const Extensible* container) const;
77 /** Set the item in the internal map; returns old value */
78 void* set_raw(Extensible* container, void* value);
79 /** Remove the item from the internal map; returns old value */
80 void* unset_raw(Extensible* container);
83 /** class Extensible is the parent class of many classes such as User and Channel.
84 * class Extensible implements a system which allows modules to 'extend' the class by attaching data within
85 * a map associated with the object. In this way modules can store their own custom information within user
86 * objects, channel objects and server objects, without breaking other modules (this is more sensible than using
87 * a flags variable, and each module defining bits within the flag as 'theirs' as it is less prone to conflict and
88 * supports arbitary data storage).
90 class CoreExport Extensible : public classbase
93 typedef insp::flat_map<reference<ExtensionItem>, void*> ExtensibleStore;
95 // Friend access for the protected getter/setter
96 friend class ExtensionItem;
98 /** Private data store.
99 * Holds all extensible metadata for the class.
101 ExtensibleStore extensions;
103 /** True if this Extensible has been culled.
104 * A warning is generated if false on destruction.
106 unsigned int culled:1;
109 * Get the extension items for iteraton (i.e. for metadata sync during netburst)
111 inline const ExtensibleStore& GetExtList() const { return extensions; }
114 CullResult cull() CXX11_OVERRIDE;
115 virtual ~Extensible();
116 void doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove);
119 * Free all extension items attached to this Extensible
121 void FreeAllExtItems();
124 class CoreExport ExtensionManager
127 typedef std::map<std::string, reference<ExtensionItem> > ExtMap;
129 bool Register(ExtensionItem* item);
130 void BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list);
131 ExtensionItem* GetItem(const std::string& name);
133 /** Get all registered extensions keyed by their names
134 * @return Const map of ExtensionItem pointers keyed by their names
136 const ExtMap& GetExts() const { return types; }
142 /** Base class for items that are NOT synchronized between servers */
143 class CoreExport LocalExtItem : public ExtensionItem
146 LocalExtItem(const std::string& key, ExtensibleType exttype, Module* owner);
147 virtual ~LocalExtItem();
148 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const CXX11_OVERRIDE;
149 void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE;
150 void free(Extensible* container, void* item) CXX11_OVERRIDE = 0;
153 template <typename T, typename Del = stdalgo::defaultdeleter<T> >
154 class SimpleExtItem : public LocalExtItem
157 SimpleExtItem(const std::string& Key, ExtensibleType exttype, Module* parent)
158 : LocalExtItem(Key, exttype, parent)
162 virtual ~SimpleExtItem()
166 inline T* get(const Extensible* container) const
168 return static_cast<T*>(get_raw(container));
171 inline void set(Extensible* container, const T& value)
173 T* ptr = new T(value);
174 T* old = static_cast<T*>(set_raw(container, ptr));
179 inline void set(Extensible* container, T* value)
181 T* old = static_cast<T*>(set_raw(container, value));
186 inline void unset(Extensible* container)
188 T* old = static_cast<T*>(unset_raw(container));
193 void free(Extensible* container, void* item) CXX11_OVERRIDE
196 del(static_cast<T*>(item));
200 class CoreExport LocalStringExt : public SimpleExtItem<std::string>
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 CXX11_OVERRIDE;
206 void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE;
209 class CoreExport LocalIntExt : public LocalExtItem
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 CXX11_OVERRIDE;
215 void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE;
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(Extensible* container, void* item) CXX11_OVERRIDE;
222 class CoreExport StringExtItem : public ExtensionItem
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 CXX11_OVERRIDE;
229 void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE;
230 void set(Extensible* container, const std::string& value);
231 void unset(Extensible* container);
232 void free(Extensible* container, void* item) CXX11_OVERRIDE;