UGDK
|
00001 00002 #ifndef UGDK_SCRIPT_LUA_PRIMITIVE_H_ 00003 #define UGDK_SCRIPT_LUA_PRIMITIVE_H_ 00004 00005 #include <ugdk/script/languages/lua/header.h> 00006 00007 namespace ugdk { 00008 namespace script { 00009 namespace lua { 00010 00011 #define CHECK_LUA_TYPE(name, type) (defined name) && (name == type) 00012 00014 #define LUA_OPNAME(name) lua_##name 00015 00017 00024 #define DEFINE_LUA_PRIMITIVE_OP(name) \ 00025 template <class T> \ 00026 class LUA_OPNAME(name) { \ 00027 private: \ 00028 LUA_OPNAME(name)() {} \ 00029 void primitive(); \ 00030 } 00031 00032 #define DEFINE_LUA_PRIMITIVE_OPCASE(name, partial, type, ret, arg, exp) \ 00033 template <partial> \ 00034 class LUA_OPNAME(name)<type> { \ 00035 public: \ 00036 static ret primitive(lua_State* L, arg) { \ 00037 exp; \ 00038 } \ 00039 private: \ 00040 LUA_OPNAME(name)() {} \ 00041 } 00042 00047 DEFINE_LUA_PRIMITIVE_OP(push); 00048 00049 #define DEFINE_LUA_PARTIAL_PUSH(partial, type, call) \ 00050 DEFINE_LUA_PRIMITIVE_OPCASE(push, partial, type, void, type val, call) 00051 00052 #define DEFINE_LUA_FULL_PUSH(type, call) \ 00053 DEFINE_LUA_PARTIAL_PUSH(, type, call) 00054 00055 #define DEFINE_LUA_SIMPLE_PUSH(type, name) \ 00056 DEFINE_LUA_FULL_PUSH(type, lua_push##name(L, val)) 00057 00058 00059 DEFINE_LUA_PARTIAL_PUSH(class T, T*, lua_pushlightuserdata(L, AsUData<T>(val))); 00060 DEFINE_LUA_FULL_PUSH(lua_CFunction, lua_pushcclosure(L, val, 0)); 00061 DEFINE_LUA_SIMPLE_PUSH(const char*, string); 00062 DEFINE_LUA_SIMPLE_PUSH(bool, boolean); 00063 DEFINE_LUA_SIMPLE_PUSH(int, integer); 00064 DEFINE_LUA_SIMPLE_PUSH(double, number); 00065 DEFINE_LUA_SIMPLE_PUSH(UData, lightuserdata); 00066 00067 #undef DEFINE_LUA_PARTIAL_PUSH 00068 #undef DEFINE_LUA_FULL_PUSH 00069 #undef DEFINE_LUA_SIMPLE_PUSH 00070 00078 DEFINE_LUA_PRIMITIVE_OP(to); 00079 00080 #define DEFINE_LUA_TO(type, call) \ 00081 DEFINE_LUA_PRIMITIVE_OPCASE(to, , type, type, int index, return (call)) 00082 00083 #define DEFINE_LUA_SIMPLE_TO(type, name) \ 00084 DEFINE_LUA_TO(type, static_cast<type>(lua_to##name(L, index))) 00085 00086 DEFINE_LUA_TO(const char*, lua_tolstring(L, index, NULL)); 00087 DEFINE_LUA_TO(bool, !!(lua_toboolean(L, index))); 00088 DEFINE_LUA_SIMPLE_TO(int, integer); 00089 DEFINE_LUA_SIMPLE_TO(double, number); 00090 DEFINE_LUA_SIMPLE_TO(UData, userdata); 00091 00092 #undef DEFINE_LUA_TO 00093 #undef DEFINE_LUA_SIMPLE_TO 00094 00102 DEFINE_LUA_PRIMITIVE_OP(is); 00103 00104 #define DEFINE_LUA_IS(type, check) \ 00105 DEFINE_LUA_PRIMITIVE_OPCASE(is, , type, bool, int index, return !!(check)) 00106 00107 #define DEFINE_LUA_SIMPLE_IS(type, name) \ 00108 DEFINE_LUA_IS(type, lua_is##name(L, index)) 00109 00110 DEFINE_LUA_SIMPLE_IS(const char*, string); 00111 DEFINE_LUA_IS(bool, !lua_isnone(L, index)); 00112 DEFINE_LUA_SIMPLE_IS(int, number); 00113 DEFINE_LUA_SIMPLE_IS(double, number); 00114 00115 #undef DEFINE_LUA_IS 00116 #undef DEFINE_LUA_SIMPLE_IS 00117 00121 } /* namespace lua */ 00122 } /* namespace script */ 00123 } /* namespace ugdk */ 00124 00125 #endif /* UGDK_SCRIPT_LUA_PRIMITIVE_H_ */