From 2b36c1e20e90f7a1e97740673adc697c40ce367e Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Wed, 13 Jul 2011 01:09:11 -0600 Subject: [PATCH] cleaned up script wrapper and bindings --- src/moof/mesh_bindings.cc | 6 +- src/moof/script.hh | 313 +++++++++++++------------------------- 2 files changed, 105 insertions(+), 214 deletions(-) diff --git a/src/moof/mesh_bindings.cc b/src/moof/mesh_bindings.cc index 8c9f9e0..290b42a 100644 --- a/src/moof/mesh_bindings.cc +++ b/src/moof/mesh_bindings.cc @@ -31,14 +31,14 @@ static int mesh_new(script& script) static int mesh_draw(script& script) { - mesh_handle* mesh; + mesh_handle mesh; script[1].require_object("mesh").get(mesh); scalar alpha; if (script[2].get(alpha)) - (*mesh)->draw(alpha); + mesh->draw(alpha); else - (*mesh)->draw(); + mesh->draw(); return 0; } diff --git a/src/moof/script.hh b/src/moof/script.hh index ae582fc..0431f1a 100644 --- a/src/moof/script.hh +++ b/src/moof/script.hh @@ -127,54 +127,24 @@ public: script_(const_cast(&s)) {} // check the type of the value - bool is_boolean() const - { - return (bool)lua_isboolean(script_->state_, index); - } - bool is_imported_function() const - { - return (bool)lua_iscfunction(script_->state_, index); - } - bool is_function() const - { - return (bool)lua_isfunction(script_->state_, index); - } - bool is_nil() const - { - return (bool)lua_isnil(script_->state_, index); - } - bool is_none() const - { - return (bool)lua_isnone(script_->state_, index); - } - bool is_none_or_nil() const - { - return (bool)lua_isnoneornil(script_->state_, index); - } - bool is_number() const - { - return (bool)lua_isnumber(script_->state_, index); - } - bool is_string() const - { - return (bool)lua_isstring(script_->state_, index); - } - bool is_table() const - { - return (bool)lua_istable(script_->state_, index); - } - bool is_thread() const - { - return (bool)lua_isthread(script_->state_, index); - } - bool is_data() const - { - return (bool)lua_isuserdata(script_->state_, index); - } - bool is_light_data() const - { - return (bool)lua_islightuserdata(script_->state_, index); - } +#define IS_TYPE(T, N) \ + bool is_##N() const \ + { \ + return (bool)lua_is##T(script_->state_, index); \ + }// + IS_TYPE(boolean, boolean); + IS_TYPE(cfunction, imported_function); + IS_TYPE(function, function); + IS_TYPE(lightuserdata, light_data); + IS_TYPE(nil, nil); + IS_TYPE(none, none); + IS_TYPE(noneornil, none_or_nil); + IS_TYPE(number, number); + IS_TYPE(string, string); + IS_TYPE(table, table); + IS_TYPE(thread, thread); + IS_TYPE(userdata, data); +#undef IS_TYPE /** * Check the value and throw an error if its the wrong type. @@ -185,54 +155,21 @@ public: return *this; } - const slot& - require_boolean(const std::string& what = "boolean") const - { - if (!is_boolean()) raise_type_error(what); - return *this; - } - const slot& - require_number(const std::string& what = "number") const - { - if (!is_number()) raise_type_error(what); - return *this; - } - const slot& - require_string(const std::string& what = "string") const - { - if (!is_string()) raise_type_error(what); - return *this; - } - const slot& - require_table(const std::string& what = "table") const - { - if (!is_table()) raise_type_error(what); - return *this; - } - const slot& - require_function(const std::string& what = "function") const - { - if (!is_function()) raise_type_error(what); - return *this; - } - const slot& - require_data(const std::string& what = "userdata") const - { - if (!is_data()) raise_type_error(what); - return *this; - } - const slot& - require_nil(const std::string& what = "nil") const - { - if (!is_nil()) raise_type_error(what); - return *this; - } - const slot& - require_thread(const std::string& what = "thread") const - { - if (!is_thread()) raise_type_error(what); - return *this; - } +#define REQUIRE_TYPE(T) \ + const slot& require_##T(const std::string& what = #T) const \ + { \ + if (!is_##T()) raise_type_error(what); \ + return *this; \ + }// + REQUIRE_TYPE(boolean); + REQUIRE_TYPE(number); + REQUIRE_TYPE(string); + REQUIRE_TYPE(table); + REQUIRE_TYPE(function); + REQUIRE_TYPE(data); + REQUIRE_TYPE(nil); + REQUIRE_TYPE(thread); +#undef REQUIRE_TYPE template const slot& @@ -355,16 +292,28 @@ public: /** * Convert the underlying value to a C++ type. */ - template - bool get(T& value) const - { - if (is_number()) - { - value = (T)lua_tointeger(script_->state_, index); - return true; - } - return false; - } + +#define GET_INT_OF_TYPE(T) \ + bool get(T& value) const \ + { \ + if (is_number()) \ + { \ + value = static_cast(lua_tointeger(script_->state_, index)); \ + return true; \ + } \ + return false; \ + }// + GET_INT_OF_TYPE(char); + GET_INT_OF_TYPE(unsigned char); + GET_INT_OF_TYPE(short); + GET_INT_OF_TYPE(unsigned short); + GET_INT_OF_TYPE(int); + GET_INT_OF_TYPE(unsigned int); + GET_INT_OF_TYPE(long); + GET_INT_OF_TYPE(unsigned long); + GET_INT_OF_TYPE(long long); + GET_INT_OF_TYPE(unsigned long long); +#undef GET_INT_OF_TYPE bool get(float& value) const { @@ -404,7 +353,6 @@ public: } return false; } - bool get(std::string& value) const { const char* str; @@ -427,6 +375,16 @@ public: } return false; } + template + bool get(T& value) const + { + if (is_data()) + { + value = *reinterpret_cast(lua_touserdata(script_->state_, index)); + return true; + } + return false; + } void* get_data() const { return lua_touserdata(script_->state_, index); @@ -574,11 +532,8 @@ public: */ void pop() { - if - (index < 0) script_->pop(-index); - else - script_->pop(script_->stack_size() - - index + 1); + if (index < 0) script_->pop(-index); + else script_->pop(script_->stack_size() - index + 1); } /** @@ -676,7 +631,7 @@ public: std::ostringstream stream; stream << "bad argument " << index << " to '" << func - << "' (" << message << ")"; + << "' (" << message << ")"; throw std::invalid_argument(stream.str()); return 0; @@ -687,6 +642,7 @@ public: mutable class script* script_; }; + script() : state_(0) { @@ -740,54 +696,22 @@ public: pop(); } - void import_base_library() - { - push(luaopen_base); - push(LUA_COLIBNAME); - call(1, 0); - } - void import_package_library() - { - push(luaopen_package); - push(LUA_LOADLIBNAME); - call(1, 0); - } - void import_string_library() - { - push(luaopen_string); - push(LUA_STRLIBNAME); - call(1, 0); - } - void import_table_library() - { - push(luaopen_table); - push(LUA_TABLIBNAME); - call(1, 0); - } - void import_math_library() - { - push(luaopen_math); - push(LUA_MATHLIBNAME); - call(1, 0); - } - void import_io_library() - { - push(luaopen_io); - push(LUA_IOLIBNAME); - call(1, 0); - } - void import_os_library() - { - push(luaopen_os); - push(LUA_OSLIBNAME); - call(1, 0); - } - void import_debug_library() - { - push(luaopen_debug); - push(LUA_DBLIBNAME); - call(1, 0); - } +#define IMPORT_LIBRARY(L, K) \ + void import_##L##_library() \ + { \ + push(luaopen_##L); \ + push(LUA_##K##LIBNAME); \ + call(1, 0); \ + }// + IMPORT_LIBRARY(base, CO); + IMPORT_LIBRARY(debug, DB); + IMPORT_LIBRARY(io, IO); + IMPORT_LIBRARY(math, MATH); + IMPORT_LIBRARY(os, OS); + IMPORT_LIBRARY(package, LOAD); + IMPORT_LIBRARY(string, STR); + IMPORT_LIBRARY(table, TAB); +#undef IMPORT_LIBRARY void import_function(const std::string& name, const function& function) @@ -937,56 +861,23 @@ public: * Push some values onto the stack. */ - slot push(char value) - { - lua_pushinteger(state_, lua_Integer(value)); - return top(); - } - slot push(unsigned char value) - { - lua_pushinteger(state_, lua_Integer(value)); - return top(); - } - slot push(short value) - { - lua_pushinteger(state_, lua_Integer(value)); - return top(); - } - slot push(unsigned short value) - { - lua_pushinteger(state_, lua_Integer(value)); - return top(); - } - slot push(int value) - { - lua_pushinteger(state_, lua_Integer(value)); - return top(); - } - slot push(unsigned int value) - { - lua_pushinteger(state_, lua_Integer(value)); - return top(); - } - slot push(long value) - { - lua_pushinteger(state_, lua_Integer(value)); - return top(); - } - slot push(unsigned long value) - { - lua_pushinteger(state_, lua_Integer(value)); - return top(); - } - slot push(long long value) - { - lua_pushinteger(state_, lua_Integer(value)); - return top(); - } - slot push(unsigned long long value) - { - lua_pushinteger(state_, lua_Integer(value)); - return top(); - } +#define PUSH_INT_OF_TYPE(T) \ + slot push(T value) \ + { \ + lua_pushinteger(state_, static_cast(value)); \ + return top(); \ + }// + PUSH_INT_OF_TYPE(char); + PUSH_INT_OF_TYPE(unsigned char); + PUSH_INT_OF_TYPE(short); + PUSH_INT_OF_TYPE(unsigned short); + PUSH_INT_OF_TYPE(int); + PUSH_INT_OF_TYPE(unsigned int); + PUSH_INT_OF_TYPE(long); + PUSH_INT_OF_TYPE(unsigned long); + PUSH_INT_OF_TYPE(long long); + PUSH_INT_OF_TYPE(unsigned long long); +#undef PUSH_INT_OF_TYPE slot push(bool value) { -- 2.43.0