stringStack.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #include <stdio.h>
  23. #include "console/consoleInternal.h"
  24. #include "console/stringStack.h"
  25. void ConsoleValueStack::getArgcArgv(StringTableEntry name, U32 *argc, ConsoleValueRef **in_argv, bool popStackFrame /* = false */)
  26. {
  27. U32 startStack = mStackFrames[mFrame-1];
  28. U32 argCount = getMin(mStackPos - startStack, (U32)MaxArgs - 1);
  29. *in_argv = mArgv;
  30. mArgv[0].value = CSTK.pushStackString(name);
  31. for(U32 i = 0; i < argCount; i++) {
  32. ConsoleValueRef *ref = &mArgv[i+1];
  33. ref->value = &mStack[startStack + i];
  34. }
  35. argCount++;
  36. *argc = argCount;
  37. if(popStackFrame)
  38. popFrame();
  39. }
  40. ConsoleValueStack::ConsoleValueStack() :
  41. mFrame(0),
  42. mStackPos(0)
  43. {
  44. for (int i=0; i<ConsoleValueStack::MaxStackDepth; i++) {
  45. mStack[i].init();
  46. mStack[i].type = ConsoleValue::TypeInternalString;
  47. }
  48. }
  49. ConsoleValueStack::~ConsoleValueStack()
  50. {
  51. }
  52. void ConsoleValueStack::pushVar(ConsoleValue *variable)
  53. {
  54. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  55. AssertFatal(false, "Console Value Stack is empty");
  56. return;
  57. }
  58. switch (variable->type)
  59. {
  60. case ConsoleValue::TypeInternalInt:
  61. mStack[mStackPos++].setIntValue((S32)variable->getIntValue());
  62. case ConsoleValue::TypeInternalFloat:
  63. mStack[mStackPos++].setFloatValue((F32)variable->getFloatValue());
  64. default:
  65. mStack[mStackPos++].setStackStringValue(variable->getStringValue());
  66. }
  67. }
  68. void ConsoleValueStack::pushValue(ConsoleValue &variable)
  69. {
  70. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  71. AssertFatal(false, "Console Value Stack is empty");
  72. return;
  73. }
  74. switch (variable.type)
  75. {
  76. case ConsoleValue::TypeInternalInt:
  77. mStack[mStackPos++].setIntValue((S32)variable.getIntValue());
  78. case ConsoleValue::TypeInternalFloat:
  79. mStack[mStackPos++].setFloatValue((F32)variable.getFloatValue());
  80. case ConsoleValue::TypeInternalStringStackPtr:
  81. mStack[mStackPos++].setStringStackPtrValue(variable.getStringStackPtr());
  82. default:
  83. mStack[mStackPos++].setStringValue(variable.getStringValue());
  84. }
  85. }
  86. ConsoleValue* ConsoleValueStack::reserveValues(U32 count)
  87. {
  88. U32 startPos = mStackPos;
  89. if (startPos+count >= ConsoleValueStack::MaxStackDepth) {
  90. AssertFatal(false, "Console Value Stack is empty");
  91. return NULL;
  92. }
  93. //Con::printf("[%i]CSTK reserveValues %i", mStackPos, count);
  94. mStackPos += count;
  95. return &mStack[startPos];
  96. }
  97. bool ConsoleValueStack::reserveValues(U32 count, ConsoleValueRef *outValues)
  98. {
  99. U32 startPos = mStackPos;
  100. if (startPos+count >= ConsoleValueStack::MaxStackDepth) {
  101. AssertFatal(false, "Console Value Stack is empty");
  102. return false;
  103. }
  104. //Con::printf("[%i]CSTK reserveValues %i", mStackPos, count);
  105. for (U32 i=0; i<count; i++)
  106. {
  107. outValues[i].value = &mStack[mStackPos+i];
  108. }
  109. mStackPos += count;
  110. return true;
  111. }
  112. ConsoleValue *ConsoleValueStack::pushString(const char *value)
  113. {
  114. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  115. AssertFatal(false, "Console Value Stack is empty");
  116. return NULL;
  117. }
  118. //Con::printf("[%i]CSTK pushString %s", mStackPos, value);
  119. mStack[mStackPos++].setStringValue(value);
  120. return &mStack[mStackPos-1];
  121. }
  122. ConsoleValue *ConsoleValueStack::pushStackString(const char *value)
  123. {
  124. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  125. AssertFatal(false, "Console Value Stack is empty");
  126. return NULL;
  127. }
  128. //Con::printf("[%i]CSTK pushString %s", mStackPos, value);
  129. mStack[mStackPos++].setStackStringValue(value);
  130. return &mStack[mStackPos-1];
  131. }
  132. ConsoleValue *ConsoleValueStack::pushStringStackPtr(StringStackPtr value)
  133. {
  134. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  135. AssertFatal(false, "Console Value Stack is empty");
  136. return NULL;
  137. }
  138. //Con::printf("[%i]CSTK pushStringStackPtr %s", mStackPos, StringStackPtrRef(value).getPtr(&STR));
  139. mStack[mStackPos++].setStringStackPtrValue(value);
  140. return &mStack[mStackPos-1];
  141. }
  142. ConsoleValue *ConsoleValueStack::pushUINT(U32 value)
  143. {
  144. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  145. AssertFatal(false, "Console Value Stack is empty");
  146. return NULL;
  147. }
  148. //Con::printf("[%i]CSTK pushUINT %i", mStackPos, value);
  149. mStack[mStackPos++].setIntValue(value);
  150. return &mStack[mStackPos-1];
  151. }
  152. ConsoleValue *ConsoleValueStack::pushFLT(float value)
  153. {
  154. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  155. AssertFatal(false, "Console Value Stack is empty");
  156. return NULL;
  157. }
  158. //Con::printf("[%i]CSTK pushFLT %f", mStackPos, value);
  159. mStack[mStackPos++].setFloatValue(value);
  160. return &mStack[mStackPos-1];
  161. }
  162. static ConsoleValue gNothing;
  163. ConsoleValue* ConsoleValueStack::pop()
  164. {
  165. if (mStackPos == 0) {
  166. AssertFatal(false, "Console Value Stack is empty");
  167. return &gNothing;
  168. }
  169. return &mStack[--mStackPos];
  170. }
  171. void ConsoleValueStack::pushFrame()
  172. {
  173. //Con::printf("CSTK pushFrame[%i] (%i)", mFrame, mStackPos);
  174. mStackFrames[mFrame++] = mStackPos;
  175. }
  176. void ConsoleValueStack::resetFrame()
  177. {
  178. if (mFrame == 0) {
  179. mStackPos = 0;
  180. return;
  181. }
  182. U32 start = mStackFrames[mFrame-1];
  183. //for (U32 i=start; i<mStackPos; i++) {
  184. //mStack[i].clear();
  185. //}
  186. mStackPos = start;
  187. //Con::printf("CSTK resetFrame to %i", mStackPos);
  188. }
  189. void ConsoleValueStack::clearFrames()
  190. {
  191. mStackPos = 0;
  192. mFrame = 0;
  193. }
  194. void ConsoleValueStack::popFrame()
  195. {
  196. //Con::printf("CSTK popFrame");
  197. if (mFrame == 0) {
  198. // Go back to start
  199. mStackPos = 0;
  200. return;
  201. }
  202. U32 start = mStackFrames[mFrame-1];
  203. //for (U32 i=start; i<mStackPos; i++) {
  204. //mStack[i].clear();
  205. //}
  206. mStackPos = start;
  207. mFrame--;
  208. }