summaryrefslogtreecommitdiff
path: root/include/serialize.h
blob: 891af726fe00c5cabb52e7efe66e6376ac820d47 (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
/*
 *
 * (C) 2003-2012 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church.
 *
 */

#ifndef SERIALIZE_H
#define SERIALIZE_H

#include <sstream>

#include "anope.h"

namespace Serialize
{
 	enum DataType
	{
		DT_TEXT,
		DT_INT
	};
}

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());
		is >> val;
		return *this;
	}
	std::istream &operator>>(Anope::string &val);
	stringstream &setType(Serialize::DataType t);
	Serialize::DataType getType() const;
	stringstream &setKey();
	bool getKey() const;
	stringstream &setMax(unsigned m);
	unsigned getMax() const;
};


extern void RegisterTypes();

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

	std::list<Serializable *>::iterator s_iter;

 protected:
	Serializable();
	Serializable(const Serializable &);

	virtual ~Serializable();

	Serializable &operator=(const Serializable &);

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

	virtual Anope::string serialize_name() const = 0;
	virtual serialized_data serialize() = 0;

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

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

	const Anope::string &GetName();

	void Create(Serializable::serialized_data &data);

	static SerializeType *Find(const Anope::string &name);

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

#endif // SERIALIZE_H