/
record.h
292 lines (253 loc) · 9.09 KB
/
record.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
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
285
286
287
288
289
290
291
292
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2009-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBDENG2_RECORD_H
#define LIBDENG2_RECORD_H
#include "../ISerializable"
#include "../String"
#include "../Variable"
#include "../Value"
#include "../Audience"
#include "../Log"
#include <map>
#include <list>
namespace de
{
class Function;
/**
* A set of variables. A record may have any number of subrecords.
* Note that the members of a record do not have an order.
*
* @see http://en.wikipedia.org/wiki/Record_(computer_science)
*
* @ingroup data
*/
class DENG2_PUBLIC Record : public ISerializable, public LogEntry::Arg::Base
{
public:
/// Unknown variable name was given. @ingroup errors
DENG2_ERROR(NotFoundError);
/// All variables and subrecords in the record must have a name. @ingroup errors
DENG2_ERROR(UnnamedError);
typedef std::map<String, Variable*> Members;
typedef std::map<String, Record*> Subrecords;
typedef std::pair<String, String> KeyValue;
typedef std::list<KeyValue> List;
DENG2_DEFINE_AUDIENCE(Deletion, void recordBeingDeleted(Record& record))
public:
Record();
/**
* Constructs a copy of another record.
*
* @param other Record to copy.
*/
Record(const Record& other);
virtual ~Record();
/**
* Deletes all the variables in the record.
*/
void clear();
/**
* Determines if the record contains a variable or a subrecord named @a variableName.
*/
bool has(const String& name) const;
/**
* Determines if the record contains a variable named @a variableName.
*/
bool hasMember(const String& variableName) const;
/**
* Determines if the record contains a subrecord named @a subrecordName.
*/
bool hasSubrecord(const String& subrecordName) const;
/**
* Adds a new variable to the record.
*
* @param variable Variable to add. Record gets ownership.
* The variable must have a name.
*
* @return @a variable, for convenience.
*/
Variable& add(Variable* variable);
/**
* Removes a variable from the record.
*
* @param variable Variable owned by the record.
*
* @return Caller gets ownership of the removed variable.
*/
Variable* remove(Variable& variable);
/**
* Adds a number variable to the record. The variable is set up to only accept
* number values.
*
* @param variableName Name of the variable.
* @param number Value of the variable.
*
* @return The number variable.
*/
Variable& addNumber(const String& variableName, const Value::Number& number);
/**
* Adds a text variable to the record. The variable is set up to only accept
* text values.
*
* @param variableName Name of the variable.
* @param text Value of the variable.
*
* @return The text variable.
*/
Variable& addText(const String& variableName, const Value::Text& text);
/**
* Adds an array variable to the record. The variable is set up to only accept
* array values.
*
* @param variableName Name of the variable.
*
* @return The array variable.
*/
Variable& addArray(const String& variableName);
/**
* Adds a dictionary variable to the record. The variable is set up to only accept
* dictionary values.
*
* @param variableName Name of the variable.
*
* @return The dictionary variable.
*/
Variable& addDictionary(const String& variableName);
/**
* Adds a block variable to the record. The variable is set up to only accept
* block values.
*
* @param variableName Name of the variable.
*
* @return The block variable.
*/
Variable& addBlock(const String& variableName);
/**
* Adds a new subrecord to the record.
*
* @param name Name to use for the subrecord. This must be a valid variable name.
* @param subrecord Record to add. This record gets ownership
* of the subrecord. The variable must have a name.
*
* @return @a subrecord, for convenience.
*/
Record& add(const String& name, Record* subrecord);
/**
* Adds a new empty subrecord to the record.
*
* @param name Name to use for the subrecord. This must be a valid variable name.
*
* @return The new subrecord.
*/
Record& addRecord(const String& name);
/**
* Removes a subrecord from the record.
*
* @param name Name of subrecord owned by this record.
*
* @return Caller gets ownership of the removed record.
*/
Record* remove(const String& name);
/**
* Looks up a variable in the record. Variables in subrecords can be accessed
* using the member notation: <code>subrecord-name.variable-name</code>
*
* @param name Variable name.
*
* @return Variable.
*/
Variable& operator [] (const String& name);
/**
* Looks up a variable in the record. Variables in subrecords can be accessed
* using the member notation: <code>subrecord-name.variable-name</code>
*
* @param name Variable name.
*
* @return Variable (non-modifiable).
*/
const Variable& operator [] (const String& name) const;
/**
* Looks up a subrecord in the record.
*
* @param name Name of the subrecord.
*
* @return Subrecord.
*/
Record& subrecord(const String& name);
/**
* Looks up a subrecord in the record.
*
* @param name Name of the subrecord.
*
* @return Subrecord (non-modifiable).
*/
const Record& subrecord(const String& name) const;
/**
* Returns a non-modifiable map of the members.
*/
const Members& members() const { return _members; }
/**
* Returns a non-modifiable map of the subrecords.
*/
const Subrecords& subrecords() const { return _subrecords; }
/**
* Creates a text representation of the record. Each variable name is
* prefixed with @a prefix.
*
* @param prefix Prefix for each variable name.
* @param lines NULL (used for recursion into subrecords).
*
* @return Text representation.
*/
String asText(const String& prefix, List* lines) const;
/**
* Convenience template for getting the value of a variable in a
* specific type.
*
* @param name Name of variable.
*
* @return Value cast to a specific value type.
*/
template <typename ValueType>
const ValueType& value(const String& name) const {
return (*this)[name].value<ValueType>();
}
/**
* Convenience method for finding the Function referenced by a member.
*
* @param name Name of member.
*
* @return The function, or @c NULL if the member does not exist or
* has some other value than FunctionValue.
*/
const Function* function(const String& name) const;
// Implements ISerializable.
void operator >> (Writer& to) const;
void operator << (Reader& from);
// Implements LogEntry::Arg::Base.
LogEntry::Arg::Type logEntryArgType() const { return LogEntry::Arg::STRING; }
String asText() const { return asText("", 0); }
private:
Members _members;
Subrecords _subrecords;
};
/// Converts the record into a human-readable text representation.
DENG2_PUBLIC QTextStream& operator << (QTextStream& os, const Record& record);
}
#endif /* LIBDENG2_RECORD_H */