-
Notifications
You must be signed in to change notification settings - Fork 0
/
Properties.h
218 lines (162 loc) · 5.42 KB
/
Properties.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
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
#ifndef PROPERTIES_H_INCLUDED
#define PROPERTIES_H_INCLUDED
#include <string>
#include <sstream>
#include <map>
#include <SFML/Network.hpp>
#include "Split.h"
class Properties
{
public:
Properties(){};
~Properties(){};
// Get & Set properties
template <class T>
void setProperty(std::string key, T value){
std::ostringstream stream;
stream << value;
m_properties[key] = stream.str();
};
template <class T>
T getProperty(std::string key){
if (m_properties.count(key) == 0) return T();
std::stringstream stream;
T value;
stream.str(m_properties[key]);
stream >> value;
return value;
};
template <class T>
T getProperty(std::string prefix, std::string key){
return getProperty<T>(prefix + "." + key);
};
template <class T>
T getProperty(std::string prefix, sf::Uint32 prefixId, std::string key){
std::ostringstream stream;
stream << prefixId;
return getProperty<T>(prefix + "." + stream.str() + "." + key);
};
sf::Uint32 getPropertiesCount() const{
return m_properties.size();
};
// Read/Write to packet
template <class T>
bool readPropertyValue(std::string key, sf::Packet& packet){
T value;
if (packet >> value){
setProperty<T>(key, value);
return true;
}
return false;
};
template <class T>
void writePropertyValue(std::string key, sf::Packet& packet){
packet << getProperty<T>(key);
};
void writeProperties(sf::Packet& packet){
packet << getPropertiesCount();
for (Properties::const_properties_iterator it = propertiesBegin(); it != propertiesEnd(); it++){
packet << it->first;
packet << it->second;
}
};
void readProperties(sf::Packet& packet){
sf::Uint32 count;
packet >> count;
for (sf::Uint32 i(0); i < count; i++){
std::string key, value;
packet >> key >> value;
setProperty(key, value);
}
};
// iterators
typedef std::map<std::string, std::string>::const_iterator const_properties_iterator;
const_properties_iterator propertiesBegin() const{ return m_properties.begin(); };
const_properties_iterator propertiesEnd() const{ return m_properties.end(); };
/////////////////////////////////
// Properties writing helpers ---
/////////////////////////////////
// Writing to sf::Packet
template <class T>
static void write(sf::Packet& packet, std::string key, T value){
write(packet, "", key, value);
};
template <class T>
static void write(sf::Packet& packet, std::string prefix, std::string key, T value){
std::ostringstream stream;
stream << value;
packet << (prefix == "") ? key : prefix + "." + key;
packet << stream.str();
};
template <class T>
static void write(sf::Packet& packet, std::string prefix, sf::Uint32 prefixId, std::string key, T value){
std::ostringstream stream;
stream << prefixId;
write(packet, prefix + "." + stream.str(), key, value);
};
// Writing to Properties object
template <class T>
static void write(Properties& properties, std::string key, T value){
write(properties, "", key, value);
};
template <class T>
static void write(Properties& properties, std::string prefix, std::string key, T value){
properties.setProperty((prefix == "") ? key : prefix + "." + key, value);
};
template <class T>
static void write(Properties& properties, std::string prefix, sf::Uint32 prefixId, std::string key, T value){
std::ostringstream stream;
stream << prefixId;
write(properties, prefix + "." + stream.str(), key, value);
};
// Importing a Properties object to an other
static void write(Properties& properties, std::string key, Properties& value){
write(properties, "", key, value);
};
static void write(Properties& properties, std::string prefix, std::string key, Properties& value){
for (Properties::const_properties_iterator it = value.propertiesBegin(); it != value.propertiesEnd(); it++){
properties.write(properties, prefix + "." + key, it->first, it->second);
}
};
static void write(Properties& properties, std::string prefix, sf::Uint32 prefixId, std::string key, Properties& value){
std::ostringstream stream;
stream << prefixId;
write(properties, prefix + "." + stream.str(), key, value);
};
// Make key string with prefix and/or prefixId
static std::string makeKey(std::string prefix, std::string key){
return prefix + "." + key;
};
static std::string makeKey(std::string prefix, sf::Uint32 prefixId, std::string key){
std::ostringstream stream;
stream << prefixId;
return prefix + "." + stream.str() + "." + key;
};
static std::string makeKey(std::string prefix, sf::Uint32 prefixId){
std::ostringstream stream;
stream << prefixId;
return prefix + "." + stream.str();
};
static std::string makeKey(sf::Uint32 prefixId, std::string key){
std::ostringstream stream;
stream << prefixId;
return stream.str() + "." + key;
};
/////////////////////////////////
// --- Properties writing helpers
/////////////////////////////////
private:
std::map<std::string, std::string> m_properties;
};
// Specifics template resolutions
template <>
inline std::string Properties::getProperty(std::string key){
if (m_properties.count(key) == 0) return std::string();
return m_properties[key];
};
template <>
inline void Properties::write(sf::Packet& packet, std::string prefix, std::string key, std::string value){
packet << (prefix == "") ? key : prefix + "." + key;
packet << value;
};
#endif // PROPERTIES_H_INCLUDED