summaryrefslogtreecommitdiff
path: root/include/serialize.h
blob: 41d79d13826c5fd3fab3d07a0b99cb9bc27edcca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef SERIALIZE_H
#define SERIALIZE_H

namespace Serialize
{
 	enum DataType
	{
		DT_TEXT,
		DT_INT
	};

	class stringstream : public std::stringstream
	{
	 private:
		DataType type;
		bool key;
		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) { }
		Anope::string astr() const { return this->str(); }
		template<typename T> std::istream &operator>>(T &val)
		{
			std::istringstream is(this->str());
			is >> val;
			return *this;
		}
		std::istream &operator>>(Anope::string &val)
		{
			val = this->str();
			return *this;
		}
		stringstream &setType(DataType t)
		{
			this->type = t;
			return *this;
		}
		DataType getType() const
		{
			return this->type;
		}
		stringstream &setKey()
		{
			this->key = true;
			return *this;
		}
		bool getKey() const
		{
			return this->key;
		}
		stringstream &setMax(unsigned m)
		{
			this->_max = m;
			return *this;
		}
		unsigned getMax() const
		{
			return this->_max;
		}
	};
}

extern void RegisterTypes();

class CoreExport Serializable
{
 private:
	static std::list<Serializable *> *serizliable_items;

	Anope::string serizliable_name;
	std::list<Serializable *>::iterator s_iter;

	Serializable()
	{
		throw CoreException();
	}

 protected:
	Serializable(const Anope::string &n) : serizliable_name(n)
	{
		if (serizliable_items == NULL)
			serizliable_items = new std::list<Serializable *>();
		serizliable_items->push_front(this);
		this->s_iter = serizliable_items->begin();
	}

	Serializable(const Serializable &)
	{
		serizliable_items->push_front(this);
		this->s_iter = serizliable_items->begin();
	}

	virtual ~Serializable()
	{
		serizliable_items->erase(this->s_iter);
	}

	Serializable &operator=(const Serializable &)
	{
		return *this;
	}

 public:
	typedef std::map<Anope::string, Serialize::stringstream> serialized_data;

	const Anope::string &GetSerializeName()
	{
		return this->serizliable_name;
	}

	virtual serialized_data serialize() = 0;

	static const std::list<Serializable *> &GetItems()
	{
		return *serizliable_items;
	}
};

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)
	{
		type_order.push_back(this->name);
		types[this->name] = this;
	}

	~SerializeType()
	{
		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);
	}

	const Anope::string &GetName()
	{
		return this->name;
	}

	void Create(Serializable::serialized_data &data)
	{
		this->unserialize(data);
	}

	static SerializeType *Find(const Anope::string &name)
	{
		Anope::map<SerializeType *>::iterator it = types.find(name);
		if (it != types.end())
			return it->second;
		return NULL;
	}

	static const std::vector<Anope::string> &GetTypeOrder()
	{
		return type_order;
	}
};

#endif // SERIALIZE_H