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