diff --git a/include/kaguya/object.hpp b/include/kaguya/object.hpp index 6bb4f28..0f22e3d 100644 --- a/include/kaguya/object.hpp +++ b/include/kaguya/object.hpp @@ -672,6 +672,15 @@ namespace kaguya return object_wrapper(l, index, false) != 0; } + templateinline optional object_getopt(lua_State* l, int index) + { + const typename traits::remove_reference::type* pointer = get_const_pointer(l, index, types::typetag::type>()); + if (!pointer) + { + return optional(); + } + return *pointer; + } templateinline T object_get(lua_State* l, int index) { @@ -759,33 +768,19 @@ namespace kaguya template To get(lua_State* l, int index) { - if (lua_type_traits::strictCheckType(l, index)) - { - return lua_type_traits::get(l, index); - } - else if (strictCheckType(l, index)) + typedef optional::get_type> opt_type; + if (auto opt = lua_type_traits::get(l, index)) { - return get(l, index); - } - else if (lua_type_traits::checkType(l, index)) - { - return lua_type_traits::get(l, index); - } - else if (checkType(l, index)) - { - return get(l, index); - } - else - { - return get(l, index); + return *opt; } + return get(l, index); } } - template + template struct ConvertibleRegisterHelper { - typedef ConvertibleRegisterHelperProxy get_type; + typedef To get_type; static bool checkType(lua_State* l, int index) { @@ -793,7 +788,7 @@ namespace kaguya { return true; } - return conv_helper_detail::checkType(l, index); + return conv_helper_detail::checkType(l, index); } static bool strictCheckType(lua_State* l, int index) { @@ -801,16 +796,16 @@ namespace kaguya { return true; } - return conv_helper_detail::strictCheckType(l, index); + return conv_helper_detail::strictCheckType(l, index); } static get_type get(lua_State* l, int index) { - if (object_checkType(l, index)) + if (auto opt = object_getopt(l, index)) { - return object_get(l, index); + return *opt; } - return conv_helper_detail::get(l, index); + return conv_helper_detail::get(l, index); } }; #else @@ -826,8 +821,8 @@ namespace kaguya { #define KAGUYA_CONVERTIBLE_REG_HELPER_CHECK_TYPE_REP(N) || lua_type_traits::checkType(state, index) #define KAGUYA_CONVERTIBLE_REG_HELPER_STRICT_CHECK_TYPE_REP(N) || lua_type_traits::strictCheckType(state, index) -#define KAGUYA_CONVERTIBLE_REG_HELPER_ST_GET_REP(N) if (lua_type_traits::strictCheckType(state, index)){ return lua_type_traits::get(state, index);}else -#define KAGUYA_CONVERTIBLE_REG_HELPER_GET_REP(N) if (lua_type_traits::checkType(state, index)){ return lua_type_traits::get(state, index);}else +#define KAGUYA_CONVERTIBLE_REG_HELPER_GET_OPT_TYPEDEF(N) typedef optional::get_type> KAGUYA_PP_CAT(opt_type, N); +#define KAGUYA_CONVERTIBLE_REG_HELPER_GET_REP(N) if (KAGUYA_PP_CAT(opt_type, N) opt = lua_type_traits::get(state, index)){return *opt;}else template bool checkType(lua_State*, int, TypeTuple<>) @@ -860,7 +855,7 @@ namespace kaguya template \ To get(lua_State* state, int index, TypeTuple)\ {\ - KAGUYA_PP_REPEAT(N, KAGUYA_CONVERTIBLE_REG_HELPER_ST_GET_REP)\ + KAGUYA_PP_REPEAT(N, KAGUYA_CONVERTIBLE_REG_HELPER_GET_OPT_TYPEDEF)\ KAGUYA_PP_REPEAT(N, KAGUYA_CONVERTIBLE_REG_HELPER_GET_REP)\ {throw LuaTypeMismatch();}\ } @@ -878,7 +873,7 @@ namespace kaguya template struct ConvertibleRegisterHelper { - typedef ConvertibleRegisterHelperProxy get_type; + typedef To get_type; typedef TypeTuple conv_types; static bool checkType(lua_State* l, int index) @@ -899,9 +894,9 @@ namespace kaguya } static get_type get(lua_State* l, int index) { - if (object_checkType(l, index)) + if (optional opt = object_getopt(l, index)) { - return object_get(l, index); + return *opt; } return conv_helper_detail::get(l, index, conv_types()); } diff --git a/test/test_09_utility.cpp b/test/test_09_utility.cpp index 9e3951b..a5cb345 100644 --- a/test/test_09_utility.cpp +++ b/test/test_09_utility.cpp @@ -25,7 +25,7 @@ namespace kaguya { template<> struct lua_type_traits - : util::ConvertibleRegisterHelper&> + : util::ConvertibleRegisterHelper, int, std::string, kaguya_test_util::TestClass*,const std::vector&> { }; }