doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Dict.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 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.
23 
24 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.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __DICT_H__
30 #define __DICT_H__
31 
32 /*
33 ===============================================================================
34 
35 Key/value dictionary
36 
37 This is a dictionary class that tracks an arbitrary number of key / value
38 pair combinations. It is used for map entity spawning, GUI state management,
39 and other things.
40 
41 Keys are compared case-insensitive.
42 
43 Does not allocate memory until the first key/value pair is added.
44 
45 ===============================================================================
46 */
47 
48 class idKeyValue {
49  friend class idDict;
50 
51 public:
52  const idStr & GetKey( void ) const { return *key; }
53  const idStr & GetValue( void ) const { return *value; }
54 
55  size_t Allocated( void ) const { return key->Allocated() + value->Allocated(); }
56  size_t Size( void ) const { return sizeof( *this ) + key->Size() + value->Size(); }
57 
58  bool operator==( const idKeyValue &kv ) const { return ( key == kv.key && value == kv.value ); }
59 
60 private:
61  const idPoolStr * key;
62  const idPoolStr * value;
63 };
64 
65 class idDict {
66 public:
67  idDict( void );
68  idDict( const idDict &other ); // allow declaration with assignment
69  ~idDict( void );
70 
71  // set the granularity for the index
72  void SetGranularity( int granularity );
73  // set hash size
74  void SetHashSize( int hashSize );
75  // clear existing key/value pairs and copy all key/value pairs from other
76  idDict & operator=( const idDict &other );
77  // copy from other while leaving existing key/value pairs in place
78  void Copy( const idDict &other );
79  // clear existing key/value pairs and transfer key/value pairs from other
80  void TransferKeyValues( idDict &other );
81  // parse dict from parser
82  bool Parse( idParser &parser );
83  // copy key/value pairs from other dict not present in this dict
84  void SetDefaults( const idDict *dict );
85  // clear dict freeing up memory
86  void Clear( void );
87  // print the dict
88  void Print() const;
89 
90  size_t Allocated( void ) const;
91  size_t Size( void ) const { return sizeof( *this ) + Allocated(); }
92 
93  void Set( const char *key, const char *value );
94  void SetFloat( const char *key, float val );
95  void SetInt( const char *key, int val );
96  void SetBool( const char *key, bool val );
97  void SetVector( const char *key, const idVec3 &val );
98  void SetVec2( const char *key, const idVec2 &val );
99  void SetVec4( const char *key, const idVec4 &val );
100  void SetAngles( const char *key, const idAngles &val );
101  void SetMatrix( const char *key, const idMat3 &val );
102 
103  // these return default values of 0.0, 0 and false
104  const char * GetString( const char *key, const char *defaultString = "" ) const;
105  float GetFloat( const char *key, const char *defaultString = "0" ) const;
106  int GetInt( const char *key, const char *defaultString = "0" ) const;
107  bool GetBool( const char *key, const char *defaultString = "0" ) const;
108  idVec3 GetVector( const char *key, const char *defaultString = NULL ) const;
109  idVec2 GetVec2( const char *key, const char *defaultString = NULL ) const;
110  idVec4 GetVec4( const char *key, const char *defaultString = NULL ) const;
111  idAngles GetAngles( const char *key, const char *defaultString = NULL ) const;
112  idMat3 GetMatrix( const char *key, const char *defaultString = NULL ) const;
113 
114  bool GetString( const char *key, const char *defaultString, const char **out ) const;
115  bool GetString( const char *key, const char *defaultString, idStr &out ) const;
116  bool GetFloat( const char *key, const char *defaultString, float &out ) const;
117  bool GetInt( const char *key, const char *defaultString, int &out ) const;
118  bool GetBool( const char *key, const char *defaultString, bool &out ) const;
119  bool GetVector( const char *key, const char *defaultString, idVec3 &out ) const;
120  bool GetVec2( const char *key, const char *defaultString, idVec2 &out ) const;
121  bool GetVec4( const char *key, const char *defaultString, idVec4 &out ) const;
122  bool GetAngles( const char *key, const char *defaultString, idAngles &out ) const;
123  bool GetMatrix( const char *key, const char *defaultString, idMat3 &out ) const;
124 
125  int GetNumKeyVals( void ) const;
126  const idKeyValue * GetKeyVal( int index ) const;
127  // returns the key/value pair with the given key
128  // returns NULL if the key/value pair does not exist
129  const idKeyValue * FindKey( const char *key ) const;
130  // returns the index to the key/value pair with the given key
131  // returns -1 if the key/value pair does not exist
132  int FindKeyIndex( const char *key ) const;
133  // delete the key/value pair with the given key
134  void Delete( const char *key );
135  // finds the next key/value pair with the given key prefix.
136  // lastMatch can be used to do additional searches past the first match.
137  const idKeyValue * MatchPrefix( const char *prefix, const idKeyValue *lastMatch = NULL ) const;
138  // randomly chooses one of the key/value pairs with the given key prefix and returns it's value
139  const char * RandomPrefix( const char *prefix, idRandom &random ) const;
140 
141  void WriteToFileHandle( idFile *f ) const;
142  void ReadFromFileHandle( idFile *f );
143 
144  // returns a unique checksum for this dictionary's content
145  int Checksum( void ) const;
146 
147  static void Init( void );
148  static void Shutdown( void );
149 
150  static void ShowMemoryUsage_f( const idCmdArgs &args );
151  static void ListKeys_f( const idCmdArgs &args );
152  static void ListValues_f( const idCmdArgs &args );
153 
154 private:
157 
160 };
161 
162 
163 ID_INLINE idDict::idDict( void ) {
164  args.SetGranularity( 16 );
165  argHash.SetGranularity( 16 );
166  argHash.Clear( 128, 16 );
167 }
168 
169 ID_INLINE idDict::idDict( const idDict &other ) {
170  *this = other;
171 }
172 
173 ID_INLINE idDict::~idDict( void ) {
174  Clear();
175 }
176 
177 ID_INLINE void idDict::SetGranularity( int granularity ) {
178  args.SetGranularity( granularity );
179  argHash.SetGranularity( granularity );
180 }
181 
182 ID_INLINE void idDict::SetHashSize( int hashSize ) {
183  if ( args.Num() == 0 ) {
184  argHash.Clear( hashSize, 16 );
185  }
186 }
187 
188 ID_INLINE void idDict::SetFloat( const char *key, float val ) {
189  Set( key, va( "%f", val ) );
190 }
191 
192 ID_INLINE void idDict::SetInt( const char *key, int val ) {
193  Set( key, va( "%i", val ) );
194 }
195 
196 ID_INLINE void idDict::SetBool( const char *key, bool val ) {
197  Set( key, va( "%i", val ) );
198 }
199 
200 ID_INLINE void idDict::SetVector( const char *key, const idVec3 &val ) {
201  Set( key, val.ToString() );
202 }
203 
204 ID_INLINE void idDict::SetVec4( const char *key, const idVec4 &val ) {
205  Set( key, val.ToString() );
206 }
207 
208 ID_INLINE void idDict::SetVec2( const char *key, const idVec2 &val ) {
209  Set( key, val.ToString() );
210 }
211 
212 ID_INLINE void idDict::SetAngles( const char *key, const idAngles &val ) {
213  Set( key, val.ToString() );
214 }
215 
216 ID_INLINE void idDict::SetMatrix( const char *key, const idMat3 &val ) {
217  Set( key, val.ToString() );
218 }
219 
220 ID_INLINE bool idDict::GetString( const char *key, const char *defaultString, const char **out ) const {
221  const idKeyValue *kv = FindKey( key );
222  if ( kv ) {
223  *out = kv->GetValue();
224  return true;
225  }
226  *out = defaultString;
227  return false;
228 }
229 
230 ID_INLINE bool idDict::GetString( const char *key, const char *defaultString, idStr &out ) const {
231  const idKeyValue *kv = FindKey( key );
232  if ( kv ) {
233  out = kv->GetValue();
234  return true;
235  }
236  out = defaultString;
237  return false;
238 }
239 
240 ID_INLINE const char *idDict::GetString( const char *key, const char *defaultString ) const {
241  const idKeyValue *kv = FindKey( key );
242  if ( kv ) {
243  return kv->GetValue();
244  }
245  return defaultString;
246 }
247 
248 ID_INLINE float idDict::GetFloat( const char *key, const char *defaultString ) const {
249  return atof( GetString( key, defaultString ) );
250 }
251 
252 ID_INLINE int idDict::GetInt( const char *key, const char *defaultString ) const {
253  return atoi( GetString( key, defaultString ) );
254 }
255 
256 ID_INLINE bool idDict::GetBool( const char *key, const char *defaultString ) const {
257  return ( atoi( GetString( key, defaultString ) ) != 0 );
258 }
259 
260 ID_INLINE idVec3 idDict::GetVector( const char *key, const char *defaultString ) const {
261  idVec3 out;
262  GetVector( key, defaultString, out );
263  return out;
264 }
265 
266 ID_INLINE idVec2 idDict::GetVec2( const char *key, const char *defaultString ) const {
267  idVec2 out;
268  GetVec2( key, defaultString, out );
269  return out;
270 }
271 
272 ID_INLINE idVec4 idDict::GetVec4( const char *key, const char *defaultString ) const {
273  idVec4 out;
274  GetVec4( key, defaultString, out );
275  return out;
276 }
277 
278 ID_INLINE idAngles idDict::GetAngles( const char *key, const char *defaultString ) const {
279  idAngles out;
280  GetAngles( key, defaultString, out );
281  return out;
282 }
283 
284 ID_INLINE idMat3 idDict::GetMatrix( const char *key, const char *defaultString ) const {
285  idMat3 out;
286  GetMatrix( key, defaultString, out );
287  return out;
288 }
289 
290 ID_INLINE int idDict::GetNumKeyVals( void ) const {
291  return args.Num();
292 }
293 
294 ID_INLINE const idKeyValue *idDict::GetKeyVal( int index ) const {
295  if ( index >= 0 && index < args.Num() ) {
296  return &args[ index ];
297  }
298  return NULL;
299 }
300 
301 #endif /* !__DICT_H__ */
float GetFloat(const char *key, const char *defaultString="0") const
Definition: Dict.h:248
GLsizei const GLfloat * value
Definition: glext.h:3614
int GetInt(const char *key, const char *defaultString="0") const
Definition: Dict.h:252
void TransferKeyValues(idDict &other)
Definition: Dict.cpp:111
void Delete(const char *key)
Definition: Dict.cpp:496
const char * ToString(int precision=2) const
Definition: Matrix.cpp:610
const idStr & GetKey(void) const
Definition: Dict.h:52
static void ListKeys_f(const idCmdArgs &args)
Definition: Dict.cpp:668
const char * ToString(int precision=2) const
Definition: Angles.cpp:238
static idStrPool globalValues
Definition: Dict.h:159
idAngles GetAngles(const char *key, const char *defaultString=NULL) const
Definition: Dict.h:278
int FindKeyIndex(const char *key) const
Definition: Dict.cpp:474
idHashIndex argHash
Definition: Dict.h:156
const idKeyValue * MatchPrefix(const char *prefix, const idKeyValue *lastMatch=NULL) const
Definition: Dict.cpp:523
void SetGranularity(int newgranularity)
Definition: List.h:305
Definition: Vector.h:316
void WriteToFileHandle(idFile *f) const
Definition: Dict.cpp:571
idVec2 GetVec2(const char *key, const char *defaultString=NULL) const
Definition: Dict.h:266
~idDict(void)
Definition: Dict.h:173
int Checksum(void) const
Definition: Dict.cpp:237
void Set(const char *key, const char *value)
Definition: Dict.cpp:275
const char * ToString(int precision=2) const
Definition: Vector.cpp:51
size_t Size(void) const
Definition: Dict.h:91
void SetFloat(const char *key, float val)
Definition: Dict.h:188
void Copy(const idDict &other)
Definition: Dict.cpp:70
void SetGranularity(const int newGranularity)
Definition: HashIndex.h:369
size_t Allocated(void) const
Definition: Dict.h:55
const idPoolStr * key
Definition: Dict.h:61
void ReadFromFileHandle(idFile *f)
Definition: Dict.cpp:607
Definition: File.h:50
Definition: Vector.h:52
idList< idKeyValue > args
Definition: Dict.h:155
static void ListValues_f(const idCmdArgs &args)
Definition: Dict.cpp:687
const idPoolStr * value
Definition: Dict.h:62
GLuint index
Definition: glext.h:3476
const char * GetString(const char *key, const char *defaultString="") const
Definition: Dict.h:240
Definition: Vector.h:808
void SetDefaults(const idDict *dict)
Definition: Dict.cpp:179
void SetAngles(const char *key, const idAngles &val)
Definition: Dict.h:212
idVec4 GetVec4(const char *key, const char *defaultString=NULL) const
Definition: Dict.h:272
void SetVec2(const char *key, const idVec2 &val)
Definition: Dict.h:208
bool GetBool(const char *key, const char *defaultString="0") const
Definition: Dict.h:256
Definition: Dict.h:65
#define NULL
Definition: Lib.h:88
void Clear(void)
Definition: Dict.cpp:201
idVec3 GetVector(const char *key, const char *defaultString=NULL) const
Definition: Dict.h:260
const char * ToString(int precision=2) const
Definition: Vector.cpp:307
bool Parse(idParser &parser)
Definition: Dict.cpp:142
static void ShowMemoryUsage_f(const idCmdArgs &args)
Definition: Dict.cpp:647
const idStr & GetValue(void) const
Definition: Dict.h:53
static void Init(void)
Definition: Dict.cpp:627
void SetBool(const char *key, bool val)
Definition: Dict.h:196
const idKeyValue * FindKey(const char *key) const
Definition: Dict.cpp:451
static idStrPool globalKeys
Definition: Dict.h:158
void SetHashSize(int hashSize)
Definition: Dict.h:182
idDict(void)
Definition: Dict.h:163
const char * ToString(int precision=2) const
Definition: Vector.cpp:221
bool operator==(const idKeyValue &kv) const
Definition: Dict.h:58
void SetGranularity(int granularity)
Definition: Dict.h:177
void SetInt(const char *key, int val)
Definition: Dict.h:192
Definition: Matrix.h:333
tuple f
Definition: idal.py:89
int Num(void) const
Definition: List.h:265
const char * RandomPrefix(const char *prefix, idRandom &random) const
Definition: Dict.cpp:553
Definition: Str.h:116
const idKeyValue * GetKeyVal(int index) const
Definition: Dict.h:294
void Clear(void)
Definition: HashIndex.h:328
void SetVec4(const char *key, const idVec4 &val)
Definition: Dict.h:204
static void Shutdown(void)
Definition: Dict.cpp:637
idMat3 GetMatrix(const char *key, const char *defaultString=NULL) const
Definition: Dict.h:284
char * va(const char *fmt,...)
Definition: Str.cpp:1568
size_t Allocated(void) const
Definition: StrPool.h:50
size_t Size(void) const
Definition: Dict.h:56
size_t Allocated(void) const
Definition: Dict.cpp:258
size_t Size(void) const
Definition: StrPool.h:52
int GetNumKeyVals(void) const
Definition: Dict.h:290
void Print() const
Definition: Dict.cpp:218
void SetVector(const char *key, const idVec3 &val)
Definition: Dict.h:200
idDict & operator=(const idDict &other)
Definition: Dict.cpp:42
void SetMatrix(const char *key, const idMat3 &val)
Definition: Dict.h:216