UGDK  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
datagear.h
Go to the documentation of this file.
1 
2 #ifndef UGDK_SCRIPT_LUA_DATAGEAR_H_
3 #define UGDK_SCRIPT_LUA_DATAGEAR_H_
4 
5 #include <ugdk/script/type.h>
9 #include <ugdk/util/uncopyable.h>
10 #include <ugdk/util/idgenerator.h>
11 
12 namespace ugdk {
13 namespace script {
14 namespace lua {
15 
16 class DataGear : public BaseGear, private ugdk::util::Uncopyable {
17 
18  public:
19 
20  /*DataGear(lua_State* L, DataID datatable_id) :
21  BaseGear(L),
22  datatable_id_(datatable_id) {}*/
23 
25  BaseGear(L),
26  idgen_(1, INT_MAX, 0) {}
27 
29  L_.pushnil();
30  L_.setfield(Constant::REGISTRYINDEX(), "UGDK_LUA_DATATABLE");
31  /*L_.aux().unref(Constant::REGISTRYINDEX(), datatable_id_);
32  datatable_id_ = LUA_NOREF;*/
33  L_.close();
34  }
35 
37  static int GenerateID(lua_State* L);
38 
40  static int DestroyID(lua_State* L);
41 
43  static int WrapData(lua_State* L);
44 
46  template <class T>
47  static int WrapPrimitive(lua_State* L);
48 
50  static int UnwrapData(lua_State* L);
51 
53  template <class T>
54  static int UnwrapPrimitive(lua_State* L);
55 
56  static int UnwrapList(lua_State* L);
57  static int UnwrapTable(lua_State* L);
58 
60 
67  static int Execute(lua_State* L);
68 
70 
77  static int GetField(lua_State* L);
78 
80 
88  static int SetField(lua_State* L);
89 
90  static int DoFile(lua_State* L);
91 
92  static int DoString(lua_State* L);
93 
94  // [-0,+1]
95  bool GetData (DataID id);
96 
97  // [-1,+0]
98  bool SetData (DataID id);
99 
100  // [-0,+0]
101  bool HasValue (DataID id);
102 
103  private:
104 
105  //DataID datatable_id_;
107 
108  DataGear& operator=(const DataGear& rhs) {
109  return *this;
110  }
111 
113  bool PushDataTable();
114 
116  void PushData (int table_index, DataID id) {
117  L_.rawgeti(table_index, id);
118  }
119 
121  void PopData (int table_index, DataID id) {
122  L_.rawseti(table_index, id);
123  }
124 
125 };
126 
127 template <class T>
129  State L_(L);
130 
131  L_.settop(2);
132  GETARG(L_, 1, DataGear, dtgear);
133  DataID id = L_.aux().checkintteger(2);
134  L_.settop(0);
135 
136  if (!dtgear.GetData(id)) {
137  L_.pushnil();
138  } // else the data will already be on top
139 
140  if (!L_.isprimitive<T>(-1))
141  return luaL_error(L, "Could not unwrap primitive from id #%d", id);
142 
143  return 1;
144 }
145 
146 template <class T>
148  State L_(L);
149 
150  L_.settop(3);
151  GETARG(L_, 1, DataGear, dtgear);
152  DataID id = L_.aux().checkintteger(2);
153  //T value = L_.aux().checkprimitive<T>(3);
154  //UData data = L_.touserdata(3);
155  //GETARGPTR(L_, 4, swig_type_info, type);
156  //L_.settop(0);
157 
158  if (!L_.isprimitive<T>(3) || !dtgear.SetData(id))
159  return luaL_error(L, "Could not wrap primitive with id #%d", id);
160 
161  //SWIG_NewPointerObj(L_, data, type, 0);
162  //if (!dtgear.SetData(id))
163  // return luaL_error(L, "Could not wrap primitive with id #%d", id);
164 
165  return 0;
166 }
167 
168 } /* namespace lua */
169 } /* namespace script */
170 } /* namespace ugdk */
171 
172 #endif /* UGDK_SCRIPT_LUA_DATAGEAR_H_ */
int checkintteger(int index)
Definition: auxlib.h:41
static int GenerateID(lua_State *L)
Generates a data ID. [-1,+1,-].
void pushnil()
Definition: state.h:38
State & L()
Definition: basegear.h:95
static int UnwrapList(lua_State *L)
void close()
Definition: state.h:30
bool isprimitive(int index) const
Definition: state.h:75
void settop(int index)
Definition: state.h:35
#define GETARG(L, i, T, name)
Definition: basegear.h:101
Definition: animation.h:11
~DataGear()
Definition: datagear.h:28
static int WrapData(lua_State *L)
Safely wraps typed data to a data ID. [-4,+0,-].
static int DoFile(lua_State *L)
void rawseti(int index, int n)
Definition: state.h:66
static int UnwrapPrimitive(lua_State *L)
Safely unwraps a primitive value from a data ID. [-2,+1,-].
Definition: datagear.h:128
static int UnwrapTable(lua_State *L)
static int UnwrapData(lua_State *L)
Safely unwraps typed data from a data ID. [-3,+1,-].
Definition: datagear.h:16
static int WrapPrimitive(lua_State *L)
Safely wraps primitive data to a data ID. [-3,+0,-].
Definition: datagear.h:147
static int DoString(lua_State *L)
Definition: idgenerator.h:10
static int SetField(lua_State *L)
Safely sets the field of an object mapped by a data ID> [-4,+1,-].
void setfield(int index, const char *k)
Definition: state.h:61
struct lua_State lua_State
Definition: defs.h:8
AuxLib & aux()
Definition: state.h:32
void rawgeti(int index, int n)
Definition: state.h:65
DataGear(lua_State *L)
Definition: datagear.h:24
Definition: basegear.h:18
static int GetField(lua_State *L)
Safely gets the field of an object mapped by a data ID. [-4,+0,?].
Definition: uncopyable.h:8
int DataID
Definition: defs.h:20
static int Execute(lua_State *L)
Safely executes the object mapped by a data ID. [-4,+0,?].
static int DestroyID(lua_State *L)
Safely destroys a data ID. [-2,+0,-].
State L_
Definition: basegear.h:88
Definition: state.h:18