summaryrefslogtreecommitdiff
path: root/include/serialize.h
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2011-11-08 17:29:16 -0500
committerAdam <Adam@anope.org>2011-11-08 17:29:16 -0500
commitb5ff856f47d8e54d12c568462a06351633c29610 (patch)
treea4e2f96c59ee49aa5e6cacdfd30db6155151ad36 /include/serialize.h
parent97b9055f92f21cd91af44a3d5dacce0024536cff (diff)
Windows
Diffstat (limited to 'include/serialize.h')
-rw-r--r--include/serialize.h149
1 files changed, 75 insertions, 74 deletions
diff --git a/include/serialize.h b/include/serialize.h
index 7f12cf176..8bd26bfda 100644
--- a/include/serialize.h
+++ b/include/serialize.h
@@ -14,11 +14,11 @@ namespace Serialize
private:
DataType type;
bool key;
- unsigned max;
+ unsigned _max;
public:
- stringstream() : std::stringstream(), type(DT_TEXT), key(false), max(0) { }
- stringstream(const stringstream &ss) : std::stringstream(ss.str()), type(DT_TEXT), key(false), max(0) { }
+ stringstream() : std::stringstream(), type(DT_TEXT), key(false), _max(0) { }
+ stringstream(const stringstream &ss) : std::stringstream(ss.str()), type(DT_TEXT), key(false), _max(0) { }
Anope::string astr() const { return this->str(); }
template<typename T> std::istream &operator>>(T &val)
{
@@ -51,116 +51,117 @@ namespace Serialize
}
stringstream &setMax(unsigned m)
{
- this->max = m;
+ this->_max = m;
return *this;
}
unsigned getMax() const
{
- return this->max;
+ return this->_max;
}
};
}
-class SerializableBase;
-
-extern std::vector<SerializableBase *> serialized_types;
-extern std::list<SerializableBase *> *serialized_items;
extern void RegisterTypes();
-class SerializableBase
+class CoreExport Serializable
{
- public:
- typedef std::map<Anope::string, Serialize::stringstream> serialized_data;
+ private:
+ static std::list<Serializable *> serizliable_items;
- virtual ~SerializableBase() { }
- virtual Anope::string serialize_name() = 0;
- virtual serialized_data serialize() = 0;
- virtual void alloc(serialized_data &) = 0;
-};
+ Anope::string serizliable_name;
+ std::list<Serializable *>::iterator s_iter;
-template<typename Type> class Serializable : public SerializableBase
-{
- public:
- static class SerializableAllocator : public SerializableBase
+ Serializable()
{
- Anope::string name;
+ throw CoreException();
+ }
- public:
- SerializableAllocator()
- {
- }
+ protected:
+ Serializable(const Anope::string &n) : serizliable_name(n)
+ {
+ serizliable_items.push_front(this);
+ this->s_iter = serizliable_items.begin();
+ }
- void Register(const Anope::string &n, int pos = -1)
- {
- this->name = n;
- serialized_types.insert(serialized_types.begin() + (pos < 0 ? serialized_types.size() : pos), this);
- }
+ Serializable(const Serializable &)
+ {
+ serizliable_items.push_front(this);
+ this->s_iter = serizliable_items.begin();
+ }
- void Unregister()
- {
- std::vector<SerializableBase *>::iterator it = std::find(serialized_types.begin(), serialized_types.end(), this);
- if (it != serialized_types.end())
- serialized_types.erase(it);
- }
+ virtual ~Serializable()
+ {
+ serizliable_items.erase(this->s_iter);
+ }
- Anope::string serialize_name()
- {
- if (this->name.empty())
- throw CoreException();
- return this->name;
- }
+ Serializable &operator=(const Serializable &)
+ {
+ return *this;
+ }
- serialized_data serialize()
- {
- throw CoreException();
- }
+ public:
+ typedef std::map<Anope::string, Serialize::stringstream> serialized_data;
- void alloc(serialized_data &data)
- {
- Type::unserialize(data);
- }
- } Alloc;
+ const Anope::string &GetSerializeName()
+ {
+ return this->serizliable_name;
+ }
- private:
- std::list<SerializableBase *>::iterator s_iter;
+ virtual serialized_data serialize() = 0;
- protected:
- Serializable()
+ static const std::list<Serializable *> &GetItems()
{
- if (serialized_items == NULL)
- serialized_items = new std::list<SerializableBase *>();
- serialized_items->push_front(this);
- this->s_iter = serialized_items->begin();
+ return serizliable_items;
}
+};
- Serializable(const Serializable &)
+class CoreExport SerializeType
+{
+ typedef void (*unserialize_func)(Serializable::serialized_data &);
+
+ static std::vector<Anope::string> type_order;
+ static Anope::map<SerializeType *> types;
+
+ Anope::string name;
+ unserialize_func unserialize;
+
+ public:
+ SerializeType(const Anope::string &n, unserialize_func f) : name(n), unserialize(f)
{
- serialized_items->push_front(this);
- this->s_iter = serialized_items->begin();
+ type_order.push_back(this->name);
+ types[this->name] = this;
}
- ~Serializable()
+ ~SerializeType()
{
- serialized_items->erase(this->s_iter);
+ std::vector<Anope::string>::iterator it = std::find(type_order.begin(), type_order.end(), this->name);
+ if (it != type_order.end())
+ type_order.erase(it);
+ types.erase(this->name);
}
- Serializable &operator=(const Serializable &)
+ const Anope::string &GetName()
{
- return *this;
+ return this->name;
}
- public:
- Anope::string serialize_name()
+ void Create(Serializable::serialized_data &data)
{
- return Alloc.serialize_name();
+ this->unserialize(data);
}
- void alloc(serialized_data &)
+ static SerializeType *Find(const Anope::string &name)
{
- throw CoreException();
+ Anope::map<SerializeType *>::iterator it = types.find(name);
+ if (it != types.end())
+ return it->second;
+ return NULL;
}
-};
-template<typename T> typename Serializable<T>::SerializableAllocator Serializable<T>::Alloc;
+ static const std::vector<Anope::string> &GetTypeOrder()
+ {
+ return type_order;
+ }
+};
#endif // SERIALIZE_H