consoleValueStack.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. // Copyright (c) 2021 TGEMIT Authors & Contributors
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #ifndef _CONSOLE_CONSOLE_VALUE_STACK_H_
  24. #define _CONSOLE_CONSOLE_VALUE_STACK_H_
  25. template<S32 StackSize>
  26. class ConsoleValueStack
  27. {
  28. constexpr static S32 allocatorSize = sizeof(ConsoleValue) * StackSize;
  29. struct Frame
  30. {
  31. ConsoleValue* values;
  32. S32 count;
  33. S32 internalCounter;
  34. };
  35. Vector<Frame> stack;
  36. char* memory;
  37. S32 sp;
  38. TORQUE_FORCEINLINE Frame alloc(S32 count)
  39. {
  40. AssertFatal(sp + count * sizeof(ConsoleValue) < allocatorSize, "ConsoleValueStack overflow");
  41. ConsoleValue* ret = reinterpret_cast<ConsoleValue*>(memory + sp);
  42. sp += count * sizeof(ConsoleValue);
  43. return { ret, count, 1 };
  44. }
  45. TORQUE_FORCEINLINE void deAlloc(S32 count)
  46. {
  47. sp -= count * sizeof(ConsoleValue);
  48. AssertFatal(sp >= 0, "Popped ConsoleValueStack too far, underflow");
  49. }
  50. public:
  51. ConsoleValueStack()
  52. {
  53. memory = (char*)dMalloc(allocatorSize);
  54. for (S32 i = 0; i < allocatorSize; i += sizeof(ConsoleValue))
  55. {
  56. constructInPlace<ConsoleValue>(reinterpret_cast<ConsoleValue*>(memory + i));
  57. }
  58. sp = 0;
  59. }
  60. ~ConsoleValueStack()
  61. {
  62. dFree(memory);
  63. }
  64. TORQUE_FORCEINLINE void pushFrame(S32 count)
  65. {
  66. AssertISV(count >= 0, "Must be >= 0 when pushing stack frame");
  67. // +1 for function name in argv[0]
  68. const Frame& frame = alloc(count + 1);
  69. stack.push_back(frame);
  70. }
  71. TORQUE_FORCEINLINE void popFrame()
  72. {
  73. AssertISV(stack.size() > 0, "Stack Underflow");
  74. deAlloc(stack.last().count);
  75. stack.pop_back();
  76. }
  77. TORQUE_FORCEINLINE void push(ConsoleValue&& val)
  78. {
  79. Frame& frame = stack.last();
  80. frame.values[frame.internalCounter++] = std::move(val);
  81. }
  82. TORQUE_FORCEINLINE void argvc(StringTableEntry fn, S32& argc, ConsoleValue** argv)
  83. {
  84. Frame& frame = stack.last();
  85. argc = frame.count;
  86. // First param is always function name
  87. frame.values[0].setStringTableEntry(fn);
  88. *argv = frame.values;
  89. }
  90. };
  91. #endif