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/>.
26 /** Shown to a human (does not need to be unserializable) */
28 /** Passed internally to this process (i.e. for /RELOADMODULE) */
30 /** Passed to other servers on the network (i.e. METADATA s2s command) */
32 /** Stored on disk (i.e. permchannel database) */
36 /** Class represnting an extension of some object
38 class CoreExport ExtensionItem : public ServiceProvider, public usecountbase
41 /** Extensible subclasses
50 /** Type (subclass) of Extensible that this ExtensionItem is valid for
52 const ExtensibleType type;
54 ExtensionItem(const std::string& key, ExtensibleType exttype, Module* owner);
55 virtual ~ExtensionItem();
56 /** Serialize this item into a string
58 * @param format The format to serialize to
59 * @param container The object containing this item
60 * @param item The item itself
62 virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const = 0;
63 /** Convert the string form back into an item
64 * @param format The format to serialize from (not FORMAT_USER)
65 * @param container The object that this item applies to
66 * @param value The return from a serialize() call that was run elsewhere with this key
68 virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value) = 0;
70 virtual void free(void* item) = 0;
73 /** Get the item from the internal map */
74 void* get_raw(const Extensible* container) const;
75 /** Set the item in the internal map; returns old value */
76 void* set_raw(Extensible* container, void* value);
77 /** Remove the item from the internal map; returns old value */
78 void* unset_raw(Extensible* container);
81 /** class Extensible is the parent class of many classes such as User and Channel.
82 * class Extensible implements a system which allows modules to 'extend' the class by attaching data within
83 * a map associated with the object. In this way modules can store their own custom information within user
84 * objects, channel objects and server objects, without breaking other modules (this is more sensible than using
85 * a flags variable, and each module defining bits within the flag as 'theirs' as it is less prone to conflict and
86 * supports arbitary data storage).
88 class CoreExport Extensible : public classbase
91 typedef insp::flat_map<reference<ExtensionItem>, void*> ExtensibleStore;
93 // Friend access for the protected getter/setter
94 friend class ExtensionItem;
96 /** Private data store.
97 * Holds all extensible metadata for the class.
99 ExtensibleStore extensions;
101 /** True if this Extensible has been culled.
102 * A warning is generated if false on destruction.
104 unsigned int culled:1;
107 * Get the extension items for iteraton (i.e. for metadata sync during netburst)
109 inline const ExtensibleStore& GetExtList() const { return extensions; }
112 virtual CullResult cull();
113 virtual ~Extensible();
114 void doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove);
117 * Free all extension items attached to this Extensible
119 void FreeAllExtItems();
122 class CoreExport ExtensionManager
125 typedef std::map<std::string, reference<ExtensionItem> > ExtMap;
127 bool Register(ExtensionItem* item);
128 void BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list);
129 ExtensionItem* GetItem(const std::string& name);
135 /** Base class for items that are NOT synchronized between servers */
136 class CoreExport LocalExtItem : public ExtensionItem
139 LocalExtItem(const std::string& key, ExtensibleType exttype, Module* owner);
140 virtual ~LocalExtItem();
141 virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
142 virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
143 virtual void free(void* item) = 0;
146 template <typename T, typename Del = stdalgo::defaultdeleter<T> >
147 class SimpleExtItem : public LocalExtItem
150 SimpleExtItem(const std::string& Key, ExtensibleType exttype, Module* parent)
151 : LocalExtItem(Key, exttype, parent)
155 virtual ~SimpleExtItem()
159 inline T* get(const Extensible* container) const
161 return static_cast<T*>(get_raw(container));
164 inline void set(Extensible* container, const T& value)
166 T* ptr = new T(value);
167 T* old = static_cast<T*>(set_raw(container, ptr));
172 inline void set(Extensible* container, T* value)
174 T* old = static_cast<T*>(set_raw(container, value));
179 inline void unset(Extensible* container)
181 T* old = static_cast<T*>(unset_raw(container));
186 virtual void free(void* item)
189 del(static_cast<T*>(item));
193 class CoreExport LocalStringExt : public SimpleExtItem<std::string>
196 LocalStringExt(const std::string& key, ExtensibleType exttype, Module* owner);
197 virtual ~LocalStringExt();
198 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
201 class CoreExport LocalIntExt : public LocalExtItem
204 LocalIntExt(const std::string& key, ExtensibleType exttype, Module* owner);
205 virtual ~LocalIntExt();
206 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
207 intptr_t get(const Extensible* container) const;
208 intptr_t set(Extensible* container, intptr_t value);
209 void unset(Extensible* container) { set(container, 0); }
210 void free(void* item);
213 class CoreExport StringExtItem : public ExtensionItem
216 StringExtItem(const std::string& key, ExtensibleType exttype, Module* owner);
217 virtual ~StringExtItem();
218 std::string* get(const Extensible* container) const;
219 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
220 void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
221 void set(Extensible* container, const std::string& value);
222 void unset(Extensible* container);
223 void free(void* item);