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 ExtensionItem(const std::string& key, Module* owner);
42 virtual ~ExtensionItem();
43 /** Serialize this item into a string
45 * @param format The format to serialize to
46 * @param container The object containing this item
47 * @param item The item itself
49 virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const = 0;
50 /** Convert the string form back into an item
51 * @param format The format to serialize from (not FORMAT_USER)
52 * @param container The object that this item applies to
53 * @param value The return from a serialize() call that was run elsewhere with this key
55 virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value) = 0;
57 virtual void free(void* item) = 0;
60 /** Get the item from the internal map */
61 void* get_raw(const Extensible* container) const;
62 /** Set the item in the internal map; returns old value */
63 void* set_raw(Extensible* container, void* value);
64 /** Remove the item from the internal map; returns old value */
65 void* unset_raw(Extensible* container);
68 /** class Extensible is the parent class of many classes such as User and Channel.
69 * class Extensible implements a system which allows modules to 'extend' the class by attaching data within
70 * a map associated with the object. In this way modules can store their own custom information within user
71 * objects, channel objects and server objects, without breaking other modules (this is more sensible than using
72 * a flags variable, and each module defining bits within the flag as 'theirs' as it is less prone to conflict and
73 * supports arbitary data storage).
75 class CoreExport Extensible : public classbase
78 typedef std::map<reference<ExtensionItem>,void*> ExtensibleStore;
80 // Friend access for the protected getter/setter
81 friend class ExtensionItem;
83 /** Private data store.
84 * Holds all extensible metadata for the class.
86 ExtensibleStore extensions;
89 * Get the extension items for iteraton (i.e. for metadata sync during netburst)
91 inline const ExtensibleStore& GetExtList() const { return extensions; }
94 virtual CullResult cull();
95 virtual ~Extensible();
96 void doUnhookExtensions(const std::vector<reference<ExtensionItem> >& toRemove);
99 class CoreExport ExtensionManager
101 std::map<std::string, reference<ExtensionItem> > types;
103 bool Register(ExtensionItem* item);
104 void BeginUnregister(Module* module, std::vector<reference<ExtensionItem> >& list);
105 ExtensionItem* GetItem(const std::string& name);
108 /** Base class for items that are NOT synchronized between servers */
109 class CoreExport LocalExtItem : public ExtensionItem
112 LocalExtItem(const std::string& key, Module* owner);
113 virtual ~LocalExtItem();
114 virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
115 virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
116 virtual void free(void* item) = 0;
120 class SimpleExtItem : public LocalExtItem
123 SimpleExtItem(const std::string& Key, Module* parent) : LocalExtItem(Key, parent)
127 virtual ~SimpleExtItem()
131 inline T* get(const Extensible* container) const
133 return static_cast<T*>(get_raw(container));
136 inline void set(Extensible* container, const T& value)
138 T* ptr = new T(value);
139 T* old = static_cast<T*>(set_raw(container, ptr));
143 inline void set(Extensible* container, T* value)
145 T* old = static_cast<T*>(set_raw(container, value));
149 inline void unset(Extensible* container)
151 T* old = static_cast<T*>(unset_raw(container));
155 virtual void free(void* item)
157 delete static_cast<T*>(item);
161 class CoreExport LocalStringExt : public SimpleExtItem<std::string>
164 LocalStringExt(const std::string& key, Module* owner);
165 virtual ~LocalStringExt();
166 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
169 class CoreExport LocalIntExt : public LocalExtItem
172 LocalIntExt(const std::string& key, Module* owner);
173 virtual ~LocalIntExt();
174 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
175 intptr_t get(const Extensible* container) const;
176 intptr_t set(Extensible* container, intptr_t value);
177 void free(void* item);
180 class CoreExport StringExtItem : public ExtensionItem
183 StringExtItem(const std::string& key, Module* owner);
184 virtual ~StringExtItem();
185 std::string* get(const Extensible* container) const;
186 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
187 void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
188 void set(Extensible* container, const std::string& value);
189 void unset(Extensible* container);
190 void free(void* item);