forked from pioneerspacesim/pioneer
/
PersistSystemData.h
45 lines (41 loc) · 1.21 KB
/
PersistSystemData.h
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
// Copyright © 2008-2012 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#ifndef _PERSISTSYSTEMDATA_H
#define _PERSISTSYSTEMDATA_H
#include "Serializer.h"
#include "galaxy/SystemPath.h"
#include <map>
template <typename T>
class PersistSystemData {
public:
void Clear() {
m_dict.clear();
}
T Get(const SystemPath &path, T defaultValIfNotExists) const {
typename std::map<SystemPath, T>::const_iterator i = m_dict.find(path.SystemOnly());
if (i == m_dict.end()) return defaultValIfNotExists;
else return (*i).second;
}
void Set(const SystemPath &path, T val) {
m_dict[path.SystemOnly()] = val;
}
void Serialize(Serializer::Writer &wr) const {
wr.Int32(m_dict.size());
for (typename std::map<SystemPath, T>::const_iterator i = m_dict.begin(); i != m_dict.end(); ++i) {
(*i).first.Serialize(wr);
wr.Auto((*i).second);
}
}
static void Unserialize(Serializer::Reader &rd, PersistSystemData<T> *pd) {
int num = rd.Int32();
while (num-- > 0) {
SystemPath path = SystemPath::Unserialize(rd);
T val;
rd.Auto(&val);
pd->m_dict[path] = val;
}
}
private:
std::map<SystemPath, T> m_dict;
};
#endif /* _PERSISTSYSTEMDATA_H */