stringStack.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 _STRINGSTACK_H_
  23. #define _STRINGSTACK_H_
  24. #ifndef _STRINGFUNCTIONS_H_
  25. #include "core/strings/stringFunctions.h"
  26. #endif
  27. #ifndef _STRINGTABLE_H_
  28. #include "core/stringTable.h"
  29. #endif
  30. #ifndef _CONSOLE_H_
  31. #include "console/console.h"
  32. #endif
  33. typedef U32 StringStackPtr;
  34. struct StringStack;
  35. /// Helper class which stores a relative pointer in the StringStack buffer
  36. class StringStackPtrRef
  37. {
  38. public:
  39. StringStackPtrRef() : mOffset(0) {;}
  40. StringStackPtrRef(StringStackPtr offset) : mOffset(offset) {;}
  41. StringStackPtr mOffset;
  42. /// Get pointer to string in stack stk
  43. inline char *getPtr(StringStack *stk);
  44. };
  45. /// Core stack for interpreter operations.
  46. ///
  47. /// This class provides some powerful semantics for working with strings, and is
  48. /// used heavily by the console interpreter.
  49. struct StringStack
  50. {
  51. enum {
  52. MaxStackDepth = 1024,
  53. MaxArgs = 20,
  54. ReturnBufferSpace = 512
  55. };
  56. char *mBuffer;
  57. U32 mBufferSize;
  58. const char *mArgV[MaxArgs];
  59. U32 mFrameOffsets[MaxStackDepth];
  60. U32 mStartOffsets[MaxStackDepth];
  61. U32 mNumFrames;
  62. U32 mArgc;
  63. U32 mStart;
  64. U32 mLen;
  65. U32 mStartStackSize;
  66. U32 mFunctionOffset;
  67. U32 mArgBufferSize;
  68. char *mArgBuffer;
  69. void validateBufferSize(U32 size);
  70. void validateArgBufferSize(U32 size);
  71. StringStack();
  72. ~StringStack();
  73. /// Set the top of the stack to be an integer value.
  74. void setIntValue(U32 i);
  75. /// Set the top of the stack to be a float value.
  76. void setFloatValue(F64 v);
  77. /// Return a temporary buffer we can use to return data.
  78. char* getReturnBuffer(U32 size);
  79. /// Return a buffer we can use for arguments.
  80. ///
  81. /// This updates the function offset.
  82. char *getArgBuffer(U32 size);
  83. /// Clear the function offset.
  84. void clearFunctionOffset();
  85. /// Set a string value on the top of the stack.
  86. void setStringValue(const char *s);
  87. /// Get the top of the stack, as a StringTableEntry.
  88. ///
  89. /// @note Don't free this memory!
  90. inline StringTableEntry getSTValue()
  91. {
  92. return StringTable->insert(mBuffer + mStart);
  93. }
  94. /// Get an integer representation of the top of the stack.
  95. inline S64 getIntValue()
  96. {
  97. return dAtol(mBuffer + mStart);
  98. }
  99. /// Get a float representation of the top of the stack.
  100. inline F64 getFloatValue()
  101. {
  102. return dAtod(mBuffer + mStart);
  103. }
  104. /// Get a string representation of the top of the stack.
  105. ///
  106. /// @note This returns a pointer to the actual top of the stack, be careful!
  107. inline const char *getStringValue()
  108. {
  109. return mBuffer + mStart;
  110. }
  111. inline const char *getPreviousStringValue()
  112. {
  113. return mBuffer + mStartOffsets[mStartStackSize-1];
  114. }
  115. /// Advance the start stack, placing a zero length string on the top.
  116. ///
  117. /// @note You should use StringStack::push, not this, if you want to
  118. /// properly push the stack.
  119. void advance();
  120. /// Advance the start stack, placing a single character, null-terminated strong
  121. /// on the top.
  122. ///
  123. /// @note You should use StringStack::push, not this, if you want to
  124. /// properly push the stack.
  125. void advanceChar(char c);
  126. /// Push the stack, placing a zero-length string on the top.
  127. void push();
  128. inline void setLen(U32 newlen)
  129. {
  130. mLen = newlen;
  131. }
  132. /// Pop the start stack.
  133. void rewind();
  134. // Terminate the current string, and pop the start stack.
  135. void rewindTerminate();
  136. /// Compare 1st and 2nd items on stack, consuming them in the process,
  137. /// and returning true if they matched, false if they didn't.
  138. U32 compare();
  139. void clearFrames();
  140. };
  141. extern StringStack STR;
  142. inline char* StringStackPtrRef::getPtr(StringStack *stk) { return stk->mBuffer + mOffset; }
  143. #endif