-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDict.h
321 lines (255 loc) · 10.9 KB
/
Dict.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
===========================================================================
Doom 3 GPL Source Code
Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
Doom 3 Source Code 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 3 of the License, or
(at your option) any later version.
Doom 3 Source Code 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 Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#ifndef __DICT_H__
#define __DICT_H__
#include "idlib/containers/StrPool.h"
#include "idlib/math/Angles.h"
#include "idlib/math/Matrix.h"
#include "idlib/Str.h"
#include "idlib/Parser.h"
/*
===============================================================================
Key/value dictionary
This is a dictionary class that tracks an arbitrary number of key / value
pair combinations. It is used for map entity spawning, GUI state management,
and other things.
Keys are compared case-insensitive.
Does not allocate memory until the first key/value pair is added.
===============================================================================
*/
class idKeyValue {
friend class idDict;
public:
const idStr & GetKey( void ) const { return *key; }
const idStr & GetValue( void ) const { return *value; }
size_t Allocated( void ) const { return key->Allocated() + value->Allocated(); }
size_t Size( void ) const { return sizeof( *this ) + key->Size() + value->Size(); }
bool operator==( const idKeyValue &kv ) const { return ( key == kv.key && value == kv.value ); }
private:
const idPoolStr * key;
const idPoolStr * value;
};
class idDict {
public:
idDict( void );
idDict( const idDict &other ); // allow declaration with assignment
~idDict( void );
// set the granularity for the index
void SetGranularity( int granularity );
// set hash size
void SetHashSize( int hashSize );
// clear existing key/value pairs and copy all key/value pairs from other
idDict & operator=( const idDict &other );
// copy from other while leaving existing key/value pairs in place
void Copy( const idDict &other );
// clear existing key/value pairs and transfer key/value pairs from other
void TransferKeyValues( idDict &other );
// parse dict from parser
bool Parse( idParser &parser );
// copy key/value pairs from other dict not present in this dict
void SetDefaults( const idDict *dict );
// clear dict freeing up memory
void Clear( void );
// print the dict
void Print() const;
size_t Allocated( void ) const;
size_t Size( void ) const { return sizeof( *this ) + Allocated(); }
void Set( const char *key, const char *value );
void SetFloat( const char *key, float val );
void SetInt( const char *key, int val );
void SetBool( const char *key, bool val );
void SetVector( const char *key, const idVec3 &val );
void SetVec2( const char *key, const idVec2 &val );
void SetVec4( const char *key, const idVec4 &val );
void SetAngles( const char *key, const idAngles &val );
void SetMatrix( const char *key, const idMat3 &val );
// these return default values of 0.0, 0 and false
const char * GetString( const char *key, const char *defaultString = "" ) const;
float GetFloat( const char *key, const char *defaultString = "0" ) const;
int GetInt( const char *key, const char *defaultString = "0" ) const;
bool GetBool( const char *key, const char *defaultString = "0" ) const;
idVec3 GetVector( const char *key, const char *defaultString = NULL ) const;
idVec2 GetVec2( const char *key, const char *defaultString = NULL ) const;
idVec4 GetVec4( const char *key, const char *defaultString = NULL ) const;
idAngles GetAngles( const char *key, const char *defaultString = NULL ) const;
idMat3 GetMatrix( const char *key, const char *defaultString = NULL ) const;
bool GetBool( const char *key, const bool defaultBool ) const; //added for LM
bool GetString( const char *key, const char *defaultString, const char **out ) const;
bool GetString( const char *key, const char *defaultString, idStr &out ) const;
bool GetFloat( const char *key, const char *defaultString, float &out ) const;
bool GetInt( const char *key, const char *defaultString, int &out ) const;
bool GetBool( const char *key, const char *defaultString, bool &out ) const;
bool GetVector( const char *key, const char *defaultString, idVec3 &out ) const;
bool GetVec2( const char *key, const char *defaultString, idVec2 &out ) const;
bool GetVec4( const char *key, const char *defaultString, idVec4 &out ) const;
bool GetAngles( const char *key, const char *defaultString, idAngles &out ) const;
bool GetMatrix( const char *key, const char *defaultString, idMat3 &out ) const;
bool GetBool( const char *key, const bool defaultBool, bool &out ) const; //added for LM
int GetNumKeyVals( void ) const;
const idKeyValue * GetKeyVal( int index ) const;
// returns the key/value pair with the given key
// returns NULL if the key/value pair does not exist
const idKeyValue * FindKey( const char *key ) const;
// returns the index to the key/value pair with the given key
// returns -1 if the key/value pair does not exist
int FindKeyIndex( const char *key ) const;
// delete the key/value pair with the given key
void Delete( const char *key );
// finds the next key/value pair with the given key prefix.
// lastMatch can be used to do additional searches past the first match.
const idKeyValue * MatchPrefix( const char *prefix, const idKeyValue *lastMatch = NULL ) const;
// randomly chooses one of the key/value pairs with the given key prefix and returns it's value
const char * RandomPrefix( const char *prefix, idRandom &random ) const;
void WriteToFileHandle( idFile *f ) const;
void ReadFromFileHandle( idFile *f );
// returns a unique checksum for this dictionary's content
int Checksum( void ) const;
static void Init( void );
static void Shutdown( void );
static void ShowMemoryUsage_f( const idCmdArgs &args );
static void ListKeys_f( const idCmdArgs &args );
static void ListValues_f( const idCmdArgs &args );
private:
idList<idKeyValue> args;
idHashIndex argHash;
static idStrPool globalKeys;
static idStrPool globalValues;
};
ID_INLINE idDict::idDict( void ) {
args.SetGranularity( 16 );
argHash.SetGranularity( 16 );
argHash.Clear( 128, 16 );
}
ID_INLINE idDict::idDict( const idDict &other ) {
*this = other;
}
ID_INLINE idDict::~idDict( void ) {
Clear();
}
ID_INLINE void idDict::SetGranularity( int granularity ) {
args.SetGranularity( granularity );
argHash.SetGranularity( granularity );
}
ID_INLINE void idDict::SetHashSize( int hashSize ) {
if ( args.Num() == 0 ) {
argHash.Clear( hashSize, 16 );
}
}
ID_INLINE void idDict::SetFloat( const char *key, float val ) {
Set( key, va( "%f", val ) );
}
ID_INLINE void idDict::SetInt( const char *key, int val ) {
Set( key, va( "%i", val ) );
}
ID_INLINE void idDict::SetBool( const char *key, bool val ) {
Set( key, va( "%i", val ) );
}
ID_INLINE void idDict::SetVector( const char *key, const idVec3 &val ) {
Set( key, val.ToString() );
}
ID_INLINE void idDict::SetVec4( const char *key, const idVec4 &val ) {
Set( key, val.ToString() );
}
ID_INLINE void idDict::SetVec2( const char *key, const idVec2 &val ) {
Set( key, val.ToString() );
}
ID_INLINE void idDict::SetAngles( const char *key, const idAngles &val ) {
Set( key, val.ToString() );
}
ID_INLINE void idDict::SetMatrix( const char *key, const idMat3 &val ) {
Set( key, val.ToString() );
}
ID_INLINE bool idDict::GetString( const char *key, const char *defaultString, const char **out ) const {
const idKeyValue *kv = FindKey( key );
if ( kv ) {
*out = kv->GetValue();
return true;
}
*out = defaultString;
return false;
}
ID_INLINE bool idDict::GetString( const char *key, const char *defaultString, idStr &out ) const {
const idKeyValue *kv = FindKey( key );
if ( kv ) {
out = kv->GetValue();
return true;
}
out = defaultString;
return false;
}
ID_INLINE const char *idDict::GetString( const char *key, const char *defaultString ) const {
const idKeyValue *kv = FindKey( key );
if ( kv ) {
return kv->GetValue();
}
return defaultString;
}
ID_INLINE float idDict::GetFloat( const char *key, const char *defaultString ) const {
return atof( GetString( key, defaultString ) );
}
ID_INLINE int idDict::GetInt( const char *key, const char *defaultString ) const {
return atoi( GetString( key, defaultString ) );
}
ID_INLINE bool idDict::GetBool( const char *key, const char *defaultString ) const {
return ( atoi( GetString( key, defaultString ) ) != 0 );
}
//Added for LM
ID_INLINE bool idDict::GetBool( const char *key, const bool defaultBool ) const {
const idKeyValue *kv = FindKey( key );
if ( kv ) {
return atoi( kv->GetValue() ) != 0;
}
return defaultBool;
}
ID_INLINE idVec3 idDict::GetVector( const char *key, const char *defaultString ) const {
idVec3 out;
GetVector( key, defaultString, out );
return out;
}
ID_INLINE idVec2 idDict::GetVec2( const char *key, const char *defaultString ) const {
idVec2 out;
GetVec2( key, defaultString, out );
return out;
}
ID_INLINE idVec4 idDict::GetVec4( const char *key, const char *defaultString ) const {
idVec4 out;
GetVec4( key, defaultString, out );
return out;
}
ID_INLINE idAngles idDict::GetAngles( const char *key, const char *defaultString ) const {
idAngles out;
GetAngles( key, defaultString, out );
return out;
}
ID_INLINE idMat3 idDict::GetMatrix( const char *key, const char *defaultString ) const {
idMat3 out;
GetMatrix( key, defaultString, out );
return out;
}
ID_INLINE int idDict::GetNumKeyVals( void ) const {
return args.Num();
}
ID_INLINE const idKeyValue *idDict::GetKeyVal( int index ) const {
if ( index >= 0 && index < args.Num() ) {
return &args[ index ];
}
return NULL;
}
#endif /* !__DICT_H__ */