UGDK  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
primitive.h
Go to the documentation of this file.
1 
2 #ifndef UGDK_SCRIPT_LUA_PRIMITIVE_H_
3 #define UGDK_SCRIPT_LUA_PRIMITIVE_H_
4 
6 
7 namespace ugdk {
8 namespace script {
9 namespace lua {
10 
11 #define CHECK_LUA_TYPE(name, type) (defined name) && (name == type)
12 
14 #define LUA_OPNAME(name) lua_##name
15 
17 
24 #define DEFINE_LUA_PRIMITIVE_OP(name) \
25  template <class T> \
26  class LUA_OPNAME(name) { \
27  private: \
28  LUA_OPNAME(name)() {} \
29  void primitive(); \
30  }
31 
32 #define DEFINE_LUA_PRIMITIVE_OPCASE(name, partial, type, ret, arg, exp) \
33  template <partial> \
34  class LUA_OPNAME(name)<type> { \
35  public: \
36  static ret primitive(lua_State* L, arg) { \
37  exp; \
38  } \
39  private: \
40  LUA_OPNAME(name)() {} \
41  }
42 
48 
49 #define DEFINE_LUA_PARTIAL_PUSH(partial, type, call) \
50  DEFINE_LUA_PRIMITIVE_OPCASE(push, partial, type, void, type val, call)
51 
52 #define DEFINE_LUA_FULL_PUSH(type, call) \
53  DEFINE_LUA_PARTIAL_PUSH(, type, call)
54 
55 #define DEFINE_LUA_SIMPLE_PUSH(type, name) \
56  DEFINE_LUA_FULL_PUSH(type, lua_push##name(L, val))
57 
58 
59 DEFINE_LUA_PARTIAL_PUSH(class T, T*, lua_pushlightuserdata(L, AsUData<T>(val)));
60 DEFINE_LUA_FULL_PUSH(lua_CFunction, lua_pushcclosure(L, val, 0));
61 DEFINE_LUA_SIMPLE_PUSH(const char*, string);
62 DEFINE_LUA_SIMPLE_PUSH(bool, boolean);
63 DEFINE_LUA_SIMPLE_PUSH(int, integer);
64 DEFINE_LUA_SIMPLE_PUSH(double, number);
65 DEFINE_LUA_SIMPLE_PUSH(UData, lightuserdata);
66 
67 #undef DEFINE_LUA_PARTIAL_PUSH
68 #undef DEFINE_LUA_FULL_PUSH
69 #undef DEFINE_LUA_SIMPLE_PUSH
70 
79 
80 #define DEFINE_LUA_TO(type, call) \
81  DEFINE_LUA_PRIMITIVE_OPCASE(to, , type, type, int index, return (call))
82 
83 #define DEFINE_LUA_SIMPLE_TO(type, name) \
84  DEFINE_LUA_TO(type, static_cast<type>(lua_to##name(L, index)))
85 
86 DEFINE_LUA_TO(const char*, lua_tolstring(L, index, nullptr));
87 DEFINE_LUA_TO(bool, !!(lua_toboolean(L, index)));
88 DEFINE_LUA_SIMPLE_TO(int, integer);
89 DEFINE_LUA_SIMPLE_TO(double, number);
90 DEFINE_LUA_SIMPLE_TO(UData, userdata);
91 
92 #undef DEFINE_LUA_TO
93 #undef DEFINE_LUA_SIMPLE_TO
94 
103 
104 #define DEFINE_LUA_IS(type, check) \
105  DEFINE_LUA_PRIMITIVE_OPCASE(is, , type, bool, int index, return !!(check))
106 
107 #define DEFINE_LUA_SIMPLE_IS(type, name) \
108  DEFINE_LUA_IS(type, lua_is##name(L, index))
109 
110 DEFINE_LUA_SIMPLE_IS(const char*, string);
111 DEFINE_LUA_IS(bool, !lua_isnone(L, index));
112 DEFINE_LUA_SIMPLE_IS(int, number);
113 DEFINE_LUA_SIMPLE_IS(double, number);
114 
115 #undef DEFINE_LUA_IS
116 #undef DEFINE_LUA_SIMPLE_IS
117 
121 } /* namespace lua */
122 } /* namespace script */
123 } /* namespace ugdk */
124 
125 #endif /* UGDK_SCRIPT_LUA_PRIMITIVE_H_ */
DEFINE_LUA_IS(bool,!lua_isnone(L, index))
Definition: animation.h:11
DEFINE_LUA_SIMPLE_PUSH(const char *, string)
DEFINE_LUA_FULL_PUSH(lua_CFunction, lua_pushcclosure(L, val, 0))
DEFINE_LUA_TO(const char *, lua_tolstring(L, index, nullptr))
DEFINE_LUA_PRIMITIVE_OP(push)
DEFINE_LUA_SIMPLE_TO(int, integer)
int(* lua_CFunction)(lua_State *)
Definition: defs.h:9
DEFINE_LUA_PARTIAL_PUSH(class T, T *, lua_pushlightuserdata(L, AsUData< T >(val)))
void * UData
Definition: defs.h:23
DEFINE_LUA_SIMPLE_IS(const char *, string)