UGDK  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
state.h
Go to the documentation of this file.
1 
2 #ifndef UGDK_SCRIPT_LUA_STATE_H_
3 #define UGDK_SCRIPT_LUA_STATE_H_
4 
5 #include <cstdlib>
6 #include <cstdio>
7 #include <functional>
8 
13 
14 namespace ugdk {
15 namespace script {
16 namespace lua {
17 
18 class State {
19 
20  public:
21 
23  L_(L),
24  auxlib_(L) {}
25 
26  operator bool() const { return !!(L_); }
27 
28  operator lua_State*() const { return L_; }
29 
30  void close() { lua_close(L_); L_ = nullptr; }
31 
32  AuxLib& aux() { return auxlib_; }
33 
34  int gettop() const { return lua_gettop(L_); }
35  void settop(int index) { lua_settop(L_, index); }
36 
37  void pushvalue (int index) { lua_pushvalue(L_, index); }
38  void pushnil () { lua_pushnil(L_); }
39  void pushboolean (bool b) { lua_pushboolean(L_, b); }
40  void pushinteger (lua_Integer integer) { lua_pushinteger(L_, integer); }
41  void pushnumber (lua_Number number) { lua_pushnumber(L_, number); }
42  void pushudata (UData ptr) { lua_pushlightuserdata(L_, ptr); }
43  void pushstring (const char* str) { lua_pushstring(L_, str); }
44  void pushcfunction (lua_CFunction func, int n = 0) {
45  lua_pushcclosure(L_, func, n);
46  }
47  template <class T>
48  void pushudata (T* value) { pushudata(AsUData(value)); }
49  template <class T>
50  void pushprimitive(T value) { lua_push<T>::primitive(L_, value); }
51 
52  void pop (int n) { lua_pop(L_, n); }
53 
54  void insert (int index) { lua_insert(L_, index); }
55  void remove (int index) { lua_remove(L_, index); }
56 
57  void newtable () { lua_newtable(L_); }
58 
59  void getglobal (const char* name) { lua_getglobal(L_, name); }
60  void getfield (int index, const char* k) { lua_getfield(L_, index, k); }
61  void setfield (int index, const char* k) { lua_setfield(L_, index, k); }
62 
63  void gettable (int index) { lua_gettable(L_, index); }
64  void settable (int index) { lua_settable(L_, index); }
65  void rawgeti (int index, int n) { lua_rawgeti(L_, index, n); }
66  void rawseti (int index, int n) { lua_rawseti(L_, index, n); }
67 
68  int setfenv(int index) { return lua_setfenv(L_, index); }
69  void getfenv(int index) { lua_getfenv(L_, index); }
70 
71  int getmetatable(int index) { return lua_getmetatable(L_, index); }
72  int setmetatable(int index) { return lua_setmetatable(L_, index); }
73 
74  template <class T>
75  bool isprimitive(int index) const {
76  return lua_is<T>::primitive(L_, index);
77  }
78  bool isnil (int index) const { return !!(lua_isnil(L_, index)); }
79  bool isstring (int index) const { return !!(lua_isstring(L_, index)); }
80  bool isfunction (int index) const { return !!(lua_isfunction(L_, index)); }
81  bool istable (int index) const { return !!(lua_istable(L_, index)); }
82 
83 
84  template <class T>
85  T toprimitive(int n) const { return lua_to<T>::primitive(L_, n); }
86  bool toboolean(int n) const { return !!(lua_toboolean(L_, n)); }
87  lua_Integer tointeger(int n) const { return lua_tointeger(L_, n); }
88  const char* tostring(int n) const { return lua_tostring(L_, n); }
89  void* touserdata(int n) const { return lua_touserdata(L_, n); }
90 
91  int type (int n) const { return lua_type(L_, n); }
92 
93  void call (int nargs, int nres) { lua_call(L_, nargs, nres); }
94  const Constant pcall (int nargs, int nres, int errfunc) {
95  return Constant(
96  std::bind(lua_pcall, L_, nargs, nres, errfunc)
97  );
98  }
99 
100  int gc (Constant what, int data) {
101  return lua_gc(L_, what.value(), data);
102  }
103 
104  private:
105 
106  lua_State* L_;
107  AuxLib auxlib_;
108 
109 };
110 
111 } /* namespace lua */
112 } /* namespace script */
113 } /* namespace ugdk */
114 
115 #endif /* UGDK_SCRIPT_LUA_STATE_H_ */
116 
State(lua_State *L)
Definition: state.h:22
void pushboolean(bool b)
Definition: state.h:39
void * touserdata(int n) const
Definition: state.h:89
void getglobal(const char *name)
Definition: state.h:59
void pushcfunction(lua_CFunction func, int n=0)
Definition: state.h:44
void getfield(int index, const char *k)
Definition: state.h:60
void newtable()
Definition: state.h:57
int type(int n) const
Definition: state.h:91
int getmetatable(int index)
Definition: state.h:71
void pushnil()
Definition: state.h:38
bool isfunction(int index) const
Definition: state.h:80
void close()
Definition: state.h:30
bool isprimitive(int index) const
Definition: state.h:75
void gettable(int index)
Definition: state.h:63
void settop(int index)
Definition: state.h:35
void getfenv(int index)
Definition: state.h:69
void pushudata(UData ptr)
Definition: state.h:42
const Constant pcall(int nargs, int nres, int errfunc)
Definition: state.h:94
void pushstring(const char *str)
Definition: state.h:43
Definition: animation.h:11
void pushinteger(lua_Integer integer)
Definition: state.h:40
int gc(Constant what, int data)
Definition: state.h:100
T toprimitive(int n) const
Definition: state.h:85
bool isnil(int index) const
Definition: state.h:78
void pushprimitive(T value)
Definition: state.h:50
void rawseti(int index, int n)
Definition: state.h:66
lua_Integer tointeger(int n) const
Definition: state.h:87
void settable(int index)
Definition: state.h:64
Represents a constant value from the Lua library.
Definition: header.h:25
void pushnumber(lua_Number number)
Definition: state.h:41
bool istable(int index) const
Definition: state.h:81
UData AsUData(T *p)
Definition: defs.h:26
void insert(int index)
Definition: state.h:54
Definition: auxlib.h:17
void call(int nargs, int nres)
Definition: state.h:93
void pushvalue(int index)
Definition: state.h:37
bool toboolean(int n) const
Definition: state.h:86
void pushudata(T *value)
Definition: state.h:48
int setfenv(int index)
Definition: state.h:68
int gettop() const
Definition: state.h:34
void setfield(int index, const char *k)
Definition: state.h:61
struct lua_State lua_State
Definition: defs.h:8
int(* lua_CFunction)(lua_State *)
Definition: defs.h:9
AuxLib & aux()
Definition: state.h:32
void rawgeti(int index, int n)
Definition: state.h:65
int value()
Definition: header.h:38
void pop(int n)
Definition: state.h:52
void * UData
Definition: defs.h:23
const char * tostring(int n) const
Definition: state.h:88
bool isstring(int index) const
Definition: state.h:79
int setmetatable(int index)
Definition: state.h:72
Definition: state.h:18