]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/extensible.h
3f59982318a2e14ce8783e872dd11eb71d2fd557
[user/henk/code/inspircd.git] / include / extensible.h
1 class Extensible;
2 class Module;
3
4 enum SerializeFormat
5 {
6         /** Shown to a human (does not need to be unserializable) */
7         FORMAT_USER,
8         /** Passed internally to this process (i.e. for /RELOADMODULE) */
9         FORMAT_INTERNAL,
10         /** Passed to other servers on the network (i.e. METADATA s2s command) */
11         FORMAT_NETWORK,
12         /** Stored on disk (i.e. permchannel database) */
13         FORMAT_PERSIST
14 };
15
16 /** Class represnting an extension of some object
17  */
18 class CoreExport ExtensionItem
19 {
20  public:
21         const std::string key;
22         Module* const owner;
23         ExtensionItem(const std::string& key, Module* owner);
24         /** Serialize this item into a string
25          *
26          * @param format The format to serialize to
27          * @param container The object containing this item
28          * @param item The item itself
29          */
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
35          */
36         virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value) = 0;
37         /** Free the item */
38         virtual void free(void* item) = 0;
39
40  protected:
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);
47 };
48
49 /** A private data store for an Extensible class */
50 typedef std::map<std::string,void*> ExtensibleStore;
51
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).
58  */
59 class CoreExport Extensible : public classbase
60 {
61         /** Private data store.
62          * Holds all extensible metadata for the class.
63          */
64         ExtensibleStore extensions;
65         typedef std::map<std::string, ExtensionItem*> ExtensibleTypes;
66         static ExtensibleTypes extension_types;
67  public:
68         /**
69          * Get the extension items for iteraton (i.e. for metadata sync during netburst)
70          */
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)
74         {
75                 ExtensibleTypes::iterator i = extension_types.find(name);
76                 if (i == extension_types.end())
77                         return NULL;
78                 return i->second;
79         }
80
81         virtual ~Extensible();
82
83         static bool Register(ExtensionItem* item);
84         static void UnRegister(Module* module);
85         
86         // Friend access for the protected getter/setter
87         friend class ExtensionItem;
88 };
89
90 /** Base class for items that are NOT synchronized between servers */
91 class CoreExport LocalExtItem : public ExtensionItem
92 {
93  public:
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;
98 };
99
100 template<typename T>
101 class CoreExport SimpleExtItem : public LocalExtItem
102 {
103  public:
104         SimpleExtItem(const std::string& Key, Module* parent) : LocalExtItem(Key, parent)
105         {
106         }
107
108         inline T* get(const Extensible* container)
109         {
110                 return static_cast<T*>(get_raw(container));
111         }
112
113         inline T* getNew(Extensible* container)
114         {
115                 T* ptr = get(container);
116                 if (!ptr)
117                 {
118                         ptr = new T;
119                         set_raw(container, ptr);
120                 }
121                 return ptr;
122         }
123
124         inline void set(Extensible* container, const T& value)
125         {
126                 T* ptr = new T(value);
127                 T* old = static_cast<T*>(set_raw(container, ptr));
128                 delete old;
129         }
130
131         inline void set(Extensible* container, T* value)
132         {
133                 T* old = static_cast<T*>(set_raw(container, value));
134                 delete old;
135         }
136
137         inline void unset(Extensible* container)
138         {
139                 T* old = static_cast<T*>(unset_raw(container));
140                 delete old;
141         }
142
143         virtual void free(void* item)
144         {
145                 delete static_cast<T*>(item);
146         }
147 };
148
149 class CoreExport LocalStringExt : public SimpleExtItem<std::string>
150 {
151  public:
152         LocalStringExt(const std::string& key, Module* owner);
153         std::string serialize(SerializeFormat format, const Extensible* container, void* item);
154 };
155
156 class CoreExport LocalIntExt : public LocalExtItem
157 {
158  public:
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);
164 };
165
166 class CoreExport StringExtItem : public ExtensionItem
167 {
168  public:
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);
176 };