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