home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / Dict.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  10KB  |  310 lines

  1.  
  2. #ifndef __DICT_H__
  3. #define __DICT_H__
  4.  
  5. /*
  6. ===============================================================================
  7.  
  8. Key/value dictionary
  9.  
  10. This is a dictionary class that tracks an arbitrary number of key / value
  11. pair combinations. It is used for map entity spawning, GUI state management,
  12. and other things.
  13.  
  14. Keys are compared case-insensitive.
  15.  
  16. Does not allocate memory until the first key/value pair is added.
  17.  
  18. ===============================================================================
  19. */
  20.  
  21. class idKeyValue {
  22.     friend class idDict;
  23.  
  24. public:
  25.     const idStr &        GetKey( void ) const { return *key; }
  26.     const idStr &        GetValue( void ) const { return *value; }
  27.  
  28.     size_t                Allocated( void ) const { return key->Allocated() + value->Allocated(); }
  29.     size_t                Size( void ) const { return sizeof( *this ) + key->Size() + value->Size(); }
  30.  
  31.     bool                operator==( const idKeyValue &kv ) const { return ( key == kv.key && value == kv.value ); }
  32.  
  33. private:
  34.     const idPoolStr *    key;
  35.     const idPoolStr *    value;
  36. };
  37.  
  38. // RAVEN BEGIN
  39. // jsinger: added to allow support for serialization/deserialization of binary decls
  40. #ifdef RV_BINARYDECLS
  41. #include "../serialization/SerialOutputStream.h"
  42. #include "../serialization/SerialInputStream.h"
  43. #endif
  44. // RAVEN END
  45. class idDict {
  46. public:
  47.                         idDict( void );
  48.                         idDict( const idDict &other );    // allow declaration with assignment
  49.                         ~idDict( void );
  50.  
  51.                         // set the granularity for the index
  52.     void                SetGranularity( int granularity );
  53.                         // set hash size
  54.     void                SetHashSize( int hashSize );
  55.                         // clear existing key/value pairs and copy all key/value pairs from other
  56.     idDict &            operator=( const idDict &other );
  57.                         // copy from other while leaving existing key/value pairs in place
  58.     void                Copy( const idDict &other );
  59.                         // clear existing key/value pairs and transfer key/value pairs from other
  60.     void                TransferKeyValues( idDict &other );
  61.                         // parse dict from parser
  62.     bool                Parse( idParser &parser );
  63.                         // copy key/value pairs from other dict not present in this dict
  64.     void                SetDefaults( const idDict *dict );
  65.                         // clear dict freeing up memory
  66.     void                Clear( void );
  67.                         // print the dict
  68.     void                Print() const;
  69.  
  70.     size_t                Allocated( void ) const;
  71.     size_t                Size( void ) const { return sizeof( *this ) + Allocated(); }
  72.  
  73.     void                Set( const char *key, const char *value );
  74.     void                SetFloat( const char *key, float val );
  75.     void                SetInt( const char *key, int val );
  76.     void                SetBool( const char *key, bool val );
  77.     void                SetVector( const char *key, const idVec3 &val );
  78.     void                SetVec2( const char *key, const idVec2 &val );
  79.     void                SetVec4( const char *key, const idVec4 &val );
  80.     void                SetAngles( const char *key, const idAngles &val );
  81.     void                SetMatrix( const char *key, const idMat3 &val );
  82.     
  83.                         // these return default values of 0.0, 0 and false
  84.     const char *        GetString( const char *key, const char *defaultString = "" ) const;
  85.     float                GetFloat( const char *key, const char *defaultString = "0" ) const;
  86.     int                    GetInt( const char *key, const char *defaultString = "0" ) const;
  87.     bool                GetBool( const char *key, const char *defaultString = "0" ) const;
  88.     idVec3                GetVector( const char *key, const char *defaultString = NULL ) const;
  89.     idVec2                GetVec2( const char *key, const char *defaultString = NULL ) const;
  90.     idVec4                GetVec4( const char *key, const char *defaultString = NULL ) const;
  91.     idAngles            GetAngles( const char *key, const char *defaultString = NULL ) const;
  92.     idMat3                GetMatrix( const char *key, const char *defaultString = NULL ) const;
  93.  
  94.     bool                GetString( const char *key, const char *defaultString, const char **out ) const;
  95.     bool                GetString( const char *key, const char *defaultString, idStr &out ) const;
  96.     bool                GetFloat( const char *key, const char *defaultString, float &out ) const;
  97.     bool                GetInt( const char *key, const char *defaultString, int &out ) const;
  98.     bool                GetBool( const char *key, const char *defaultString, bool &out ) const;
  99.     bool                GetVector( const char *key, const char *defaultString, idVec3 &out ) const;
  100.     bool                GetVec2( const char *key, const char *defaultString, idVec2 &out ) const;
  101.     bool                GetVec4( const char *key, const char *defaultString, idVec4 &out ) const;
  102.     bool                GetAngles( const char *key, const char *defaultString, idAngles &out ) const;
  103.     bool                GetMatrix( const char *key, const char *defaultString, idMat3 &out ) const;
  104.  
  105.     int                    GetNumKeyVals( void ) const;
  106.     const idKeyValue *    GetKeyVal( int index ) const;
  107.                         // returns the key/value pair with the given key
  108.                         // returns NULL if the key/value pair does not exist
  109.     const idKeyValue *    FindKey( const char *key ) const;
  110.                         // returns the index to the key/value pair with the given key
  111.                         // returns -1 if the key/value pair does not exist
  112.     int                    FindKeyIndex( const char *key ) const;
  113.                         // delete the key/value pair with the given key
  114.     void                Delete( const char *key );
  115.                         // finds the next key/value pair with the given key prefix.
  116.                         // lastMatch can be used to do additional searches past the first match.
  117.     const idKeyValue *    MatchPrefix( const char *prefix, const idKeyValue *lastMatch = NULL ) const;
  118.                         // randomly chooses one of the key/value pairs with the given key prefix and returns it's value
  119. // RAVEN BEGIN
  120. // abahr: added default value param
  121.     const char *        RandomPrefix( const char *prefix, idRandom &random, const char* defaultValue = "" ) const;
  122. // RAVEN END
  123.  
  124.     void                WriteToFileHandle( idFile *f ) const;
  125.     void                ReadFromFileHandle( idFile *f );
  126.  
  127. // RAVEN BEGIN
  128. // nrausch: write/read to memory
  129.     int                    WriteToMemory( void *mem, int maxSize ) const;
  130.     void                ReadFromMemory( void *mem, int size );
  131. // RAVEN END
  132.  
  133.                         // returns a unique checksum for this dictionary's content
  134.     int                    Checksum( void ) const;
  135.  
  136.     static void            Init( void );
  137.     static void            Shutdown( void );
  138.  
  139.     static void            ShowMemoryUsage_f( const idCmdArgs &args );
  140.     static void            ListKeys_f( const idCmdArgs &args );
  141.     static void            ListValues_f( const idCmdArgs &args );
  142.  
  143. // RAVEN BEGIN
  144. // jsinger: added to allow support for serialization/deserialization of binary decls
  145. #ifdef RV_BINARYDECLS
  146.     void                Write( SerialOutputStream &stream ) const;
  147.     idDict( SerialInputStream &stream );
  148. #endif
  149. // RAVEN END
  150.  
  151. // RAVEN BEGIN
  152. // mwhitlock: Dynamic memory consolidation
  153. #if defined(_RV_MEM_SYS_SUPPORT)
  154.     // Set the heap used for all allocations
  155.     void                SetAllocatorHeap ( rvHeap* heap )
  156.     {
  157.         args.SetAllocatorHeap(heap);
  158.         argHash.SetAllocatorHeap(heap);
  159.     }
  160. #endif
  161. // RAVEN END
  162.  
  163. private:
  164.     idList<idKeyValue>    args;
  165.     idHashIndex            argHash;
  166.     static idStrPool    globalKeys;
  167.     static idStrPool    globalValues;
  168. };
  169.  
  170.  
  171. ID_INLINE idDict::idDict( void ) {
  172.     args.SetGranularity( 16 );
  173.     argHash.SetGranularity( 16 );
  174.     argHash.Clear( 128, 16 );
  175. }
  176.  
  177. ID_INLINE idDict::idDict( const idDict &other ) {
  178.     *this = other;
  179. }
  180.  
  181. ID_INLINE idDict::~idDict( void ) {
  182.     Clear();
  183. }
  184.  
  185. ID_INLINE void idDict::SetGranularity( int granularity ) {
  186.     args.SetGranularity( granularity );
  187.     argHash.SetGranularity( granularity );
  188. }
  189.  
  190. ID_INLINE void idDict::SetHashSize( int hashSize ) {
  191.     if ( args.Num() == 0 ) {
  192.         argHash.Clear( hashSize, 16 );
  193.     }
  194. }
  195.  
  196. ID_INLINE void idDict::SetFloat( const char *key, float val ) {
  197.     Set( key, va( "%f", val ) );
  198. }
  199.  
  200. ID_INLINE void idDict::SetInt( const char *key, int val ) {
  201.     Set( key, va( "%i", val ) );
  202. }
  203.  
  204. ID_INLINE void idDict::SetBool( const char *key, bool val ) {
  205.     Set( key, va( "%i", val ) );
  206. }
  207.  
  208. ID_INLINE void idDict::SetVector( const char *key, const idVec3 &val ) {
  209.     Set( key, val.ToString() );
  210. }
  211.  
  212. ID_INLINE void idDict::SetVec4( const char *key, const idVec4 &val ) {
  213.     Set( key, val.ToString() );
  214. }
  215.  
  216. ID_INLINE void idDict::SetVec2( const char *key, const idVec2 &val ) {
  217.     Set( key, val.ToString() );
  218. }
  219.  
  220. ID_INLINE void idDict::SetAngles( const char *key, const idAngles &val ) {
  221.     Set( key, val.ToString() );
  222. }
  223.  
  224. ID_INLINE void idDict::SetMatrix( const char *key, const idMat3 &val ) {
  225.     Set( key, val.ToString() );
  226. }
  227.  
  228. ID_INLINE bool idDict::GetString( const char *key, const char *defaultString, const char **out ) const {
  229.     const idKeyValue *kv = FindKey( key );
  230.     if ( kv ) {
  231.         *out = kv->GetValue();
  232.         return true;
  233.     }
  234.     *out = defaultString;
  235.     return false;
  236. }
  237.  
  238. ID_INLINE bool idDict::GetString( const char *key, const char *defaultString, idStr &out ) const {
  239.     const idKeyValue *kv = FindKey( key );
  240.     if ( kv ) {
  241.         out = kv->GetValue();
  242.         return true;
  243.     }
  244.     out = defaultString;
  245.     return false;
  246. }
  247.  
  248. ID_INLINE const char *idDict::GetString( const char *key, const char *defaultString ) const {
  249.     const idKeyValue *kv = FindKey( key );
  250.     if ( kv ) {
  251.         return kv->GetValue();
  252.     }
  253.     return defaultString;
  254. }
  255.  
  256. ID_INLINE float idDict::GetFloat( const char *key, const char *defaultString ) const {
  257.     return atof( GetString( key, defaultString ) );
  258. }
  259.  
  260. ID_INLINE int idDict::GetInt( const char *key, const char *defaultString ) const {
  261.     return atoi( GetString( key, defaultString ) );
  262. }
  263.  
  264. ID_INLINE bool idDict::GetBool( const char *key, const char *defaultString ) const {
  265.     return ( atoi( GetString( key, defaultString ) ) != 0 );
  266. }
  267.  
  268. ID_INLINE idVec3 idDict::GetVector( const char *key, const char *defaultString ) const {
  269.     idVec3 out;
  270.     GetVector( key, defaultString, out );
  271.     return out;
  272. }
  273.  
  274. ID_INLINE idVec2 idDict::GetVec2( const char *key, const char *defaultString ) const {
  275.     idVec2 out;
  276.     GetVec2( key, defaultString, out );
  277.     return out;
  278. }
  279.  
  280. ID_INLINE idVec4 idDict::GetVec4( const char *key, const char *defaultString ) const {
  281.     idVec4 out;
  282.     GetVec4( key, defaultString, out );
  283.     return out;
  284. }
  285.  
  286. ID_INLINE idAngles idDict::GetAngles( const char *key, const char *defaultString ) const {
  287.     idAngles out;
  288.     GetAngles( key, defaultString, out );
  289.     return out;
  290. }
  291.  
  292. ID_INLINE idMat3 idDict::GetMatrix( const char *key, const char *defaultString ) const {
  293.     idMat3 out;
  294.     GetMatrix( key, defaultString, out );
  295.     return out;
  296. }
  297.  
  298. ID_INLINE int idDict::GetNumKeyVals( void ) const {
  299.     return args.Num();
  300. }
  301.  
  302. ID_INLINE const idKeyValue *idDict::GetKeyVal( int index ) const {
  303.     if ( index >= 0 && index < args.Num() ) {
  304.         return &args[ index ];
  305.     }
  306.     return NULL;
  307. }
  308.  
  309. #endif /* !__DICT_H__ */
  310.