UGDK
|
00001 00002 #ifndef UGDK_SCRIPT_LUA_DATAGEAR_H_ 00003 #define UGDK_SCRIPT_LUA_DATAGEAR_H_ 00004 00005 #include <ugdk/script/type.h> 00006 #include <ugdk/script/languages/lua/header.h> 00007 #include <ugdk/script/languages/lua/state.h> 00008 #include <ugdk/script/languages/lua/basegear.h> 00009 #include <ugdk/util/uncopyable.h> 00010 #include <ugdk/util/idgenerator.h> 00011 00012 namespace ugdk { 00013 namespace script { 00014 namespace lua { 00015 00016 class DataGear : public BaseGear, private ugdk::util::Uncopyable { 00017 00018 public: 00019 00020 /*DataGear(lua_State* L, DataID datatable_id) : 00021 BaseGear(L), 00022 datatable_id_(datatable_id) {}*/ 00023 00024 DataGear(lua_State *L) : 00025 BaseGear(L), 00026 idgen_(1, INT_MAX, 0) {} 00027 00028 ~DataGear() { 00029 L_.pushnil(); 00030 L_.setfield(Constant::REGISTRYINDEX(), "UGDK_LUA_DATATABLE"); 00031 /*L_.aux().unref(Constant::REGISTRYINDEX(), datatable_id_); 00032 datatable_id_ = LUA_NOREF;*/ 00033 L_.close(); 00034 } 00035 00037 static int GenerateID(lua_State* L); 00038 00040 static int DestroyID(lua_State* L); 00041 00043 static int WrapData(lua_State* L); 00044 00046 template <class T> 00047 static int WrapPrimitive(lua_State* L); 00048 00050 static int UnwrapData(lua_State* L); 00051 00053 template <class T> 00054 static int UnwrapPrimitive(lua_State* L); 00055 00056 static int UnwrapList(lua_State* L); 00057 static int UnwrapTable(lua_State* L); 00058 00060 00067 static int Execute(lua_State* L); 00068 00070 00077 static int GetField(lua_State* L); 00078 00080 00088 static int SetField(lua_State* L); 00089 00090 static int DoFile(lua_State* L); 00091 00092 static int DoString(lua_State* L); 00093 00094 // [-0,+1] 00095 bool GetData (DataID id); 00096 00097 // [-1,+0] 00098 bool SetData (DataID id); 00099 00100 private: 00101 00102 //DataID datatable_id_; 00103 ugdk::util::IDGenerator idgen_; 00104 00105 DataGear& operator=(const DataGear& rhs) { 00106 return *this; 00107 } 00108 00110 bool PushDataTable(); 00111 00113 void PushData (int table_index, DataID id) { 00114 L_.rawgeti(table_index, id); 00115 } 00116 00118 void PopData (int table_index, DataID id) { 00119 L_.rawseti(table_index, id); 00120 } 00121 00122 }; 00123 00124 template <class T> 00125 int DataGear::UnwrapPrimitive(lua_State* L) { 00126 State L_(L); 00127 00128 L_.settop(2); 00129 GETARG(L_, 1, DataGear, dtgear); 00130 DataID id = L_.aux().checkintteger(2); 00131 L_.settop(0); 00132 00133 if (!dtgear.GetData(id)) { 00134 L_.pushnil(); 00135 } // else the data will already be on top 00136 00137 if (!L_.isprimitive<T>(-1)) 00138 return luaL_error(L, "Could not unwrap primitive from id #%d", id); 00139 00140 return 1; 00141 } 00142 00143 template <class T> 00144 int DataGear::WrapPrimitive(lua_State* L) { 00145 State L_(L); 00146 00147 L_.settop(3); 00148 GETARG(L_, 1, DataGear, dtgear); 00149 DataID id = L_.aux().checkintteger(2); 00150 //T value = L_.aux().checkprimitive<T>(3); 00151 //UData data = L_.touserdata(3); 00152 //GETARGPTR(L_, 4, swig_type_info, type); 00153 //L_.settop(0); 00154 00155 if (!L_.isprimitive<T>(3) || !dtgear.SetData(id)) 00156 return luaL_error(L, "Could not wrap primitive with id #%d", id); 00157 00158 //SWIG_NewPointerObj(L_, data, type, 0); 00159 //if (!dtgear.SetData(id)) 00160 // return luaL_error(L, "Could not wrap primitive with id #%d", id); 00161 00162 return 0; 00163 } 00164 00165 } /* namespace lua */ 00166 } /* namespace script */ 00167 } /* namespace ugdk */ 00168 00169 #endif /* UGDK_SCRIPT_LUA_DATAGEAR_H_ */