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
|
#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;
}
};
}
class SerializableBase;
extern std::vector<SerializableBase *> serialized_types;
extern std::list<SerializableBase *> *serialized_items;
extern void RegisterTypes();
class SerializableBase
{
public:
typedef std::map<Anope::string, Serialize::stringstream> serialized_data;
virtual ~SerializableBase() { }
virtual Anope::string serialize_name() = 0;
virtual serialized_data serialize() = 0;
virtual void alloc(serialized_data &) = 0;
};
template<typename Type> class Serializable : public SerializableBase
{
public:
static class SerializableAllocator : public SerializableBase
{
Anope::string name;
public:
SerializableAllocator()
{
}
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);
}
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);
}
Anope::string serialize_name()
{
if (this->name.empty())
throw CoreException();
return this->name;
}
serialized_data serialize()
{
throw CoreException();
}
void alloc(serialized_data &data)
{
Type::unserialize(data);
}
} Alloc;
private:
std::list<SerializableBase *>::iterator s_iter;
protected:
Serializable()
{
if (serialized_items == NULL)
serialized_items = new std::list<SerializableBase *>();
serialized_items->push_front(this);
this->s_iter = serialized_items->begin();
}
Serializable(const Serializable &)
{
serialized_items->push_front(this);
this->s_iter = serialized_items->begin();
}
~Serializable()
{
serialized_items->erase(this->s_iter);
}
public:
Anope::string serialize_name()
{
return Alloc.serialize_name();
}
void alloc(serialized_data &)
{
throw CoreException();
}
};
template<typename T> typename Serializable<T>::SerializableAllocator Serializable<T>::Alloc;
#endif // SERIALIZE_H
|