summaryrefslogtreecommitdiff
path: root/include/serialize.h
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2012-04-23 05:08:26 -0400
committerAdam <Adam@anope.org>2012-04-23 05:08:26 -0400
commit573e49a7ead331219eb6f0d3ca9cf83e793a5c9c (patch)
treee145e04fa3d041cf92ce46da4ac790b63231059c /include/serialize.h
parent63c639e108a00d7dbb0d7ac9891684fc83a3b207 (diff)
Reworked live SQL support yet again
Diffstat (limited to 'include/serialize.h')
-rw-r--r--include/serialize.h189
1 files changed, 179 insertions, 10 deletions
diff --git a/include/serialize.h b/include/serialize.h
index 891af726f..82287d5ab 100644
--- a/include/serialize.h
+++ b/include/serialize.h
@@ -16,6 +16,7 @@
#include <sstream>
#include "anope.h"
+#include "base.h"
namespace Serialize
{
@@ -30,13 +31,13 @@ class stringstream : public std::stringstream
{
private:
Serialize::DataType type;
- bool key;
unsigned _max;
public:
stringstream();
stringstream(const stringstream &ss);
Anope::string astr() const;
+
template<typename T> std::istream &operator>>(T &val)
{
std::istringstream is(this->str());
@@ -44,23 +45,31 @@ class stringstream : public std::stringstream
return *this;
}
std::istream &operator>>(Anope::string &val);
+
+ bool operator==(const stringstream &other) const;
+ bool operator!=(const stringstream &other) const;
+
stringstream &setType(Serialize::DataType t);
Serialize::DataType getType() const;
- stringstream &setKey();
- bool getKey() const;
stringstream &setMax(unsigned m);
unsigned getMax() const;
};
+namespace Serialize
+{
+ typedef std::map<Anope::string, stringstream> Data;
+}
extern void RegisterTypes();
-class CoreExport Serializable
+class CoreExport Serializable : public virtual Base
{
private:
- static std::list<Serializable *> *serizliable_items;
+ static std::list<Serializable *> *serializable_items;
+ private:
std::list<Serializable *>::iterator s_iter;
+ Serialize::Data last_commit;
protected:
Serializable();
@@ -71,35 +80,195 @@ class CoreExport Serializable
Serializable &operator=(const Serializable &);
public:
- typedef std::map<Anope::string, stringstream> serialized_data;
+ unsigned int id;
+
+ void destroy();
+
+ void QueueUpdate();
- virtual Anope::string serialize_name() const = 0;
- virtual serialized_data serialize() = 0;
+ bool IsCached();
+ void UpdateCache();
+
+ virtual const Anope::string serialize_name() const = 0;
+ virtual Serialize::Data serialize() const = 0;
static const std::list<Serializable *> &GetItems();
};
class CoreExport SerializeType
{
- typedef void (*unserialize_func)(Serializable::serialized_data &);
+ typedef Serializable* (*unserialize_func)(Serializable *obj, Serialize::Data &);
static std::vector<Anope::string> type_order;
static Anope::map<SerializeType *> types;
Anope::string name;
unserialize_func unserialize;
+ time_t timestamp;
public:
+ std::map<unsigned int, Serializable *> objects;
+
SerializeType(const Anope::string &n, unserialize_func f);
~SerializeType();
const Anope::string &GetName();
- void Create(Serializable::serialized_data &data);
+ Serializable *Unserialize(Serializable *obj, Serialize::Data &data);
+
+ void Check();
+
+ time_t GetTimestamp() const;
+ void UpdateTimestamp();
static SerializeType *Find(const Anope::string &name);
static const std::vector<Anope::string> &GetTypeOrder();
};
+template<typename T>
+class serialize_checker
+{
+ Anope::string name;
+ T obj;
+
+ public:
+ serialize_checker(const Anope::string &n) : name(n) { }
+
+ inline const T* operator->() const
+ {
+ static SerializeType *type = SerializeType::Find(this->name);
+ if (type)
+ type->Check();
+ return &this->obj;
+ }
+ inline T* operator->()
+ {
+ static SerializeType *type = SerializeType::Find(this->name);
+ if (type)
+ type->Check();
+ return &this->obj;
+ }
+
+ inline const T& operator*() const
+ {
+ static SerializeType *type = SerializeType::Find(this->name);
+ if (type)
+ type->Check();
+ return this->obj;
+ }
+ inline T& operator*()
+ {
+ static SerializeType *type = SerializeType::Find(this->name);
+ if (type)
+ type->Check();
+ return this->obj;
+ }
+
+ inline operator const T&() const
+ {
+ static SerializeType *type = SerializeType::Find(this->name);
+ if (type)
+ type->Check();
+ return this->obj;
+ }
+ inline operator T&()
+ {
+ static SerializeType *type = SerializeType::Find(this->name);
+ if (type)
+ type->Check();
+ return this->obj;
+ }
+};
+
+#include "modules.h"
+
+template<typename T>
+class serialize_obj : public dynamic_reference_base
+{
+ protected:
+ T *ref;
+
+ public:
+ serialize_obj() : ref(NULL)
+ {
+ }
+
+ serialize_obj(T *obj) : ref(obj)
+ {
+ if (obj)
+ obj->AddReference(this);
+ }
+
+ serialize_obj(const serialize_obj<T> &other) : ref(other.ref)
+ {
+ if (*this)
+ this->ref->AddReference(this);
+ }
+
+ ~serialize_obj()
+ {
+ if (*this)
+ this->ref->DelReference(this);
+ }
+
+ virtual operator bool() const
+ {
+ if (!this->invalid)
+ return this->ref != NULL;
+ return NULL;
+ }
+
+ inline void operator=(T *newref)
+ {
+ if (*this)
+ this->ref->DelReference(this);
+
+ this->ref = newref;
+ this->invalid = false;
+
+ if (newref)
+ this->ref->AddReference(this);
+ }
+
+ virtual inline operator T*() const
+ {
+ if (!this->invalid)
+ {
+ if (this->ref)
+ {
+ FOREACH_MOD(I_OnSerializePtrAssign, OnSerializePtrAssign(this->ref));
+ }
+ return this->ref;
+ }
+ return NULL;
+ }
+
+ virtual inline T* operator*() const
+ {
+ if (!this->invalid)
+ {
+ if (this->ref)
+ {
+ FOREACH_MOD(I_OnSerializePtrAssign, OnSerializePtrAssign(this->ref));
+ }
+ return this->ref;
+ }
+ return NULL;
+ }
+
+ virtual inline T* operator->() const
+ {
+ if (!this->invalid)
+ {
+ if (this->ref)
+ {
+ FOREACH_MOD(I_OnSerializePtrAssign, OnSerializePtrAssign(this->ref));
+ }
+ return this->ref;
+ }
+ return NULL;
+ }
+};
+
#endif // SERIALIZE_H