simFieldDictionary.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #ifndef _SIMFIELDDICTIONARY_H_
  27. #define _SIMFIELDDICTIONARY_H_
  28. // Forward Refs
  29. class ConsoleBaseType;
  30. class SimObject;
  31. #include "core/stringTable.h"
  32. #include "core/stream/stream.h"
  33. #ifndef _TORQUE_STRING_H_
  34. #include "core/util/str.h"
  35. #endif
  36. /// Dictionary to keep track of dynamic fields on SimObject.
  37. class SimFieldDictionary
  38. {
  39. friend class SimFieldDictionaryIterator;
  40. public:
  41. struct Entry
  42. {
  43. Entry() : slotName(StringTable->EmptyString()), value(NULL), next(NULL), type(NULL) {};
  44. StringTableEntry slotName;
  45. char *value;
  46. Entry *next;
  47. ConsoleBaseType *type;
  48. };
  49. enum
  50. {
  51. HashTableSize = 19
  52. };
  53. Entry *mHashTable[HashTableSize];
  54. private:
  55. static Entry *smFreeList;
  56. void freeEntry(Entry *entry);
  57. Entry* addEntry(U32 bucket, StringTableEntry slotName, ConsoleBaseType* type, char* value = 0);
  58. static U32 getHashValue(StringTableEntry slotName);
  59. static U32 getHashValue(const String& fieldName);
  60. U32 mNumFields;
  61. /// In order to efficiently detect when a dynamic field has been
  62. /// added or deleted, we increment this every time we add or
  63. /// remove a field.
  64. U32 mVersion;
  65. public:
  66. const U32 getVersion() const { return mVersion; }
  67. SimFieldDictionary();
  68. ~SimFieldDictionary();
  69. void setFieldType(StringTableEntry slotName, const char *typeString);
  70. void setFieldType(StringTableEntry slotName, const U32 typeId);
  71. void setFieldType(StringTableEntry slotName, ConsoleBaseType *type);
  72. void setFieldValue(StringTableEntry slotName, const char *value);
  73. const char *getFieldValue(StringTableEntry slotName);
  74. U32 getFieldType(StringTableEntry slotName) const;
  75. Entry *findDynamicField(const String &fieldName) const;
  76. Entry *findDynamicField(StringTableEntry fieldName) const;
  77. void writeFields(SimObject *obj, Stream &strem, U32 tabStop);
  78. void printFields(SimObject *obj);
  79. void assignFrom(SimFieldDictionary *dict);
  80. U32 getNumFields() const { return mNumFields; }
  81. Entry *operator[](U32 index);
  82. void setFieldValue(StringTableEntry slotName, const char *value, ConsoleBaseType *type, bool no_replace);
  83. void assignFrom(SimFieldDictionary *dict, const char* filter, bool no_replace);
  84. };
  85. class SimFieldDictionaryIterator
  86. {
  87. SimFieldDictionary * mDictionary;
  88. S32 mHashIndex;
  89. SimFieldDictionary::Entry * mEntry;
  90. public:
  91. SimFieldDictionaryIterator(SimFieldDictionary*);
  92. SimFieldDictionary::Entry* operator++();
  93. SimFieldDictionary::Entry* operator*();
  94. };
  95. #endif // _SIMFIELDDICTIONARY_H_