xoreos  0.0.5
table.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 AURORA_LUA_TABLE_H
26 #define AURORA_LUA_TABLE_H
27 
28 #include "src/common/ustring.h"
29 
30 #include "src/aurora/lua/types.h"
31 
32 namespace Aurora {
33 
34 namespace Lua {
35 
37 class TableRef {
38 public:
39  TableRef();
40  TableRef(const Stack &stack, int index);
41  TableRef(lua_State &state, int index);
42  TableRef(const TableRef &table);
43  ~TableRef();
44 
45  const TableRef &operator=(const TableRef &table);
46 
48  int getSize() const;
49 
51  TableRef getMetaTable() const;
52 
54  void setMetaAccessEnabled(bool enabled);
55  bool isMetaAccessEnabled() const;
56 
57  void removeAt(int index);
58  void removeAt(const Common::UString &key);
59 
60  void setBooleanAt(int index, bool value);
61  void setBooleanAt(const Common::UString &key, bool value);
62  void setFloatAt(int index, float value);
63  void setFloatAt(const Common::UString &key, float value);
64  void setIntAt(int index, int value);
65  void setIntAt(const Common::UString &key, int value);
66  void setStringAt(int index, const char *value);
67  void setStringAt(const Common::UString &key, const char *value);
68  void setStringAt(int index, const Common::UString &value);
69  void setStringAt(const Common::UString &key, const Common::UString &value);
70  void setTableAt(int index, const TableRef &value);
71  void setTableAt(const Common::UString &key, const TableRef &value);
72  void setFunctionAt(int index, const FunctionRef &value);
73  void setFunctionAt(const Common::UString &key, const FunctionRef &value);
74 
78  void setUserTypeAt(int index, void *value, const Common::UString &type);
79  void setUserTypeAt(const Common::UString &key, void *value, const Common::UString &type);
80 
81  void setVariableAt(int index, const Variable &value);
82  void setVariableAt(const Common::UString &key, const Variable &value);
83 
84  bool getBooleanAt(int index) const;
85  bool getBooleanAt(const Common::UString &key) const;
86  float getFloatAt(int index) const;
87  float getFloatAt(const Common::UString &key) const;
88  int getIntAt(int index) const;
89  int getIntAt(const Common::UString &key) const;
90  Common::UString getStringAt(int index) const;
92  TableRef getTableAt(int index) const;
93  TableRef getTableAt(const Common::UString &key) const;
94  FunctionRef getFunctionAt(int index) const;
95  FunctionRef getFunctionAt(const Common::UString &key) const;
96 
100  void *getRawUserTypeAt(int index, const Common::UString &type = "") const;
101  void *getRawUserTypeAt(const Common::UString &key, const Common::UString &type = "") const;
102 
103  template<typename T>
104  T *getUserTypeAt(int index, const Common::UString &type = "") const;
105  template<typename T>
106  T *getUserTypeAt(const Common::UString &key, const Common::UString &type = "") const;
107 
108  Variable getVariableAt(int index) const;
109  Variable getVariableAt(const Common::UString &key) const;
110 
111  Common::UString getExactTypeAt(int index) const;
113  Type getTypeAt(int index) const;
114  Type getTypeAt(const Common::UString &key) const;
115 
116  Variables getVariables() const;
117 
118  bool isNilAt(int index) const;
119  bool isNilAt(const Common::UString &key) const;
120  bool isBooleanAt(int index) const;
121  bool isBooleanAt(const Common::UString &key) const;
122  bool isNumberAt(int index) const;
123  bool isNumberAt(const Common::UString &key) const;
124  bool isStringAt(int index) const;
125  bool isStringAt(const Common::UString &key) const;
126  bool isTableAt(int index) const;
127  bool isTableAt(const Common::UString &key) const;
128  bool isFunctionAt(int index) const;
129  bool isFunctionAt(const Common::UString &key) const;
130 
134  bool isUserTypeAt(int index, const Common::UString &type = "") const;
135  bool isUserTypeAt(const Common::UString &key, const Common::UString &type = "") const;
136 
138  lua_State &getLuaState() const;
139 
140  int getRef() const;
141 
142 private:
144  lua_State *_luaState;
145  int _ref;
147 
148  void pushSelf() const;
149 };
150 
151 template<typename T>
152 inline T *TableRef::getUserTypeAt(int index, const Common::UString &type) const {
153  return reinterpret_cast<T *>(getRawUserTypeAt(index, type));
154 }
155 
156 template<typename T>
157 inline T *TableRef::getUserTypeAt(const Common::UString &key, const Common::UString &type) const {
158  return reinterpret_cast<T *>(getRawUserTypeAt(key, type));
159 }
160 
161 } // End of namespace Lua
162 
163 } // End of namespace Aurora
164 
165 #endif // AURORA_LUA_TABLE_H
void removeAt(int index)
Definition: table.cpp:134
bool getBooleanAt(int index) const
Definition: table.cpp:248
void setStringAt(int index, const char *value)
Definition: table.cpp:182
A class holding an UTF-8 string.
Definition: ustring.h:48
TableRef getTableAt(int index) const
Definition: table.cpp:312
float getFloatAt(int index) const
Definition: table.cpp:264
bool isUserTypeAt(int index, const Common::UString &type="") const
Check whether the value at the given index is a usertype value.
Definition: table.cpp:500
void setVariableAt(int index, const Variable &value)
Definition: table.cpp:222
Common::UString getExactType() const
Definition: table.cpp:107
const TableRef & operator=(const TableRef &table)
Definition: table.cpp:73
void setUserTypeAt(int index, void *value, const Common::UString &type)
Push a usertype value onto the stack.
Definition: table.cpp:214
FunctionRef getFunctionAt(int index) const
Definition: table.cpp:328
TableRef getMetaTable() const
Definition: table.cpp:116
bool isNumberAt(int index) const
Definition: table.cpp:468
int getRef() const
Definition: table.cpp:522
void setFloatAt(int index, float value)
Definition: table.cpp:166
void setBooleanAt(int index, bool value)
Definition: table.cpp:158
bool _metaAccessEnabled
Definition: table.h:146
T * getUserTypeAt(int index, const Common::UString &type="") const
Definition: table.h:152
bool isNilAt(int index) const
Definition: table.cpp:452
void pushSelf() const
Definition: table.cpp:526
int getSize() const
Return the number of elements in the table.
Definition: table.cpp:94
A Lua stack wrapper.
Definition: stack.h:41
std::vector< Variable > Variables
Definition: types.h:50
void setTableAt(int index, const TableRef &value)
Definition: table.cpp:198
void setFunctionAt(int index, const FunctionRef &value)
Definition: table.cpp:206
Lua types.
A reference to a Lua function.
Definition: function.h:35
bool isFunctionAt(int index) const
Definition: table.cpp:492
Variables getVariables() const
Definition: table.cpp:438
bool isMetaAccessEnabled() const
Definition: table.cpp:130
Unicode string handling.
void setIntAt(int index, int value)
Definition: table.cpp:174
Common::UString getStringAt(int index) const
Definition: table.cpp:296
int getIntAt(int index) const
Definition: table.cpp:280
bool isTableAt(int index) const
Definition: table.cpp:484
lua_State & getLuaState() const
Return the underlying Lua state.
Definition: table.cpp:516
void * getRawUserTypeAt(int index, const Common::UString &type="") const
Return a usertype value at the given index in the stack.
Definition: table.cpp:344
A reference to a Lua table.
Definition: table.h:37
Common::UString getExactTypeAt(int index) const
Definition: table.cpp:386
lua_State * _luaState
The Lua state.
Definition: table.h:144
bool isStringAt(int index) const
Definition: table.cpp:476
bool isBooleanAt(int index) const
Definition: table.cpp:460
Type getTypeAt(int index) const
Definition: table.cpp:412
void setMetaAccessEnabled(bool enabled)
Enable/Disable access to the table elements using metamethods.
Definition: table.cpp:126
Variable getVariableAt(int index) const
Definition: table.cpp:360