stringStack.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. {
  71. if(size > mBufferSize)
  72. {
  73. mBufferSize = size + 2048;
  74. mBuffer = (char *) dRealloc(mBuffer, mBufferSize);
  75. }
  76. }
  77. void validateArgBufferSize(U32 size)
  78. {
  79. if(size > mArgBufferSize)
  80. {
  81. mArgBufferSize = size + 2048;
  82. mArgBuffer = (char *) dRealloc(mArgBuffer, mArgBufferSize);
  83. }
  84. }
  85. StringStack()
  86. {
  87. mBufferSize = 0;
  88. mBuffer = NULL;
  89. mArgBufferSize = 0;
  90. mArgBuffer = NULL;
  91. mNumFrames = 0;
  92. mStart = 0;
  93. mLen = 0;
  94. mStartStackSize = 0;
  95. mFunctionOffset = 0;
  96. validateBufferSize(8192);
  97. validateArgBufferSize(2048);
  98. dMemset(mBuffer, '\0', mBufferSize);
  99. dMemset(mArgBuffer, '\0', mArgBufferSize);
  100. }
  101. ~StringStack()
  102. {
  103. if( mBuffer )
  104. dFree( mBuffer );
  105. if( mArgBuffer )
  106. dFree( mArgBuffer );
  107. }
  108. /// Set the top of the stack to be an integer value.
  109. void setIntValue(U32 i)
  110. {
  111. validateBufferSize(mStart + 32);
  112. dSprintf(mBuffer + mStart, 32, "%d", i);
  113. mLen = dStrlen(mBuffer + mStart);
  114. }
  115. /// Set the top of the stack to be a float value.
  116. void setFloatValue(F64 v)
  117. {
  118. validateBufferSize(mStart + 32);
  119. dSprintf(mBuffer + mStart, 32, "%g", v);
  120. mLen = dStrlen(mBuffer + mStart);
  121. }
  122. /// Return a temporary buffer we can use to return data.
  123. char* getReturnBuffer(U32 size)
  124. {
  125. AssertFatal(Con::isMainThread(), "Manipulating return buffer from a secondary thread!");
  126. validateArgBufferSize(size);
  127. return mArgBuffer;
  128. }
  129. /// Return a buffer we can use for arguments.
  130. ///
  131. /// This updates the function offset.
  132. char *getArgBuffer(U32 size)
  133. {
  134. AssertFatal(Con::isMainThread(), "Manipulating console arg buffer from a secondary thread!");
  135. validateBufferSize(mStart + mFunctionOffset + size);
  136. char *ret = mBuffer + mStart + mFunctionOffset;
  137. mFunctionOffset += size;
  138. return ret;
  139. }
  140. /// Clear the function offset.
  141. void clearFunctionOffset()
  142. {
  143. //Con::printf("StringStack mFunctionOffset = 0 (from %i)", mFunctionOffset);
  144. mFunctionOffset = 0;
  145. }
  146. /// Set a string value on the top of the stack.
  147. void setStringValue(const char *s)
  148. {
  149. if(!s)
  150. {
  151. mLen = 0;
  152. mBuffer[mStart] = 0;
  153. return;
  154. }
  155. mLen = dStrlen(s);
  156. validateBufferSize(mStart + mLen + 2);
  157. dStrcpy(mBuffer + mStart, s);
  158. }
  159. /// Get the top of the stack, as a StringTableEntry.
  160. ///
  161. /// @note Don't free this memory!
  162. inline StringTableEntry getSTValue()
  163. {
  164. return StringTable->insert(mBuffer + mStart);
  165. }
  166. /// Get an integer representation of the top of the stack.
  167. inline U32 getIntValue()
  168. {
  169. return dAtoi(mBuffer + mStart);
  170. }
  171. /// Get a float representation of the top of the stack.
  172. inline F64 getFloatValue()
  173. {
  174. return dAtof(mBuffer + mStart);
  175. }
  176. /// Get a string representation of the top of the stack.
  177. ///
  178. /// @note This returns a pointer to the actual top of the stack, be careful!
  179. inline const char *getStringValue()
  180. {
  181. return mBuffer + mStart;
  182. }
  183. inline const char *getPreviousStringValue()
  184. {
  185. return mBuffer + mStartOffsets[mStartStackSize-1];
  186. }
  187. inline StringStackPtr getStringValuePtr()
  188. {
  189. return (getStringValue() - mBuffer);
  190. }
  191. inline StringStackPtr getPreviousStringValuePtr()
  192. {
  193. return (getPreviousStringValue() - mBuffer);
  194. }
  195. /// Advance the start stack, placing a zero length string on the top.
  196. ///
  197. /// @note You should use StringStack::push, not this, if you want to
  198. /// properly push the stack.
  199. void advance()
  200. {
  201. mStartOffsets[mStartStackSize++] = mStart;
  202. mStart += mLen;
  203. mLen = 0;
  204. }
  205. /// Advance the start stack, placing a single character, null-terminated strong
  206. /// on the top.
  207. ///
  208. /// @note You should use StringStack::push, not this, if you want to
  209. /// properly push the stack.
  210. void advanceChar(char c)
  211. {
  212. mStartOffsets[mStartStackSize++] = mStart;
  213. mStart += mLen;
  214. mBuffer[mStart] = c;
  215. mBuffer[mStart+1] = 0;
  216. mStart += 1;
  217. mLen = 0;
  218. }
  219. /// Push the stack, placing a zero-length string on the top.
  220. void push()
  221. {
  222. advanceChar(0);
  223. }
  224. inline void setLen(U32 newlen)
  225. {
  226. mLen = newlen;
  227. }
  228. /// Pop the start stack.
  229. void rewind()
  230. {
  231. mStart = mStartOffsets[--mStartStackSize];
  232. mLen = dStrlen(mBuffer + mStart);
  233. }
  234. // Terminate the current string, and pop the start stack.
  235. void rewindTerminate()
  236. {
  237. mBuffer[mStart] = 0;
  238. mStart = mStartOffsets[--mStartStackSize];
  239. mLen = dStrlen(mBuffer + mStart);
  240. }
  241. /// Compare 1st and 2nd items on stack, consuming them in the process,
  242. /// and returning true if they matched, false if they didn't.
  243. U32 compare()
  244. {
  245. // Figure out the 1st and 2nd item offsets.
  246. U32 oldStart = mStart;
  247. mStart = mStartOffsets[--mStartStackSize];
  248. // Compare current and previous strings.
  249. U32 ret = !dStricmp(mBuffer + mStart, mBuffer + oldStart);
  250. // Put an empty string on the top of the stack.
  251. mLen = 0;
  252. mBuffer[mStart] = 0;
  253. return ret;
  254. }
  255. void pushFrame()
  256. {
  257. //Con::printf("StringStack pushFrame [frame=%i, start=%i]", mNumFrames, mStartStackSize);
  258. mFrameOffsets[mNumFrames++] = mStartStackSize;
  259. mStartOffsets[mStartStackSize++] = mStart;
  260. mStart += ReturnBufferSpace;
  261. validateBufferSize(0);
  262. }
  263. void popFrame()
  264. {
  265. //Con::printf("StringStack popFrame [frame=%i, start=%i]", mNumFrames, mStartStackSize);
  266. mStartStackSize = mFrameOffsets[--mNumFrames];
  267. mStart = mStartOffsets[mStartStackSize];
  268. mLen = 0;
  269. }
  270. void clearFrames()
  271. {
  272. //Con::printf("StringStack clearFrames");
  273. mNumFrames = 0;
  274. mStart = 0;
  275. mLen = 0;
  276. mStartStackSize = 0;
  277. mFunctionOffset = 0;
  278. }
  279. /// Get the arguments for a function call from the stack.
  280. void getArgcArgv(StringTableEntry name, U32 *argc, const char ***in_argv, bool popStackFrame = false);
  281. };
  282. // New console value stack
  283. class ConsoleValueStack
  284. {
  285. enum {
  286. MaxStackDepth = 1024,
  287. MaxArgs = 20,
  288. ReturnBufferSpace = 512
  289. };
  290. public:
  291. ConsoleValueStack();
  292. ~ConsoleValueStack();
  293. void pushVar(ConsoleValue *variable);
  294. void pushValue(ConsoleValue &value);
  295. ConsoleValue* reserveValues(U32 numValues);
  296. bool reserveValues(U32 numValues, ConsoleValueRef *values);
  297. ConsoleValue* pop();
  298. ConsoleValue *pushString(const char *value);
  299. ConsoleValue *pushStackString(const char *value);
  300. ConsoleValue *pushStringStackPtr(StringStackPtr ptr);
  301. ConsoleValue *pushUINT(U32 value);
  302. ConsoleValue *pushFLT(float value);
  303. void pushFrame();
  304. void popFrame();
  305. void resetFrame();
  306. void clearFrames();
  307. void getArgcArgv(StringTableEntry name, U32 *argc, ConsoleValueRef **in_argv, bool popStackFrame = false);
  308. ConsoleValue mStack[MaxStackDepth];
  309. U32 mStackFrames[MaxStackDepth];
  310. U32 mFrame;
  311. U32 mStackPos;
  312. ConsoleValueRef mArgv[MaxArgs];
  313. };
  314. extern StringStack STR;
  315. extern ConsoleValueStack CSTK;
  316. inline char* StringStackPtrRef::getPtr(StringStack *stk) { return stk->mBuffer + mOffset; }
  317. #endif