Anope IRC Services  Version 2.0
serialize.h
Go to the documentation of this file.
1 /*
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  */
12 
13 #ifndef SERIALIZE_H
14 #define SERIALIZE_H
15 
16 #include <sstream>
17 
18 #include "anope.h"
19 #include "base.h"
20 
21 namespace Serialize
22 {
23  class Data
24  {
25  public:
26  enum Type
27  {
30  };
31 
32  virtual ~Data() { }
33 
34  virtual std::iostream& operator[](const Anope::string &key) = 0;
35  virtual std::set<Anope::string> KeySet() const { throw CoreException("Not supported"); }
36  virtual size_t Hash() const { throw CoreException("Not supported"); }
37 
38  virtual void SetType(const Anope::string &key, Type t) { }
39  virtual Type GetType(const Anope::string &key) const { return DT_TEXT; }
40  };
41 
42  extern void RegisterTypes();
43  extern void CheckTypes();
44 
45  class Type;
46  template<typename T> class Checker;
47  template<typename T> class Reference;
48 }
49 
54 class CoreExport Serializable : public virtual Base
55 {
56  private:
57  /* A list of every serializable item in Anope.
58  * Some of these are static and constructed at runtime,
59  * so this list must be on the heap, as it is not always
60  * constructed before other objects are if it isn't.
61  */
62  static std::list<Serializable *> *SerializableItems;
63  /* The type of item this object is */
65  private:
66  /* Iterator into serializable_items */
67  std::list<Serializable *>::iterator s_iter;
68  /* The hash of the last serialized form of this object commited to the database */
69  size_t last_commit;
70  /* The last time this object was commited to the database */
72 
73  protected:
74  Serializable(const Anope::string &serialize_type);
75  Serializable(const Serializable &);
76 
77  Serializable &operator=(const Serializable &);
78 
79  public:
80  virtual ~Serializable();
81 
82  /* Unique ID (per type, not globally) for this object */
83  uint64_t id;
84 
85  /* Only used by redis, to ignore updates */
86  unsigned short redis_ignore;
87 
90  void QueueUpdate();
91 
92  bool IsCached(Serialize::Data &);
93  void UpdateCache(Serialize::Data &);
94 
95  bool IsTSCached();
96  void UpdateTS();
97 
101  Serialize::Type* GetSerializableType() const { return this->s_type; }
102 
103  virtual void Serialize(Serialize::Data &data) const = 0;
104 
105  static const std::list<Serializable *> &GetItems();
106 };
107 
108 /* A serializable type. There should be one of these classes for each type
109  * of class that inherits from Serialiable. Used for unserializing objects
110  * of this type, as it requires a function pointer to a static member function.
111  */
113 {
114  typedef Serializable* (*unserialize_func)(Serializable *obj, Serialize::Data &);
115 
116  static std::vector<Anope::string> TypeOrder;
117  static std::map<Anope::string, Serialize::Type *> Types;
118 
119  /* The name of this type, should be a class name */
121  unserialize_func unserialize;
122  /* Owner of this type. Used for placing objects of this type in separate databases
123  * based on what module, if any, owns it.
124  */
126 
127  /* The timesatmp for this type. All objects of this type are as up to date as
128  * this timestamp. if curtime == timestamp then we have the most up to date
129  * version of every object of this type.
130  */
131  time_t timestamp;
132 
133  public:
134  /* Map of Serializable::id to Serializable objects */
135  std::map<uint64_t, Serializable *> objects;
136 
142  Type(const Anope::string &n, unserialize_func f, Module *owner = NULL);
143  ~Type();
144 
148  const Anope::string &GetName() { return this->name; }
149 
156  Serializable *Unserialize(Serializable *obj, Serialize::Data &data);
157 
160  void Check();
161 
165  time_t GetTimestamp() const;
166 
169  void UpdateTimestamp();
170 
171  Module* GetOwner() const { return this->owner; }
172 
173  static Serialize::Type *Find(const Anope::string &name);
174 
175  static const std::vector<Anope::string> &GetTypeOrder();
176 
177  static const std::map<Anope::string, Serialize::Type *>& GetTypes();
178 };
179 
186 template<typename T>
187 class Serialize::Checker
188 {
190  T obj;
192 
193  inline void Check() const
194  {
195  if (!type)
196  type = Serialize::Type::Find(this->name);
197  if (type)
198  type->Check();
199  }
200 
201  public:
202  Checker(const Anope::string &n) : name(n), type(NULL) { }
203 
204  inline const T* operator->() const
205  {
206  this->Check();
207  return &this->obj;
208  }
209  inline T* operator->()
210  {
211  this->Check();
212  return &this->obj;
213  }
214 
215  inline const T& operator*() const
216  {
217  this->Check();
218  return this->obj;
219  }
220  inline T& operator*()
221  {
222  this->Check();
223  return this->obj;
224  }
225 
226  inline operator const T&() const
227  {
228  this->Check();
229  return this->obj;
230  }
231  inline operator T&()
232  {
233  this->Check();
234  return this->obj;
235  }
236 };
237 
244 template<typename T>
245 class Serialize::Reference : public ReferenceBase
246 {
247  protected:
248  T *ref;
249 
250  public:
251  Reference() : ref(NULL)
252  {
253  }
254 
255  Reference(T *obj) : ref(obj)
256  {
257  if (obj)
258  obj->AddReference(this);
259  }
260 
261  Reference(const Reference<T> &other) : ReferenceBase(other), ref(other.ref)
262  {
263  if (ref && !invalid)
264  this->ref->AddReference(this);
265  }
266 
268  {
269  if (ref && !invalid)
270  this->ref->DelReference(this);
271  }
272 
273  inline Reference<T>& operator=(const Reference<T> &other)
274  {
275  if (this != &other)
276  {
277  if (ref && !invalid)
278  this->ref->DelReference(this);
279 
280  this->ref = other.ref;
281  this->invalid = other.invalid;
282 
283  if (ref && !invalid)
284  this->ref->AddReference(this);
285  }
286  return *this;
287  }
288 
289  inline operator bool() const
290  {
291  if (!this->invalid)
292  return this->ref != NULL;
293  return false;
294  }
295 
296  inline operator T*() const
297  {
298  if (!this->invalid)
299  {
300  if (this->ref)
301  // This can invalidate me
302  this->ref->QueueUpdate();
303  if (!this->invalid)
304  return this->ref;
305  }
306  return NULL;
307  }
308 
309  inline T* operator*() const
310  {
311  if (!this->invalid)
312  {
313  if (this->ref)
314  // This can invalidate me
315  this->ref->QueueUpdate();
316  if (!this->invalid)
317  return this->ref;
318  }
319  return NULL;
320  }
321 
322  inline T* operator->() const
323  {
324  if (!this->invalid)
325  {
326  if (this->ref)
327  // This can invalidate me
328  this->ref->QueueUpdate();
329  if (!this->invalid)
330  return this->ref;
331  }
332  return NULL;
333  }
334 };
335 
336 #endif // SERIALIZE_H
const Anope::string & GetName()
Definition: serialize.h:148
Definition: base.h:17
static std::map< Anope::string, Serialize::Type * > Types
Definition: serialize.h:117
const T * operator->() const
Definition: serialize.h:204
std::list< Serializable * >::iterator s_iter
Definition: serialize.h:67
Module * GetOwner() const
Definition: serialize.h:171
virtual std::iostream & operator[](const Anope::string &key)=0
Reference< T > & operator=(const Reference< T > &other)
Definition: serialize.h:273
virtual Type GetType(const Anope::string &key) const
Definition: serialize.h:39
void CheckTypes()
Definition: serialize.cpp:37
void QueueUpdate()
Definition: serialize.cpp:83
const T & operator*() const
Definition: serialize.h:215
virtual std::set< Anope::string > KeySet() const
Definition: serialize.h:35
Module * owner
Definition: serialize.h:125
virtual void SetType(const Anope::string &key, Type t)
Definition: serialize.h:38
uint64_t id
Definition: serialize.h:83
void RegisterTypes()
Definition: serialize.cpp:30
Serialize::Type * type
Definition: serialize.h:191
virtual size_t Hash() const
Definition: serialize.h:36
Checker(const Anope::string &n)
Definition: serialize.h:202
Anope::string name
Definition: serialize.h:189
Anope::string name
Definition: serialize.h:120
unserialize_func unserialize
Definition: serialize.h:121
Serialize::Type * s_type
Definition: serialize.h:64
time_t timestamp
Definition: serialize.h:131
Reference(const Reference< T > &other)
Definition: serialize.h:261
std::map< uint64_t, Serializable * > objects
Definition: serialize.h:135
void Check() const
Definition: serialize.h:193
size_t last_commit
Definition: serialize.h:69
#define CoreExport
Definition: services.h:62
static std::vector< Anope::string > TypeOrder
Definition: serialize.h:116
virtual ~Data()
Definition: serialize.h:32
time_t last_commit_time
Definition: serialize.h:71
Anope::string name
Definition: access.cpp:22
static std::list< Serializable * > * SerializableItems
Definition: serialize.h:62
void DelReference(ReferenceBase *r)
Definition: base.cpp:38
T * operator->() const
Definition: serialize.h:322
Serialize::Type * GetSerializableType() const
Definition: serialize.h:101
bool invalid
Definition: base.h:37
Type(const Anope::string &n, unserialize_func f, Module *owner=NULL)
T * operator*() const
Definition: serialize.h:309
unsigned short redis_ignore
Definition: serialize.h:86