6 /** Shown to a human (does not need to be unserializable) */
8 /** Passed internally to this process (i.e. for /RELOADMODULE) */
10 /** Passed to other servers on the network (i.e. METADATA s2s command) */
12 /** Stored on disk (i.e. permchannel database) */
16 /** Class represnting an extension of some object
18 class CoreExport ExtensionItem
21 const std::string key;
23 ExtensionItem(const std::string& key, Module* owner);
24 /** Serialize this item into a string
26 * @param format The format to serialize to
27 * @param container The object containing this item
28 * @param item The item itself
30 virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) = 0;
31 /** Convert the string form back into an item
32 * @param format The format to serialize from (not FORMAT_USER)
33 * @param container The object that this item applies to
34 * @param value The return from a serialize() call that was run elsewhere with this key
36 virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value) = 0;
38 virtual void free(void* item) = 0;
41 /** Get the item from the internal map */
42 void* get_raw(const Extensible* container);
43 /** Set the item in the internal map; returns old value */
44 void* set_raw(Extensible* container, void* value);
45 /** Remove the item from the internal map; returns old value */
46 void* unset_raw(Extensible* container);
49 /** A private data store for an Extensible class */
50 typedef std::map<std::string,void*> ExtensibleStore;
52 /** class Extensible is the parent class of many classes such as User and Channel.
53 * class Extensible implements a system which allows modules to 'extend' the class by attaching data within
54 * a map associated with the object. In this way modules can store their own custom information within user
55 * objects, channel objects and server objects, without breaking other modules (this is more sensible than using
56 * a flags variable, and each module defining bits within the flag as 'theirs' as it is less prone to conflict and
57 * supports arbitary data storage).
59 class CoreExport Extensible : public classbase
61 /** Private data store.
62 * Holds all extensible metadata for the class.
64 ExtensibleStore extensions;
65 typedef std::map<std::string, ExtensionItem*> ExtensibleTypes;
66 static ExtensibleTypes extension_types;
69 * Get the extension items for iteraton (i.e. for metadata sync during netburst)
71 inline const ExtensibleStore& GetExtList() const { return extensions; }
72 static inline const ExtensibleTypes& GetTypeList() { return extension_types; }
73 static inline ExtensionItem* GetItem(const std::string& name)
75 ExtensibleTypes::iterator i = extension_types.find(name);
76 if (i == extension_types.end())
81 virtual ~Extensible();
83 static bool Register(ExtensionItem* item);
84 static void UnRegister(Module* module);
86 // Friend access for the protected getter/setter
87 friend class ExtensionItem;
90 /** Base class for items that are NOT synchronized between servers */
91 class CoreExport LocalExtItem : public ExtensionItem
94 LocalExtItem(const std::string& key, Module* owner);
95 virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item);
96 virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
97 virtual void free(void* item) = 0;
101 class CoreExport SimpleExtItem : public LocalExtItem
104 SimpleExtItem(const std::string& Key, Module* parent) : LocalExtItem(Key, parent)
108 inline T* get(const Extensible* container)
110 return static_cast<T*>(get_raw(container));
113 inline T* getNew(Extensible* container)
115 T* ptr = get(container);
119 set_raw(container, ptr);
124 inline void set(Extensible* container, const T& value)
126 T* ptr = new T(value);
127 T* old = static_cast<T*>(set_raw(container, ptr));
131 inline void set(Extensible* container, T* value)
133 T* old = static_cast<T*>(set_raw(container, value));
137 inline void unset(Extensible* container)
139 T* old = static_cast<T*>(unset_raw(container));
143 virtual void free(void* item)
145 delete static_cast<T*>(item);
149 class CoreExport LocalStringExt : public SimpleExtItem<std::string>
152 LocalStringExt(const std::string& key, Module* owner);
153 std::string serialize(SerializeFormat format, const Extensible* container, void* item);
156 class CoreExport LocalIntExt : public LocalExtItem
159 LocalIntExt(const std::string& key, Module* owner);
160 std::string serialize(SerializeFormat format, const Extensible* container, void* item);
161 intptr_t get(const Extensible* container);
162 intptr_t set(Extensible* container, intptr_t value);
163 void free(void* item);
166 class CoreExport StringExtItem : public ExtensionItem
169 StringExtItem(const std::string& key, Module* owner);
170 std::string* get(const Extensible* container);
171 std::string serialize(SerializeFormat format, const Extensible* container, void* item);
172 void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
173 void set(Extensible* container, const std::string& value);
174 void unset(Extensible* container);
175 void free(void* item);