Skip to content

Commit

Permalink
optimize for ConvertibleRegisterHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
satoren committed Nov 15, 2016
1 parent c93598a commit a68e5c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
57 changes: 26 additions & 31 deletions include/kaguya/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,15 @@ namespace kaguya
return object_wrapper<T>(l, index, false) != 0;
}

template<typename T>inline optional<T> object_getopt(lua_State* l, int index)
{
const typename traits::remove_reference<T>::type* pointer = get_const_pointer(l, index, types::typetag<typename traits::remove_reference<T>::type>());
if (!pointer)
{
return optional<T>();
}
return *pointer;
}

template<typename T>inline T object_get(lua_State* l, int index)
{
Expand Down Expand Up @@ -759,58 +768,44 @@ namespace kaguya
template<class To, class From, class... Remain>
To get(lua_State* l, int index)
{
if (lua_type_traits<From>::strictCheckType(l, index))
{
return lua_type_traits<From>::get(l, index);
}
else if (strictCheckType<To, Remain...>(l, index))
typedef optional<lua_type_traits<From>::get_type> opt_type;
if (auto opt = lua_type_traits<opt_type>::get(l, index))
{
return get<To, Remain...>(l, index);
}
else if (lua_type_traits<From>::checkType(l, index))
{
return lua_type_traits<From>::get(l, index);
}
else if (checkType<To, Remain...>(l, index))
{
return get<To, Remain...>(l, index);
}
else
{
return get<To>(l, index);
return *opt;
}
return get<To, Remain...>(l, index);
}
}

template<class To, class From, class... Remain>
template<class To, class... From>
struct ConvertibleRegisterHelper
{
typedef ConvertibleRegisterHelperProxy<To> get_type;
typedef To get_type;

static bool checkType(lua_State* l, int index)
{
if (object_checkType<To>(l, index))
{
return true;
}
return conv_helper_detail::checkType<To, From, Remain...>(l, index);
return conv_helper_detail::checkType<To, From...>(l, index);
}
static bool strictCheckType(lua_State* l, int index)
{
if (object_strictCheckType<To>(l, index))
{
return true;
}
return conv_helper_detail::strictCheckType<To, From, Remain...>(l, index);
return conv_helper_detail::strictCheckType<To, From...>(l, index);
}

static get_type get(lua_State* l, int index)
{
if (object_checkType<To>(l, index))
if (auto opt = object_getopt<To>(l, index))
{
return object_get<To>(l, index);
return *opt;
}
return conv_helper_detail::get<get_type, From, Remain...>(l, index);
return conv_helper_detail::get<get_type, From...>(l, index);
}
};
#else
Expand All @@ -826,8 +821,8 @@ namespace kaguya
{
#define KAGUYA_CONVERTIBLE_REG_HELPER_CHECK_TYPE_REP(N) || lua_type_traits<KAGUYA_PP_CAT(A,N)>::checkType(state, index)
#define KAGUYA_CONVERTIBLE_REG_HELPER_STRICT_CHECK_TYPE_REP(N) || lua_type_traits<KAGUYA_PP_CAT(A,N)>::strictCheckType(state, index)
#define KAGUYA_CONVERTIBLE_REG_HELPER_ST_GET_REP(N) if (lua_type_traits<KAGUYA_PP_CAT(A,N)>::strictCheckType(state, index)){ return lua_type_traits<KAGUYA_PP_CAT(A,N)>::get(state, index);}else
#define KAGUYA_CONVERTIBLE_REG_HELPER_GET_REP(N) if (lua_type_traits<KAGUYA_PP_CAT(A,N)>::checkType(state, index)){ return lua_type_traits<KAGUYA_PP_CAT(A,N)>::get(state, index);}else
#define KAGUYA_CONVERTIBLE_REG_HELPER_GET_OPT_TYPEDEF(N) typedef optional<typename lua_type_traits<KAGUYA_PP_CAT(A, N)>::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<KAGUYA_PP_CAT(opt_type, N)>::get(state, index)){return *opt;}else

template<typename To>
bool checkType(lua_State*, int, TypeTuple<>)
Expand Down Expand Up @@ -860,7 +855,7 @@ namespace kaguya
template<typename To,KAGUYA_PP_TEMPLATE_DEF_REPEAT(N)> \
To get(lua_State* state, int index, TypeTuple<KAGUYA_PP_TEMPLATE_ARG_REPEAT(N)>)\
{\
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();}\
}
Expand All @@ -878,7 +873,7 @@ namespace kaguya

template<typename To, KAGUYA_PP_CONVERTIBLE_REG_HELPER_DEF_REPEAT(KAGUYA_FUNCTION_MAX_ARGS)>
struct ConvertibleRegisterHelper {
typedef ConvertibleRegisterHelperProxy<To> get_type;
typedef To get_type;
typedef TypeTuple<KAGUYA_PP_TEMPLATE_ARG_REPEAT(KAGUYA_FUNCTION_MAX_ARGS)> conv_types;

static bool checkType(lua_State* l, int index)
Expand All @@ -899,9 +894,9 @@ namespace kaguya
}
static get_type get(lua_State* l, int index)
{
if (object_checkType<To>(l, index))
if (optional<To> opt = object_getopt<To>(l, index))
{
return object_get<To>(l, index);
return *opt;
}
return conv_helper_detail::get<get_type>(l, index, conv_types());
}
Expand Down
2 changes: 1 addition & 1 deletion test/test_09_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace kaguya
{
template<>
struct lua_type_traits<ProxyClassTest>
: util::ConvertibleRegisterHelper<ProxyClassTest, std::string, int, kaguya_test_util::TestClass*,const std::vector<int>&>
: util::ConvertibleRegisterHelper<util::ConvertibleRegisterHelperProxy<ProxyClassTest>, int, std::string, kaguya_test_util::TestClass*,const std::vector<int>&>
{
};
}
Expand Down

0 comments on commit a68e5c3

Please sign in to comment.