UGDK
src/ugdk/script/languages/lua/state.h
Go to the documentation of this file.
00001 
00002 #ifndef UGDK_SCRIPT_LUA_STATE_H_
00003 #define UGDK_SCRIPT_LUA_STATE_H_
00004 
00005 //#include <ugdk/portable/tr1.h>
00006 
00007 #include <cstdlib>
00008 #include <cstdio>
00009 
00010 #include <ugdk/portable/tr1.h>
00011 #include FROM_TR1(functional)
00012 
00013 #include <ugdk/script/languages/lua/header.h>
00014 #include <ugdk/script/languages/lua/defs.h>
00015 #include <ugdk/script/languages/lua/primitive.h>
00016 #include <ugdk/script/languages/lua/auxlib.h>
00017 
00018 namespace ugdk {
00019 namespace script {
00020 namespace lua {
00021 
00022 class State {
00023 
00024   public:
00025 
00026     State (lua_State* L) :
00027       L_(L),
00028       auxlib_(L) {}
00029 
00030     operator bool() const { return !!(L_); }
00031 
00032     operator lua_State*() const { return L_; }
00033 
00034     void close() { lua_close(L_); L_ = NULL; }
00035 
00036     AuxLib& aux() { return auxlib_; }
00037 
00038     int gettop() const { return lua_gettop(L_); }
00039     void settop(int index) { lua_settop(L_, index); }
00040 
00041     void pushvalue (int index) { lua_pushvalue(L_, index); }
00042     void pushnil () { lua_pushnil(L_); }
00043     void pushboolean (bool b) { lua_pushboolean(L_, b); }
00044     void pushinteger (lua_Integer integer) { lua_pushinteger(L_, integer); }
00045     void pushnumber (lua_Number number) { lua_pushnumber(L_, number); }
00046     void pushudata (UData ptr) { lua_pushlightuserdata(L_, ptr); }
00047     void pushstring (const char* str) { lua_pushstring(L_, str); }
00048     void pushcfunction (lua_CFunction func, int n = 0) {
00049         lua_pushcclosure(L_, func, n);
00050     }
00051     template <class T>
00052     void pushudata (T* value) { pushudata(AsUData(value)); }
00053     template <class T>
00054     void pushprimitive(T value) { lua_push<T>::primitive(L_, value); }
00055 
00056     void pop (int n) { lua_pop(L_, n); }
00057 
00058     void insert (int index) { lua_insert(L_, index); }
00059     void remove (int index) { lua_remove(L_, index); }
00060 
00061     void newtable () { lua_newtable(L_); }
00062 
00063     void getglobal (const char* name) { lua_getglobal(L_, name); }
00064     void getfield (int index, const char* k) { lua_getfield(L_, index, k); }
00065     void setfield (int index, const char* k) { lua_setfield(L_, index, k); }
00066 
00067     void gettable (int index) { lua_gettable(L_, index); }
00068     void settable (int index) { lua_settable(L_, index); }
00069     void rawgeti (int index, int n) { lua_rawgeti(L_, index, n); }
00070     void rawseti (int index, int n) { lua_rawseti(L_, index, n); }
00071 
00072     int setfenv(int index) { return lua_setfenv(L_, index); }
00073     void getfenv(int index) { lua_getfenv(L_, index); }
00074 
00075     int getmetatable(int index) { return lua_getmetatable(L_, index); }
00076     int setmetatable(int index) { return lua_setmetatable(L_, index); }
00077 
00078     template <class T>
00079     bool isprimitive(int index) const {
00080         return lua_is<T>::primitive(L_, index);
00081     }
00082     bool isnil (int index) const { return !!(lua_isnil(L_, index)); }
00083     bool isstring (int index) const { return !!(lua_isstring(L_, index)); }
00084     bool isfunction (int index) const { return !!(lua_isfunction(L_, index)); }
00085     bool istable (int index) const { return !!(lua_istable(L_, index)); }
00086 
00087 
00088     template <class T>
00089     T toprimitive(int n) const { return lua_to<T>::primitive(L_, n); }
00090     bool toboolean(int n) const { return !!(lua_toboolean(L_, n)); }
00091     lua_Integer tointeger(int n) const { return lua_tointeger(L_, n); }
00092     const char* tostring(int n) const { return lua_tostring(L_, n); }
00093     void* touserdata(int n) const { return lua_touserdata(L_, n); }
00094 
00095     int type (int n) const { return lua_type(L_, n); }
00096     
00097     void call (int nargs, int nres) { lua_call(L_, nargs, nres); }
00098     const Constant pcall (int nargs, int nres, int errfunc) {
00099         return Constant(
00100             std::tr1::bind(lua_pcall, L_, nargs, nres, errfunc)
00101         );
00102     }
00103 
00104     int gc (Constant what, int data) {
00105         return lua_gc(L_, what.value(), data);
00106     }
00107 
00108   private:
00109 
00110     lua_State*  L_;
00111     AuxLib      auxlib_;
00112 
00113 };
00114 
00115 } /* namespace lua */
00116 } /* namespace script */
00117 } /* namespace ugdk */
00118 
00119 #endif /* UGDK_SCRIPT_LUA_STATE_H_ */
00120 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines