stringStack.h

Engine/source/console/stringStack.h

More...

Classes:

class

Core stack for interpreter operations.

class

Helper class which stores a relative pointer in the StringStack buffer.

Public Typedefs

StringStackPtr 

Detailed Description

Public Typedefs

typedef U32 StringStackPtr 

Public Variables

ConsoleValueStack CSTK 
StringStack STR 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  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
 24#ifndef _STRINGSTACK_H_
 25#define _STRINGSTACK_H_
 26
 27#ifndef _STRINGFUNCTIONS_H_
 28#include "core/strings/stringFunctions.h"
 29#endif
 30
 31#ifndef _STRINGTABLE_H_
 32#include "core/stringTable.h"
 33#endif
 34
 35#ifndef _CONSOLE_H_
 36#include "console/console.h"
 37#endif
 38
 39typedef U32 StringStackPtr;
 40struct StringStack;
 41
 42/// Helper class which stores a relative pointer in the StringStack buffer
 43class StringStackPtrRef
 44{
 45public:
 46   StringStackPtrRef() : mOffset(0) {;}
 47   StringStackPtrRef(StringStackPtr offset) : mOffset(offset) {;}
 48
 49   StringStackPtr mOffset;
 50
 51   /// Get pointer to string in stack stk
 52   inline char *getPtr(StringStack *stk);
 53};
 54
 55/// Core stack for interpreter operations.
 56///
 57/// This class provides some powerful semantics for working with strings, and is
 58/// used heavily by the console interpreter.
 59struct StringStack
 60{
 61   enum {
 62      MaxStackDepth = 1024,
 63      MaxArgs = 20,
 64      ReturnBufferSpace = 512
 65   };
 66   char *mBuffer;
 67   U32   mBufferSize;
 68   const char *mArgV[MaxArgs];
 69   U32 mFrameOffsets[MaxStackDepth];
 70   U32 mStartOffsets[MaxStackDepth];
 71
 72   U32 mNumFrames;
 73   U32 mArgc;
 74
 75   U32 mStart;
 76   U32 mLen;
 77   U32 mStartStackSize;
 78   U32 mFunctionOffset;
 79
 80   U32 mArgBufferSize;
 81   char *mArgBuffer;
 82
 83   void validateBufferSize(U32 size);
 84   void validateArgBufferSize(U32 size);
 85
 86   StringStack();
 87   ~StringStack();
 88
 89   /// Set the top of the stack to be an integer value.
 90   void setIntValue(U32 i);
 91
 92   /// Set the top of the stack to be a float value.
 93   void setFloatValue(F64 v);
 94
 95   /// Return a temporary buffer we can use to return data.
 96   char* getReturnBuffer(U32 size);
 97
 98   /// Return a buffer we can use for arguments.
 99   ///
100   /// This updates the function offset.
101   char *getArgBuffer(U32 size);
102
103   /// Clear the function offset.
104   void clearFunctionOffset();
105
106   /// Set a string value on the top of the stack.
107   void setStringValue(const char *s);
108
109   /// Get the top of the stack, as a StringTableEntry.
110   ///
111   /// @note Don't free this memory!
112   inline StringTableEntry getSTValue()
113   {
114      return StringTable->insert(mBuffer + mStart);
115   }
116
117   /// Get an integer representation of the top of the stack.
118   inline U32 getIntValue()
119   {
120      return dAtoi(mBuffer + mStart);
121   }
122
123   /// Get a float representation of the top of the stack.
124   inline F64 getFloatValue()
125   {
126      return dAtof(mBuffer + mStart);
127   }
128
129   /// Get a string representation of the top of the stack.
130   ///
131   /// @note This returns a pointer to the actual top of the stack, be careful!
132   inline const char *getStringValue()
133   {
134      return mBuffer + mStart;
135   }
136
137   inline const char *getPreviousStringValue()
138   {
139      return mBuffer + mStartOffsets[mStartStackSize-1];
140   }
141
142   inline StringStackPtr getStringValuePtr()
143   {
144      return (getStringValue() - mBuffer);
145   }
146
147   inline StringStackPtr getPreviousStringValuePtr()
148   {
149      return (getPreviousStringValue() - mBuffer);
150   }
151
152   /// Advance the start stack, placing a zero length string on the top.
153   ///
154   /// @note You should use StringStack::push, not this, if you want to
155   ///       properly push the stack.
156   void advance();
157
158   /// Advance the start stack, placing a single character, null-terminated strong
159   /// on the top.
160   ///
161   /// @note You should use StringStack::push, not this, if you want to
162   ///       properly push the stack.
163   void advanceChar(char c);
164
165   /// Push the stack, placing a zero-length string on the top.
166   void push();
167
168   inline void setLen(U32 newlen)
169   {
170      mLen = newlen;
171   }
172
173   /// Pop the start stack.
174   void rewind();
175
176   // Terminate the current string, and pop the start stack.
177   void rewindTerminate();
178
179   /// Compare 1st and 2nd items on stack, consuming them in the process,
180   /// and returning true if they matched, false if they didn't.
181   U32 compare();
182
183   void pushFrame();
184
185   void popFrame();
186
187   void clearFrames();
188
189   /// Get the arguments for a function call from the stack.
190   void getArgcArgv(StringTableEntry name, U32 *argc, const char ***in_argv, bool popStackFrame = false);
191};
192
193
194// New console value stack
195class ConsoleValueStack
196{
197   enum {
198      MaxStackDepth = 1024,
199      MaxArgs = 20,
200      ReturnBufferSpace = 512
201   };
202
203public:
204   ConsoleValueStack();
205   ~ConsoleValueStack();
206
207   void pushVar(ConsoleValue *variable);
208   void pushValue(ConsoleValue &value);
209   ConsoleValue* reserveValues(U32 numValues);
210   bool reserveValues(U32 numValues, ConsoleValueRef *values);
211   ConsoleValue* pop();
212
213   ConsoleValue *pushString(const char *value);
214   ConsoleValue *pushStackString(const char *value);
215   ConsoleValue *pushStringStackPtr(StringStackPtr ptr);
216   ConsoleValue *pushUINT(U32 value);
217   ConsoleValue *pushFLT(float value);
218
219   void pushFrame();
220   void popFrame();
221
222   void resetFrame();
223   void clearFrames();
224
225   void getArgcArgv(StringTableEntry name, U32 *argc, ConsoleValueRef **in_argv, bool popStackFrame = false);
226
227   ConsoleValue mStack[MaxStackDepth];
228   U32 mStackFrames[MaxStackDepth];
229
230   U32 mFrame;
231   U32 mStackPos;
232
233   ConsoleValueRef mArgv[MaxArgs];
234};
235
236extern StringStack STR;
237extern ConsoleValueStack CSTK;
238
239inline char* StringStackPtrRef::getPtr(StringStack *stk) { return stk->mBuffer + mOffset; }
240
241#endif
242