stringStack.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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] = name;
  31. for(U32 i = 0; i < argCount; i++) {
  32. ConsoleValueRef *ref = &mArgv[i+1];
  33. ref->value = &mStack[startStack + i];
  34. ref->stringStackValue = NULL;
  35. }
  36. argCount++;
  37. *argc = argCount;
  38. if(popStackFrame)
  39. popFrame();
  40. }
  41. ConsoleValueStack::ConsoleValueStack() :
  42. mFrame(0),
  43. mStackPos(0)
  44. {
  45. for (int i=0; i<ConsoleValueStack::MaxStackDepth; i++) {
  46. mStack[i].init();
  47. mStack[i].type = ConsoleValue::TypeInternalString;
  48. }
  49. }
  50. ConsoleValueStack::~ConsoleValueStack()
  51. {
  52. }
  53. void ConsoleValueStack::pushVar(ConsoleValue *variable)
  54. {
  55. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  56. AssertFatal(false, "Console Value Stack is empty");
  57. return;
  58. }
  59. switch (variable->type)
  60. {
  61. case ConsoleValue::TypeInternalInt:
  62. mStack[mStackPos++].setIntValue((S32)variable->getIntValue());
  63. case ConsoleValue::TypeInternalFloat:
  64. mStack[mStackPos++].setFloatValue((F32)variable->getFloatValue());
  65. default:
  66. mStack[mStackPos++].setStackStringValue(variable->getStringValue());
  67. }
  68. }
  69. void ConsoleValueStack::pushValue(ConsoleValue &variable)
  70. {
  71. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  72. AssertFatal(false, "Console Value Stack is empty");
  73. return;
  74. }
  75. switch (variable.type)
  76. {
  77. case ConsoleValue::TypeInternalInt:
  78. mStack[mStackPos++].setIntValue((S32)variable.getIntValue());
  79. case ConsoleValue::TypeInternalFloat:
  80. mStack[mStackPos++].setFloatValue((F32)variable.getFloatValue());
  81. default:
  82. mStack[mStackPos++].setStringValue(variable.getStringValue());
  83. }
  84. }
  85. ConsoleValue *ConsoleValueStack::pushString(const char *value)
  86. {
  87. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  88. AssertFatal(false, "Console Value Stack is empty");
  89. return NULL;
  90. }
  91. //Con::printf("[%i]CSTK pushString %s", mStackPos, value);
  92. mStack[mStackPos++].setStringValue(value);
  93. return &mStack[mStackPos-1];
  94. }
  95. ConsoleValue *ConsoleValueStack::pushStackString(const char *value)
  96. {
  97. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  98. AssertFatal(false, "Console Value Stack is empty");
  99. return NULL;
  100. }
  101. //Con::printf("[%i]CSTK pushString %s", mStackPos, value);
  102. mStack[mStackPos++].setStackStringValue(value);
  103. return &mStack[mStackPos-1];
  104. }
  105. ConsoleValue *ConsoleValueStack::pushUINT(U32 value)
  106. {
  107. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  108. AssertFatal(false, "Console Value Stack is empty");
  109. return NULL;
  110. }
  111. //Con::printf("[%i]CSTK pushUINT %i", mStackPos, value);
  112. mStack[mStackPos++].setIntValue(value);
  113. return &mStack[mStackPos-1];
  114. }
  115. ConsoleValue *ConsoleValueStack::pushFLT(float value)
  116. {
  117. if (mStackPos == ConsoleValueStack::MaxStackDepth) {
  118. AssertFatal(false, "Console Value Stack is empty");
  119. return NULL;
  120. }
  121. //Con::printf("[%i]CSTK pushFLT %f", mStackPos, value);
  122. mStack[mStackPos++].setFloatValue(value);
  123. return &mStack[mStackPos-1];
  124. }
  125. static ConsoleValue gNothing;
  126. ConsoleValue* ConsoleValueStack::pop()
  127. {
  128. if (mStackPos == 0) {
  129. AssertFatal(false, "Console Value Stack is empty");
  130. return &gNothing;
  131. }
  132. return &mStack[--mStackPos];
  133. }
  134. void ConsoleValueStack::pushFrame()
  135. {
  136. //Con::printf("CSTK pushFrame[%i] (%i)", mFrame, mStackPos);
  137. mStackFrames[mFrame++] = mStackPos;
  138. }
  139. void ConsoleValueStack::resetFrame()
  140. {
  141. if (mFrame == 0) {
  142. mStackPos = 0;
  143. return;
  144. }
  145. U32 start = mStackFrames[mFrame-1];
  146. //for (U32 i=start; i<mStackPos; i++) {
  147. //mStack[i].clear();
  148. //}
  149. mStackPos = start;
  150. //Con::printf("CSTK resetFrame to %i", mStackPos);
  151. }
  152. void ConsoleValueStack::clearFrames()
  153. {
  154. mStackPos = 0;
  155. mFrame = 0;
  156. }
  157. void ConsoleValueStack::popFrame()
  158. {
  159. //Con::printf("CSTK popFrame");
  160. if (mFrame == 0) {
  161. // Go back to start
  162. mStackPos = 0;
  163. return;
  164. }
  165. U32 start = mStackFrames[mFrame-1];
  166. //for (U32 i=start; i<mStackPos; i++) {
  167. //mStack[i].clear();
  168. //}
  169. mStackPos = start;
  170. mFrame--;
  171. }