xoreos  0.0.5
stack.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef ENGINES_AURORA_LUA_STACK_H
26 #define ENGINES_AURORA_LUA_STACK_H
27 
28 #include "src/common/ustring.h"
29 
30 #include "src/aurora/lua/types.h"
31 
32 struct lua_State;
33 
34 namespace Aurora {
35 
36 namespace Lua {
37 
38 class Variable;
39 
41 class Stack {
42 public:
43  Stack(lua_State &state);
44  ~Stack();
45 
47  int getSize() const;
48 
50  void pushNil();
52  void pushBoolean(bool value);
54  void pushFloat(float value);
56  void pushInt(int value);
58  void pushString(const char *value);
60  void pushString(const Common::UString &value);
62  void pushTable(const TableRef &value);
64  void pushFunction(const FunctionRef &value);
66  void pushRawUserType(void *value, const Common::UString &type);
67 
68  void pushVariable(const Variable &var);
69  void pushVariables(const Variables &vars);
70 
74  template<typename T>
75  void pushUserType(T &value, const Common::UString &type);
76 
78  bool getBooleanAt(int index) const;
80  float getFloatAt(int index) const;
82  int getIntAt(int index) const;
84  Common::UString getStringAt(int index) const;
86  TableRef getTableAt(int index) const;
88  FunctionRef getFunctionAt(int index) const;
90  void *getRawUserTypeAt(int index, const Common::UString &type = "") const;
91 
95  template<typename T>
96  T *getUserTypeAt(int index, const Common::UString &type = "") const;
97 
98  Variable getVariableAt(int index) const;
99 
101  Common::UString getExactTypeAt(int index) const;
102  Type getTypeAt(int index) const;
103 
104  Variables getVariables() const;
105  Variables getVariablesFromTop(int count) const;
106 
108  bool isNilAt(int index) const;
110  bool isBooleanAt(int index) const;
112  bool isNumberAt(int index) const;
114  bool isStringAt(int index) const;
116  bool isTableAt(int index) const;
118  bool isFunctionAt(int index) const;
122  bool isUserTypeAt(int index, const Common::UString &type = "") const;
123 
124  void registerGCForTopObject();
125 
127  lua_State &getLuaState() const;
128 
129 private:
131  lua_State &_luaState;
132 
134  bool checkIndex(int index) const;
135 };
136 
137 template<typename T>
138 void Stack::pushUserType(T &value, const Common::UString &type) {
139  return pushRawUserType(&value, type);
140 }
141 
142 template<typename T>
143 T *Stack::getUserTypeAt(int index, const Common::UString &type) const {
144  return reinterpret_cast<T *>(getRawUserTypeAt(index, type));
145 }
146 
147 } // End of namespace Lua
148 
149 } // End of namespace Aurora
150 
151 #endif // ENGINES_AURORA_LUA_STACK_H
bool isNilAt(int index) const
Check whether the value with the given index is a nil.
Definition: stack.cpp:263
bool isStringAt(int index) const
Check whether the value at the given index is a string.
Definition: stack.cpp:275
Variables getVariablesFromTop(int count) const
Definition: stack.cpp:254
TableRef getTableAt(int index) const
Return a table at the given index in the stack.
Definition: stack.cpp:162
bool checkIndex(int index) const
Check whether the given index is valid.
Definition: stack.cpp:305
A class holding an UTF-8 string.
Definition: ustring.h:48
Stack(lua_State &state)
Definition: stack.cpp:40
void pushVariables(const Variables &vars)
Definition: stack.cpp:128
Common::UString getStringAt(int index) const
Return a string at the given index in the stack.
Definition: stack.cpp:155
void pushInt(int value)
Push an integer value onto the stack.
Definition: stack.cpp:64
Variable getVariableAt(int index) const
Definition: stack.cpp:185
T * getUserTypeAt(int index, const Common::UString &type="") const
Return a usertype value at the given index in the stack.
Definition: stack.h:143
bool isBooleanAt(int index) const
Check whether the value at the given index is a boolean value.
Definition: stack.cpp:267
int getSize() const
Return the number of elements in the stack.
Definition: stack.cpp:48
FunctionRef getFunctionAt(int index) const
Return a function at the given index in the stack.
Definition: stack.cpp:169
Common::UString getExactTypeAt(int index) const
Return the type of the value at the given index in the stack.
Definition: stack.cpp:208
void pushUserType(T &value, const Common::UString &type)
Push a usertype value onto the stack.
Definition: stack.h:138
void pushFunction(const FunctionRef &value)
Push a function onto the stack.
Definition: stack.cpp:85
void pushFloat(float value)
Push a float value onto the stack.
Definition: stack.cpp:60
void registerGCForTopObject()
Definition: stack.cpp:296
bool getBooleanAt(int index) const
Return a boolean value at the given index in the stack.
Definition: stack.cpp:134
void * getRawUserTypeAt(int index, const Common::UString &type="") const
Return a raw usertype value at the given index in the stack.
Definition: stack.cpp:176
void pushTable(const TableRef &value)
Push a table onto the stack.
Definition: stack.cpp:76
Type getTypeAt(int index) const
Definition: stack.cpp:218
A Lua stack wrapper.
Definition: stack.h:41
std::vector< Variable > Variables
Definition: types.h:50
void pushRawUserType(void *value, const Common::UString &type)
Push a raw usertype value onto the stack.
Definition: stack.cpp:94
Lua types.
A reference to a Lua function.
Definition: function.h:35
void pushBoolean(bool value)
Push a boolean value onto the stack.
Definition: stack.cpp:56
Unicode string handling.
lua_State & _luaState
The Lua state.
Definition: stack.h:131
Variables getVariables() const
Definition: stack.cpp:246
bool isFunctionAt(int index) const
Check whether the value at the given index is a function.
Definition: stack.cpp:283
void pushNil()
Push a nil value onto the stack.
Definition: stack.cpp:52
A reference to a Lua table.
Definition: table.h:37
void pushVariable(const Variable &var)
Definition: stack.cpp:98
int getIntAt(int index) const
Return an integer value at the given index in the stack.
Definition: stack.cpp:148
float getFloatAt(int index) const
Return a float value at the given index in the stack.
Definition: stack.cpp:141
bool isTableAt(int index) const
Check whether the value at the given index is a table.
Definition: stack.cpp:279
bool isNumberAt(int index) const
Check whether the value at the given index is a number.
Definition: stack.cpp:271
bool isUserTypeAt(int index, const Common::UString &type="") const
Check whether the value at the given index is a usertype value.
Definition: stack.cpp:287
lua_State & getLuaState() const
Return the underlying Lua state.
Definition: stack.cpp:301
void pushString(const char *value)
Push a raw C string onto the stack.
Definition: stack.cpp:68