@@ -45,100 +45,94 @@ namespace stack {
4545
4646 template <typename Handler>
4747 static optional<R> get (lua_State* L, int index, Handler&& handler, record& tracking) {
48- if (!unqualified_check<T>(L, index, std::forward<Handler>(handler))) {
49- tracking.use (static_cast <int >(!lua_isnone (L, index)));
50- return nullopt ;
51- }
52- return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
53- }
54- };
55-
56- template <typename T>
57- struct unqualified_check_getter <T, std::enable_if_t <is_lua_reference<T>::value>> {
58- template <typename Handler>
59- static optional<T> get (lua_State* L, int index, Handler&& handler, record& tracking) {
60- // actually check if it's none here, otherwise
61- // we'll have a none object inside an optional!
62- bool success = lua_isnoneornil (L, index) == 0 && stack::check<T>(L, index, no_panic);
63- if (!success) {
64- // expected type, actual type
65- tracking.use (static_cast <int >(success));
66- handler (L, index, type::poly, type_of (L, index), " " );
67- return nullopt ;
68- }
69- return stack_detail::unchecked_get<T>(L, index, tracking);
70- }
71- };
72-
73- template <typename T>
74- struct unqualified_check_getter <T, std::enable_if_t <std::is_integral<T>::value && lua_type_of<T>::value == type::number>> {
75- template <typename Handler>
76- static optional<T> get (lua_State* L, int index, Handler&& handler, record& tracking) {
77- #if SOL_LUA_VERSION >= 503
78- if (lua_isinteger (L, index) != 0 ) {
79- tracking.use (1 );
80- return static_cast <T>(lua_tointeger (L, index));
81- }
82- #endif
83- int isnum = 0 ;
84- const lua_Number value = lua_tonumberx (L, index, &isnum);
85- if (isnum != 0 ) {
86- #if (defined(SOL_SAFE_NUMERICS) && SOL_SAFE_NUMERICS) && !(defined(SOL_NO_CHECK_NUMBER_PRECISION) && SOL_NO_CHECK_NUMBER_PRECISION)
87- const auto integer_value = llround (value);
88- if (static_cast <lua_Number>(integer_value) == value) {
48+ if constexpr (!meta::meta_detail::is_adl_sol_lua_check_v<T> && !meta::meta_detail::is_adl_sol_lua_get_v<T>) {
49+ if constexpr (is_lua_reference_v<T>) {
50+ // actually check if it's none here, otherwise
51+ // we'll have a none object inside an optional!
52+ bool success = lua_isnoneornil (L, index) == 0 && stack::check<T>(L, index, no_panic);
53+ if (!success) {
54+ // expected type, actual type
55+ tracking.use (static_cast <int >(success));
56+ handler (L, index, type::poly, type_of (L, index), " " );
57+ return nullopt ;
58+ }
59+ return stack_detail::unchecked_get<T>(L, index, tracking);
60+ }
61+ else if constexpr (std::is_same_v<T, bool >) {
62+ return lua_toboolean (L, index) != 0 ;
63+ }
64+ else if constexpr (std::is_integral_v<T> && !std::is_same_v<T, bool >) {
65+ #if SOL_LUA_VERSION >= 503
66+ if (lua_isinteger (L, index) != 0 ) {
67+ tracking.use (1 );
68+ return static_cast <T>(lua_tointeger (L, index));
69+ }
70+ #endif
71+ int isnum = 0 ;
72+ const lua_Number value = lua_tonumberx (L, index, &isnum);
73+ if (isnum != 0 ) {
74+ #if (defined(SOL_SAFE_NUMERICS) && SOL_SAFE_NUMERICS) && !(defined(SOL_NO_CHECK_NUMBER_PRECISION) && SOL_NO_CHECK_NUMBER_PRECISION)
75+ const auto integer_value = llround (value);
76+ if (static_cast <lua_Number>(integer_value) == value) {
77+ tracking.use (1 );
78+ return static_cast <T>(integer_value);
79+ }
80+ #else
81+ tracking.use (1 );
82+ return static_cast <T>(value);
83+ #endif
84+ }
85+ const type t = type_of (L, index);
86+ tracking.use (static_cast <int >(t != type::none));
87+ handler (L, index, type::number, t, " not an integer" );
88+ return nullopt ;
89+ }
90+ else if constexpr (std::is_floating_point_v<T>) {
91+ int isnum = 0 ;
92+ lua_Number value = lua_tonumberx (L, index, &isnum);
93+ if (isnum == 0 ) {
94+ type t = type_of (L, index);
95+ tracking.use (static_cast <int >(t != type::none));
96+ handler (L, index, type::number, t, " not a valid floating point number" );
97+ return nullopt ;
98+ }
8999 tracking.use (1 );
90- return static_cast <T>(integer_value);
100+ return static_cast <T>(value);
101+ }
102+ else if constexpr (std::is_enum_v<T> && !meta::any_same_v<T, meta_function, type>) {
103+ int isnum = 0 ;
104+ lua_Integer value = lua_tointegerx (L, index, &isnum);
105+ if (isnum == 0 ) {
106+ type t = type_of (L, index);
107+ tracking.use (static_cast <int >(t != type::none));
108+ handler (L, index, type::number, t, " not a valid enumeration value" );
109+ return nullopt ;
110+ }
111+ tracking.use (1 );
112+ return static_cast <T>(value);
113+ }
114+ else {
115+ if (!unqualified_check<T>(L, index, std::forward<Handler>(handler))) {
116+ tracking.use (static_cast <int >(!lua_isnone (L, index)));
117+ return nullopt ;
118+ }
119+ return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
91120 }
92- #else
93- tracking.use (1 );
94- return static_cast <T>(value);
95- #endif
96- }
97- const type t = type_of (L, index);
98- tracking.use (static_cast <int >(t != type::none));
99- handler (L, index, type::number, t, " not an integer" );
100- return nullopt ;
101- }
102- };
103-
104- template <typename T>
105- struct unqualified_check_getter <T, std::enable_if_t <std::is_enum<T>::value && !meta::any_same<T, meta_function, type>::value>> {
106- template <typename Handler>
107- static optional<T> get (lua_State* L, int index, Handler&& handler, record& tracking) {
108- int isnum = 0 ;
109- lua_Integer value = lua_tointegerx (L, index, &isnum);
110- if (isnum == 0 ) {
111- type t = type_of (L, index);
112- tracking.use (static_cast <int >(t != type::none));
113- handler (L, index, type::number, t, " not a valid enumeration value" );
114- return nullopt ;
115121 }
116- tracking.use (1 );
117- return static_cast <T>(value);
118- }
119- };
120-
121- template <typename T>
122- struct unqualified_check_getter <T, std::enable_if_t <std::is_floating_point<T>::value>> {
123- template <typename Handler>
124- static optional<T> get (lua_State* L, int index, Handler&& handler, record& tracking) {
125- int isnum = 0 ;
126- lua_Number value = lua_tonumberx (L, index, &isnum);
127- if (isnum == 0 ) {
128- type t = type_of (L, index);
129- tracking.use (static_cast <int >(t != type::none));
130- handler (L, index, type::number, t, " not a valid floating point number" );
131- return nullopt ;
122+ else {
123+ if (!unqualified_check<T>(L, index, std::forward<Handler>(handler))) {
124+ tracking.use (static_cast <int >(!lua_isnone (L, index)));
125+ return nullopt ;
126+ }
127+ return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
132128 }
133- tracking.use (1 );
134- return static_cast <T>(value);
135129 }
136130 };
137131
138132 template <typename T>
139133 struct unqualified_getter <optional<T>> {
140134 static decltype (auto ) get(lua_State* L, int index, record& tracking) {
141- return check_get <T>(L, index, no_panic, tracking);
135+ return stack::unqualified_check_get <T>(L, index, no_panic, tracking);
142136 }
143137 };
144138
0 commit comments