stringStack.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. default:
  81. mStack[mStackPos++].setStringValue(variable.getStringValue());
  82. }
  83. }
  84. ConsoleValue* ConsoleValueStack::reserveValues(U32 count)
  85. {
  86. U32 startPos = mStackPos;
  87. if (startPos+count >= ConsoleValueStack::MaxStackDepth) {
  88. AssertFatal(false, "Console Value Stack is empty");
  89. return NULL;
  90. }
  91. //Con::printf("[%i]CSTK reserveValues %i", mStackPos, count);
  92. mStackPos += count;
  93. return &mStack[startPos];
  94. }
  95. bool ConsoleValueStack::reserveValues(U32 count, ConsoleValueRef *outValues)
  96. {
  97. U32 startPos = mStackPos;
  98. if (startPos+count >= ConsoleValueStack::MaxStackDepth) {
  99. AssertFatal(false, "Console Value Stack is empty");
  100. return false;
  101. }
  102. //Con::printf("[%i]CSTK reserveValues %i", mStackPos, count);
  103. for (U32 i=0; i<count; i++)
  104. {
  105. outValues[i].value = &mStack[mStackPos+i];
  106. }
  107. mStackPos += count;
  108. return true;
  109. }
  110. ConsoleValue *ConsoleValueStack::pushString(const char *value)
  111. {
  112. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  113. AssertFatal(false, "Console Value Stack is empty");
  114. return NULL;
  115. }
  116. //Con::printf("[%i]CSTK pushString %s", mStackPos, value);
  117. mStack[mStackPos++].setStringValue(value);
  118. return &mStack[mStackPos-1];
  119. }
  120. ConsoleValue *ConsoleValueStack::pushStackString(const char *value)
  121. {
  122. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  123. AssertFatal(false, "Console Value Stack is empty");
  124. return NULL;
  125. }
  126. //Con::printf("[%i]CSTK pushString %s", mStackPos, value);
  127. mStack[mStackPos++].setStackStringValue(value);
  128. return &mStack[mStackPos-1];
  129. }
  130. ConsoleValue *ConsoleValueStack::pushUINT(U32 value)
  131. {
  132. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  133. AssertFatal(false, "Console Value Stack is empty");
  134. return NULL;
  135. }
  136. //Con::printf("[%i]CSTK pushUINT %i", mStackPos, value);
  137. mStack[mStackPos++].setIntValue(value);
  138. return &mStack[mStackPos-1];
  139. }
  140. ConsoleValue *ConsoleValueStack::pushFLT(float value)
  141. {
  142. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  143. AssertFatal(false, "Console Value Stack is empty");
  144. return NULL;
  145. }
  146. //Con::printf("[%i]CSTK pushFLT %f", mStackPos, value);
  147. mStack[mStackPos++].setFloatValue(value);
  148. return &mStack[mStackPos-1];
  149. }
  150. static ConsoleValue gNothing;
  151. ConsoleValue* ConsoleValueStack::pop()
  152. {
  153. if (mStackPos == 0) {
  154. AssertFatal(false, "Console Value Stack is empty");
  155. return &gNothing;
  156. }
  157. return &mStack[--mStackPos];
  158. }
  159. void ConsoleValueStack::pushFrame()
  160. {
  161. //Con::printf("CSTK pushFrame[%i] (%i)", mFrame, mStackPos);
  162. mStackFrames[mFrame++] = mStackPos;
  163. }
  164. void ConsoleValueStack::resetFrame()
  165. {
  166. if (mFrame == 0) {
  167. mStackPos = 0;
  168. return;
  169. }
  170. U32 start = mStackFrames[mFrame-1];
  171. //for (U32 i=start; i<mStackPos; i++) {
  172. //mStack[i].clear();
  173. //}
  174. mStackPos = start;
  175. //Con::printf("CSTK resetFrame to %i", mStackPos);
  176. }
  177. void ConsoleValueStack::clearFrames()
  178. {
  179. mStackPos = 0;
  180. mFrame = 0;
  181. }
  182. void ConsoleValueStack::popFrame()
  183. {
  184. //Con::printf("CSTK popFrame");
  185. if (mFrame == 0) {
  186. // Go back to start
  187. mStackPos = 0;
  188. return;
  189. }
  190. U32 start = mStackFrames[mFrame-1];
  191. //for (U32 i=start; i<mStackPos; i++) {
  192. //mStack[i].clear();
  193. //}
  194. mStackPos = start;
  195. mFrame--;
  196. }