blob: 53bd096dec06d50cb771fdffbbc4eaa72c5ce3e8 (
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
*
* (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"
#include "base.h"
namespace Serialize
{
enum DataType
{
DT_TEXT,
DT_INT
};
}
class CoreExport stringstream : public std::stringstream
{
private:
Serialize::DataType type;
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);
bool operator==(const stringstream &other) const;
bool operator!=(const stringstream &other) const;
stringstream &setType(Serialize::DataType t);
Serialize::DataType getType() const;
stringstream &setMax(unsigned m);
unsigned getMax() const;
};
namespace Serialize
{
typedef std::map<Anope::string, stringstream> Data;
}
extern void RegisterTypes();
class SerializeType;
class CoreExport Serializable : public virtual Base
{
private:
static std::list<Serializable *> *serializable_items;
SerializeType *s_type;
private:
std::list<Serializable *>::iterator s_iter; // Iterator into serializable_items
Serialize::Data last_commit;
time_t last_commit_time;
Serializable();
protected:
Serializable(const Anope::string &serialize_type);
Serializable(const Serializable &);
virtual ~Serializable();
Serializable &operator=(const Serializable &);
public:
unsigned int id;
void destroy();
void QueueUpdate();
bool IsCached();
void UpdateCache();
bool IsTSCached();
void UpdateTS();
SerializeType* GetSerializableType() const;
virtual Serialize::Data serialize() const = 0;
static const std::list<Serializable *> &GetItems();
};
class CoreExport SerializeType
{
typedef Serializable* (*unserialize_func)(Serializable *obj, Serialize::Data &);
static std::vector<Anope::string> type_order;
static std::map<Anope::string, SerializeType *> types;
Anope::string name;
unserialize_func unserialize;
Module *owner;
time_t timestamp;
public:
std::map<unsigned int, Serializable *> objects;
/** Creates a new serializable type
* @param n Type name
* @param f Func to unserialize objects
* @param owner Owner of this type. Leave NULL for the core.
*/
SerializeType(const Anope::string &n, unserialize_func f, Module *owner = NULL);
~SerializeType();
const Anope::string &GetName();
Serializable *Unserialize(Serializable *obj, Serialize::Data &data);
void Check();
time_t GetTimestamp() const;
void UpdateTimestamp();
Module* GetOwner() const;
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;
}
};
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);
}
inline operator bool() const
{
if (!this->invalid)
return this->ref != NULL;
return false;
}
inline void operator=(T *newref)
{
if (*this)
this->ref->DelReference(this);
this->ref = newref;
this->invalid = false;
if (newref)
this->ref->AddReference(this);
}
inline operator T*() const
{
if (!this->invalid)
{
if (this->ref)
this->ref->QueueUpdate();
return this->ref;
}
return NULL;
}
inline T* operator*() const
{
if (!this->invalid)
{
if (this->ref)
this->ref->QueueUpdate();
return this->ref;
}
return NULL;
}
inline T* operator->() const
{
if (!this->invalid)
{
if (this->ref)
this->ref->QueueUpdate();
return this->ref;
}
return NULL;
}
};
#endif // SERIALIZE_H
|