]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/extensible.h
449c7b38a46c58fccc66822eb9f72b17092c341d
[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         virtual ~ExtensionItem();
25         /** Serialize this item into a string
26          *
27          * @param format The format to serialize to
28          * @param container The object containing this item
29          * @param item The item itself
30          */
31         virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const = 0;
32         /** Convert the string form back into an item
33          * @param format The format to serialize from (not FORMAT_USER)
34          * @param container The object that this item applies to
35          * @param value The return from a serialize() call that was run elsewhere with this key
36          */
37         virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value) = 0;
38         /** Free the item */
39         virtual void free(void* item) = 0;
40
41  protected:
42         /** Get the item from the internal map */
43         void* get_raw(const Extensible* container) const;
44         /** Set the item in the internal map; returns old value */
45         void* set_raw(Extensible* container, void* value);
46         /** Remove the item from the internal map; returns old value */
47         void* unset_raw(Extensible* container);
48 };
49
50 /** class Extensible is the parent class of many classes such as User and Channel.
51  * class Extensible implements a system which allows modules to 'extend' the class by attaching data within
52  * a map associated with the object. In this way modules can store their own custom information within user
53  * objects, channel objects and server objects, without breaking other modules (this is more sensible than using
54  * a flags variable, and each module defining bits within the flag as 'theirs' as it is less prone to conflict and
55  * supports arbitary data storage).
56  */
57 class CoreExport Extensible : public classbase
58 {
59  public:
60         typedef std::map<ExtensionItem*,void*> ExtensibleStore;
61
62         // Friend access for the protected getter/setter
63         friend class ExtensionItem;
64  private:
65         /** Private data store.
66          * Holds all extensible metadata for the class.
67          */
68         ExtensibleStore extensions;
69  public:
70         /**
71          * Get the extension items for iteraton (i.e. for metadata sync during netburst)
72          */
73         inline const ExtensibleStore& GetExtList() const { return extensions; }
74
75         virtual CullResult cull();
76         virtual ~Extensible();
77         void doUnhookExtensions(const std::vector<ExtensionItem*>& toRemove);
78 };
79
80 class CoreExport ExtensionManager
81 {
82         std::map<std::string, ExtensionItem*> types;
83  public:
84         void Register(ExtensionItem* item);
85         void BeginUnregister(Module* module, std::vector<ExtensionItem*>& list);
86         ExtensionItem* GetItem(const std::string& name);
87 };
88
89 /** Base class for items that are NOT synchronized between servers */
90 class CoreExport LocalExtItem : public ExtensionItem
91 {
92  public:
93         LocalExtItem(const std::string& key, Module* owner);
94         virtual ~LocalExtItem();
95         virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
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 SimpleExtItem : public LocalExtItem
102 {
103  public:
104         SimpleExtItem(const std::string& Key, Module* parent) : LocalExtItem(Key, parent)
105         {
106         }
107
108         virtual ~SimpleExtItem()
109         {
110         }
111
112         inline T* get(const Extensible* container) const
113         {
114                 return static_cast<T*>(get_raw(container));
115         }
116
117         inline void set(Extensible* container, const T& value)
118         {
119                 T* ptr = new T(value);
120                 T* old = static_cast<T*>(set_raw(container, ptr));
121                 delete old;
122         }
123
124         inline void set(Extensible* container, T* value)
125         {
126                 T* old = static_cast<T*>(set_raw(container, value));
127                 delete old;
128         }
129
130         inline void unset(Extensible* container)
131         {
132                 T* old = static_cast<T*>(unset_raw(container));
133                 delete old;
134         }
135
136         virtual void free(void* item)
137         {
138                 delete static_cast<T*>(item);
139         }
140 };
141
142 class CoreExport LocalStringExt : public SimpleExtItem<std::string>
143 {
144  public:
145         LocalStringExt(const std::string& key, Module* owner);
146         virtual ~LocalStringExt();
147         std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
148 };
149
150 class CoreExport LocalIntExt : public LocalExtItem
151 {
152  public:
153         LocalIntExt(const std::string& key, Module* owner);
154         virtual ~LocalIntExt();
155         std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
156         intptr_t get(const Extensible* container) const;
157         intptr_t set(Extensible* container, intptr_t value);
158         void free(void* item);
159 };
160
161 class CoreExport StringExtItem : public ExtensionItem
162 {
163  public:
164         StringExtItem(const std::string& key, Module* owner);
165         virtual ~StringExtItem();
166         std::string* get(const Extensible* container) const;
167         std::string serialize(SerializeFormat format, const Extensible* container, void* item) const;
168         void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
169         void set(Extensible* container, const std::string& value);
170         void unset(Extensible* container);
171         void free(void* item);
172 };