consoleDictionary.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _CONSOLE_DICTIONARY_H_
  23. #define _CONSOLE_DICTIONARY_H_
  24. #ifndef _STRINGTABLE_H_
  25. #include "string/stringTable.h"
  26. #endif
  27. #ifndef _VECTOR_H_
  28. #include "collection/vector.h"
  29. #endif
  30. #ifndef _CONSOLETYPES_H_
  31. #include "console/consoleTypes.h"
  32. #endif
  33. //-----------------------------------------------------------------------------
  34. class ExprEvalState;
  35. class CodeBlock;
  36. extern char *typeValueEmpty;
  37. //-----------------------------------------------------------------------------
  38. class Dictionary
  39. {
  40. public:
  41. struct Entry
  42. {
  43. enum
  44. {
  45. TypeInternalInt = -3,
  46. TypeInternalFloat = -2,
  47. TypeInternalString = -1,
  48. };
  49. StringTableEntry name;
  50. Entry *nextEntry;
  51. S32 type;
  52. char *sval;
  53. U32 ival; // doubles as strlen when type = -1
  54. F32 fval;
  55. U32 bufferLen;
  56. void *dataPtr;
  57. Entry(StringTableEntry name);
  58. ~Entry();
  59. U32 getIntValue()
  60. {
  61. if(type <= TypeInternalString)
  62. return ival;
  63. else
  64. return dAtoi(Con::getData(type, dataPtr, 0));
  65. }
  66. F32 getFloatValue()
  67. {
  68. if(type <= TypeInternalString)
  69. return fval;
  70. else
  71. return dAtof(Con::getData(type, dataPtr, 0));
  72. }
  73. const char *getStringValue()
  74. {
  75. if(type == TypeInternalString)
  76. return sval;
  77. if(type == TypeInternalFloat)
  78. return Con::getData(TypeF32, &fval, 0);
  79. else if(type == TypeInternalInt)
  80. return Con::getData(TypeS32, &ival, 0);
  81. else
  82. return Con::getData(type, dataPtr, 0);
  83. }
  84. void setIntValue(U32 val)
  85. {
  86. if(type <= TypeInternalString)
  87. {
  88. fval = (F32)val;
  89. ival = val;
  90. if(sval != typeValueEmpty)
  91. {
  92. dFree(sval);
  93. sval = typeValueEmpty;
  94. }
  95. type = TypeInternalInt;
  96. return;
  97. }
  98. else
  99. {
  100. const char *dptr = Con::getData(TypeS32, &val, 0);
  101. Con::setData(type, dataPtr, 0, 1, &dptr);
  102. }
  103. }
  104. void setFloatValue(F32 val)
  105. {
  106. if(type <= TypeInternalString)
  107. {
  108. fval = val;
  109. ival = static_cast<U32>(static_cast<S32>(val));
  110. if(sval != typeValueEmpty)
  111. {
  112. dFree(sval);
  113. sval = typeValueEmpty;
  114. }
  115. type = TypeInternalFloat;
  116. return;
  117. }
  118. else
  119. {
  120. const char *dptr = Con::getData(TypeF32, &val, 0);
  121. Con::setData(type, dataPtr, 0, 1, &dptr);
  122. }
  123. }
  124. void setStringValue(const char *value);
  125. };
  126. private:
  127. struct HashTableData
  128. {
  129. Dictionary* owner;
  130. S32 size;
  131. S32 count;
  132. Entry **data;
  133. };
  134. HashTableData *hashTable;
  135. ExprEvalState *exprState;
  136. public:
  137. StringTableEntry scopeName;
  138. Namespace *scopeNamespace;
  139. CodeBlock *code;
  140. U32 ip;
  141. Dictionary();
  142. Dictionary(ExprEvalState *state, Dictionary* ref=NULL);
  143. ~Dictionary();
  144. Entry *lookup(StringTableEntry name);
  145. Entry *add(StringTableEntry name);
  146. void setState(ExprEvalState *state, Dictionary* ref=NULL);
  147. void remove(Entry *);
  148. void reset();
  149. void exportVariables(const char *varString, const char *fileName, bool append);
  150. void deleteVariables(const char *varString);
  151. void setVariable(StringTableEntry name, const char *value);
  152. const char *getVariable(StringTableEntry name, bool *valid = NULL);
  153. void addVariable(const char *name, S32 type, void *dataPtr);
  154. bool removeVariable(StringTableEntry name);
  155. /// Return the best tab completion for prevText, with the length
  156. /// of the pre-tab string in baseLen.
  157. const char *tabComplete(const char *prevText, S32 baseLen, bool);
  158. };
  159. #endif // _CONSOLE_DICTIONARY_H_