@@ -672,6 +672,15 @@ namespace kaguya
672672 return object_wrapper<T>(l, index, false ) != 0 ;
673673 }
674674
675+ template <typename T>inline optional<T> object_getopt (lua_State* l, int index)
676+ {
677+ const typename traits::remove_reference<T>::type* pointer = get_const_pointer (l, index, types::typetag<typename traits::remove_reference<T>::type>());
678+ if (!pointer)
679+ {
680+ return optional<T>();
681+ }
682+ return *pointer;
683+ }
675684
676685 template <typename T>inline T object_get (lua_State* l, int index)
677686 {
@@ -759,58 +768,44 @@ namespace kaguya
759768 template <class To , class From , class ... Remain>
760769 To get (lua_State* l, int index)
761770 {
762- if (lua_type_traits<From>::strictCheckType (l, index))
763- {
764- return lua_type_traits<From>::get (l, index);
765- }
766- else if (strictCheckType<To, Remain...>(l, index))
771+ typedef optional<typename lua_type_traits<From>::get_type> opt_type;
772+ if (auto opt = lua_type_traits<opt_type>::get (l, index))
767773 {
768- return get<To, Remain...>(l, index);
769- }
770- else if (lua_type_traits<From>::checkType (l, index))
771- {
772- return lua_type_traits<From>::get (l, index);
773- }
774- else if (checkType<To, Remain...>(l, index))
775- {
776- return get<To, Remain...>(l, index);
777- }
778- else
779- {
780- return get<To>(l, index);
774+ return *opt;
781775 }
776+ return get<To, Remain...>(l, index);
782777 }
783778 }
784779
785- template <class To , class From , class ... Remain >
780+ template <class To , class ... From >
786781 struct ConvertibleRegisterHelper
787782 {
788- typedef ConvertibleRegisterHelperProxy<To> get_type;
783+ typedef To get_type;
789784
790785 static bool checkType (lua_State* l, int index)
791786 {
792787 if (object_checkType<To>(l, index))
793788 {
794789 return true ;
795790 }
796- return conv_helper_detail::checkType<To, From, Remain ...>(l, index);
791+ return conv_helper_detail::checkType<To, From...>(l, index);
797792 }
798793 static bool strictCheckType (lua_State* l, int index)
799794 {
800795 if (object_strictCheckType<To>(l, index))
801796 {
802797 return true ;
803798 }
804- return conv_helper_detail::strictCheckType<To, From, Remain ...>(l, index);
799+ return conv_helper_detail::strictCheckType<To, From...>(l, index);
805800 }
806801
807802 static get_type get (lua_State* l, int index)
808803 {
809- if (object_checkType <To>(l, index))
804+ if (auto opt = object_getopt <To>(l, index))
810805 {
811- return object_get<To>(l, index) ;
806+ return *opt ;
812807 }
813- return conv_helper_detail::get<get_type, From, Remain ...>(l, index);
808+ return conv_helper_detail::get<get_type, From...>(l, index);
814809 }
815810 };
816811#else
@@ -826,8 +821,8 @@ namespace kaguya
826821 {
827822#define KAGUYA_CONVERTIBLE_REG_HELPER_CHECK_TYPE_REP (N ) || lua_type_traits<KAGUYA_PP_CAT(A,N)>::checkType(state, index)
828823#define KAGUYA_CONVERTIBLE_REG_HELPER_STRICT_CHECK_TYPE_REP (N ) || lua_type_traits<KAGUYA_PP_CAT(A,N)>::strictCheckType(state, index)
829- #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
830- #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
824+ #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);
825+ #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
831826
832827 template <typename To>
833828 bool checkType (lua_State*, int , TypeTuple<>)
@@ -860,7 +855,7 @@ namespace kaguya
860855 template <typename To,KAGUYA_PP_TEMPLATE_DEF_REPEAT(N)> \
861856 To get (lua_State* state, int index, TypeTuple<KAGUYA_PP_TEMPLATE_ARG_REPEAT(N)>)\
862857 {\
863- KAGUYA_PP_REPEAT (N, KAGUYA_CONVERTIBLE_REG_HELPER_ST_GET_REP )\
858+ KAGUYA_PP_REPEAT (N, KAGUYA_CONVERTIBLE_REG_HELPER_GET_OPT_TYPEDEF )\
864859 KAGUYA_PP_REPEAT (N, KAGUYA_CONVERTIBLE_REG_HELPER_GET_REP)\
865860 {throw LuaTypeMismatch ();}\
866861 }
@@ -878,7 +873,7 @@ namespace kaguya
878873
879874 template <typename To, KAGUYA_PP_CONVERTIBLE_REG_HELPER_DEF_REPEAT(KAGUYA_FUNCTION_MAX_ARGS)>
880875 struct ConvertibleRegisterHelper {
881- typedef ConvertibleRegisterHelperProxy<To> get_type;
876+ typedef To get_type;
882877 typedef TypeTuple<KAGUYA_PP_TEMPLATE_ARG_REPEAT(KAGUYA_FUNCTION_MAX_ARGS)> conv_types;
883878
884879 static bool checkType (lua_State* l, int index)
@@ -899,9 +894,9 @@ namespace kaguya
899894 }
900895 static get_type get (lua_State* l, int index)
901896 {
902- if (object_checkType <To>(l, index))
897+ if (optional<To> opt = object_getopt <To>(l, index))
903898 {
904- return object_get<To>(l, index) ;
899+ return *opt ;
905900 }
906901 return conv_helper_detail::get<get_type>(l, index, conv_types ());
907902 }
0 commit comments