simFieldDictionary.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SIMFIELDDICTIONARY_H_
  23. #define _SIMFIELDDICTIONARY_H_
  24. // Forward Refs
  25. class ConsoleBaseType;
  26. class SimObject;
  27. #include "core/stringTable.h"
  28. #include "core/stream/stream.h"
  29. #ifndef _TORQUE_STRING_H_
  30. #include "core/util/str.h"
  31. #endif
  32. /// Dictionary to keep track of dynamic fields on SimObject.
  33. class SimFieldDictionary
  34. {
  35. friend class SimFieldDictionaryIterator;
  36. public:
  37. struct Entry
  38. {
  39. Entry() : type( NULL ) {};
  40. StringTableEntry slotName;
  41. char *value;
  42. Entry *next;
  43. ConsoleBaseType *type;
  44. };
  45. enum
  46. {
  47. HashTableSize = 19
  48. };
  49. Entry *mHashTable[HashTableSize];
  50. private:
  51. static Entry *smFreeList;
  52. void freeEntry(Entry *entry);
  53. Entry* addEntry( U32 bucket, StringTableEntry slotName, ConsoleBaseType* type, char* value = 0 );
  54. static U32 getHashValue( StringTableEntry slotName );
  55. static U32 getHashValue( const String& fieldName );
  56. U32 mNumFields;
  57. /// In order to efficiently detect when a dynamic field has been
  58. /// added or deleted, we increment this every time we add or
  59. /// remove a field.
  60. U32 mVersion;
  61. public:
  62. const U32 getVersion() const { return mVersion; }
  63. SimFieldDictionary();
  64. ~SimFieldDictionary();
  65. void setFieldType(StringTableEntry slotName, const char *typeString);
  66. void setFieldType(StringTableEntry slotName, const U32 typeId);
  67. void setFieldType(StringTableEntry slotName, ConsoleBaseType *type);
  68. void setFieldValue(StringTableEntry slotName, const char *value);
  69. const char *getFieldValue(StringTableEntry slotName);
  70. U32 getFieldType(StringTableEntry slotName) const;
  71. Entry *findDynamicField(const String &fieldName) const;
  72. Entry *findDynamicField( StringTableEntry fieldName) const;
  73. void writeFields(SimObject *obj, Stream &strem, U32 tabStop);
  74. void printFields(SimObject *obj);
  75. void assignFrom(SimFieldDictionary *dict);
  76. U32 getNumFields() const { return mNumFields; }
  77. Entry *operator[](U32 index);
  78. };
  79. class SimFieldDictionaryIterator
  80. {
  81. SimFieldDictionary * mDictionary;
  82. S32 mHashIndex;
  83. SimFieldDictionary::Entry * mEntry;
  84. public:
  85. SimFieldDictionaryIterator(SimFieldDictionary*);
  86. SimFieldDictionary::Entry* operator++();
  87. SimFieldDictionary::Entry* operator*();
  88. };
  89. #endif // _SIMFIELDDICTIONARY_H_