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