From fbf5b48dc84e5b834dffb4de09d7c911aec83153 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Mon, 28 Jan 2019 12:33:21 -0500 Subject: [PATCH] void support --- include/sol/stack_core.hpp | 51 +++++----- include/sol/traits.hpp | 40 +++----- include/sol/types.hpp | 3 - single/include/sol/forward.hpp | 4 +- single/include/sol/sol.hpp | 98 +++++++++---------- .../runtime_tests/source/usertype_unique.cpp | 13 +++ 6 files changed, 105 insertions(+), 104 deletions(-) diff --git a/include/sol/stack_core.hpp b/include/sol/stack_core.hpp index 4abe06b0..209036b3 100644 --- a/include/sol/stack_core.hpp +++ b/include/sol/stack_core.hpp @@ -330,13 +330,15 @@ namespace sol { void* pointer_adjusted; void* data_adjusted; - bool result = attempt_alloc(L, std::alignment_of_v, sizeof(T*), std::alignment_of_v, sizeof(T), initial_size, pointer_adjusted, data_adjusted); + bool result + = attempt_alloc(L, std::alignment_of_v, sizeof(T*), std::alignment_of_v, sizeof(T), initial_size, pointer_adjusted, data_adjusted); if (!result) { // we're likely to get something that fails to perform the proper allocation a second time, // so we use the suggested_new_size bump to help us out here pointer_adjusted = nullptr; data_adjusted = nullptr; - result = attempt_alloc(L, std::alignment_of_v, sizeof(T*), std::alignment_of_v, sizeof(T), misaligned_size, pointer_adjusted, data_adjusted); + result = attempt_alloc( + L, std::alignment_of_v, sizeof(T*), std::alignment_of_v, sizeof(T), misaligned_size, pointer_adjusted, data_adjusted); if (!result) { if (pointer_adjusted == nullptr) { luaL_error(L, "aligned allocation of userdata block (pointer section) for '%s' failed", detail::demangle().c_str()); @@ -1343,30 +1345,35 @@ namespace sol { template int comparsion_operator_wrap(lua_State* L) { - auto maybel = stack::unqualified_check_get(L, 1); - if (!maybel) { + if constexpr (std::is_void_v) { return stack::push(L, false); } - auto mayber = stack::unqualified_check_get(L, 2); - if (!mayber) { - return stack::push(L, false); - } - decltype(auto) l = *maybel; - decltype(auto) r = *mayber; - if constexpr (std::is_same_v) { - std::equal_to<> op; - return stack::push(L, op(detail::ptr(l), detail::ptr(r))); - } else { - if constexpr (std::is_same_v, Op> // cf-hack - || std::is_same_v, Op> // - || std::is_same_v, Op>) { // - if (detail::ptr(l) == detail::ptr(r)) { - return stack::push(L, true); - } + auto maybel = stack::unqualified_check_get(L, 1); + if (!maybel) { + return stack::push(L, false); + } + auto mayber = stack::unqualified_check_get(L, 2); + if (!mayber) { + return stack::push(L, false); + } + decltype(auto) l = *maybel; + decltype(auto) r = *mayber; + if constexpr (std::is_same_v) { + std::equal_to<> op; + return stack::push(L, op(detail::ptr(l), detail::ptr(r))); + } + else { + if constexpr (std::is_same_v, Op> // cf-hack + || std::is_same_v, Op> // + || std::is_same_v, Op>) { // + if (detail::ptr(l) == detail::ptr(r)) { + return stack::push(L, true); + } + } + Op op; + return stack::push(L, op(detail::deref(l), detail::deref(r))); } - Op op; - return stack::push(L, op(detail::deref(l), detail::deref(r))); } } diff --git a/include/sol/traits.hpp b/include/sol/traits.hpp index 4e4982d5..2d90fc6c 100644 --- a/include/sol/traits.hpp +++ b/include/sol/traits.hpp @@ -38,6 +38,11 @@ namespace sol { + namespace detail { + struct unchecked_t {}; + const unchecked_t unchecked = unchecked_t{}; + } // namespace detail + namespace meta { using sfinae_yes_t = std::true_type; using sfinae_no_t = std::false_type; @@ -513,39 +518,24 @@ namespace sol { struct is_matched_lookup_impl : std::false_type {}; template struct is_matched_lookup_impl : std::is_same {}; + + template + using non_void_t = std::conditional_t, ::sol::detail::unchecked_t, T>; } // namespace meta_detail template - struct supports_op_less : decltype(meta_detail::supports_op_less_test(std::declval(), std::declval())) {}; - template - struct supports_op_less : std::false_type {}; - template - struct supports_op_less : std::false_type {}; + using supports_op_less = decltype(meta_detail::supports_op_less_test(std::declval&>(), std::declval&>())); template - struct supports_op_equal : decltype(meta_detail::supports_op_equal_test(std::declval(), std::declval())) {}; - template - struct supports_op_equal : std::false_type {}; - template - struct supports_op_equal : std::false_type {}; + using supports_op_equal = decltype(meta_detail::supports_op_equal_test(std::declval&>(), std::declval&>())); template - struct supports_op_less_equal : decltype(meta_detail::supports_op_less_equal_test(std::declval(), std::declval())) {}; - template - struct supports_op_less_equal : std::false_type {}; - template - struct supports_op_less_equal : std::false_type {}; + using supports_op_less_equal = decltype(meta_detail::supports_op_less_equal_test(std::declval&>(), std::declval&>())); template - struct supports_ostream_op : decltype(meta_detail::supports_ostream_op(std::declval(), std::declval())) {}; + using supports_ostream_op = decltype(meta_detail::supports_ostream_op(std::declval&>(), std::declval&>())); template - struct supports_ostream_op : std::false_type {}; + using supports_adl_to_string = decltype(meta_detail::supports_adl_to_string(std::declval&>())); + template - struct supports_ostream_op : std::false_type {}; - template - struct supports_adl_to_string : decltype(meta_detail::supports_adl_to_string(std::declval())) {}; - template <> - struct supports_adl_to_string : std::false_type {}; - - template - using supports_to_string_member = meta::boolean::value>; + using supports_to_string_member = meta::boolean>::value>; template struct is_callable : boolean::value> {}; diff --git a/include/sol/types.hpp b/include/sol/types.hpp index 397ac86e..08370699 100644 --- a/include/sol/types.hpp +++ b/include/sol/types.hpp @@ -93,9 +93,6 @@ namespace sol { } }; - struct unchecked_t {}; - const unchecked_t unchecked = unchecked_t{}; - struct yield_tag_t {}; const yield_tag_t yield_tag = yield_tag_t{}; } // namespace detail diff --git a/single/include/sol/forward.hpp b/single/include/sol/forward.hpp index 25ae8e9d..e64384ae 100644 --- a/single/include/sol/forward.hpp +++ b/single/include/sol/forward.hpp @@ -20,8 +20,8 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // This file was generated with a script. -// Generated 2019-01-28 16:32:19.530572 UTC -// This header was generated with sol v2.20.6 (revision ad494bd) +// Generated 2019-01-28 17:33:06.880854 UTC +// This header was generated with sol v2.20.6 (revision e1f3e5f) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP diff --git a/single/include/sol/sol.hpp b/single/include/sol/sol.hpp index 15e77f2e..113ca574 100644 --- a/single/include/sol/sol.hpp +++ b/single/include/sol/sol.hpp @@ -20,8 +20,8 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // This file was generated with a script. -// Generated 2019-01-28 16:32:19.243332 UTC -// This header was generated with sol v2.20.6 (revision ad494bd) +// Generated 2019-01-28 17:33:06.587638 UTC +// This header was generated with sol v2.20.6 (revision e1f3e5f) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_HPP @@ -1230,6 +1230,11 @@ namespace sol { #include namespace sol { + namespace detail { + struct unchecked_t {}; + const unchecked_t unchecked = unchecked_t{}; + } // namespace detail + namespace meta { using sfinae_yes_t = std::true_type; using sfinae_no_t = std::false_type; @@ -1705,39 +1710,24 @@ namespace sol { struct is_matched_lookup_impl : std::false_type {}; template struct is_matched_lookup_impl : std::is_same {}; + + template + using non_void_t = std::conditional_t, ::sol::detail::unchecked_t, T>; } // namespace meta_detail template - struct supports_op_less : decltype(meta_detail::supports_op_less_test(std::declval(), std::declval())) {}; - template - struct supports_op_less : std::false_type {}; - template - struct supports_op_less : std::false_type {}; + using supports_op_less = decltype(meta_detail::supports_op_less_test(std::declval&>(), std::declval&>())); template - struct supports_op_equal : decltype(meta_detail::supports_op_equal_test(std::declval(), std::declval())) {}; - template - struct supports_op_equal : std::false_type {}; - template - struct supports_op_equal : std::false_type {}; + using supports_op_equal = decltype(meta_detail::supports_op_equal_test(std::declval&>(), std::declval&>())); template - struct supports_op_less_equal : decltype(meta_detail::supports_op_less_equal_test(std::declval(), std::declval())) {}; - template - struct supports_op_less_equal : std::false_type {}; - template - struct supports_op_less_equal : std::false_type {}; + using supports_op_less_equal = decltype(meta_detail::supports_op_less_equal_test(std::declval&>(), std::declval&>())); template - struct supports_ostream_op : decltype(meta_detail::supports_ostream_op(std::declval(), std::declval())) {}; + using supports_ostream_op = decltype(meta_detail::supports_ostream_op(std::declval&>(), std::declval&>())); template - struct supports_ostream_op : std::false_type {}; + using supports_adl_to_string = decltype(meta_detail::supports_adl_to_string(std::declval&>())); + template - struct supports_ostream_op : std::false_type {}; - template - struct supports_adl_to_string : decltype(meta_detail::supports_adl_to_string(std::declval())) {}; - template <> - struct supports_adl_to_string : std::false_type {}; - - template - using supports_to_string_member = meta::boolean::value>; + using supports_to_string_member = meta::boolean>::value>; template struct is_callable : boolean::value> {}; @@ -5888,9 +5878,6 @@ namespace sol { } }; - struct unchecked_t {}; - const unchecked_t unchecked = unchecked_t{}; - struct yield_tag_t {}; const yield_tag_t yield_tag = yield_tag_t{}; } // namespace detail @@ -8923,13 +8910,15 @@ namespace sol { void* pointer_adjusted; void* data_adjusted; - bool result = attempt_alloc(L, std::alignment_of_v, sizeof(T*), std::alignment_of_v, sizeof(T), initial_size, pointer_adjusted, data_adjusted); + bool result + = attempt_alloc(L, std::alignment_of_v, sizeof(T*), std::alignment_of_v, sizeof(T), initial_size, pointer_adjusted, data_adjusted); if (!result) { // we're likely to get something that fails to perform the proper allocation a second time, // so we use the suggested_new_size bump to help us out here pointer_adjusted = nullptr; data_adjusted = nullptr; - result = attempt_alloc(L, std::alignment_of_v, sizeof(T*), std::alignment_of_v, sizeof(T), misaligned_size, pointer_adjusted, data_adjusted); + result = attempt_alloc( + L, std::alignment_of_v, sizeof(T*), std::alignment_of_v, sizeof(T), misaligned_size, pointer_adjusted, data_adjusted); if (!result) { if (pointer_adjusted == nullptr) { luaL_error(L, "aligned allocation of userdata block (pointer section) for '%s' failed", detail::demangle().c_str()); @@ -9934,30 +9923,35 @@ namespace sol { template int comparsion_operator_wrap(lua_State* L) { - auto maybel = stack::unqualified_check_get(L, 1); - if (!maybel) { + if constexpr (std::is_void_v) { return stack::push(L, false); } - auto mayber = stack::unqualified_check_get(L, 2); - if (!mayber) { - return stack::push(L, false); - } - decltype(auto) l = *maybel; - decltype(auto) r = *mayber; - if constexpr (std::is_same_v) { - std::equal_to<> op; - return stack::push(L, op(detail::ptr(l), detail::ptr(r))); - } else { - if constexpr (std::is_same_v, Op> // cf-hack - || std::is_same_v, Op> // - || std::is_same_v, Op>) { // - if (detail::ptr(l) == detail::ptr(r)) { - return stack::push(L, true); - } + auto maybel = stack::unqualified_check_get(L, 1); + if (!maybel) { + return stack::push(L, false); + } + auto mayber = stack::unqualified_check_get(L, 2); + if (!mayber) { + return stack::push(L, false); + } + decltype(auto) l = *maybel; + decltype(auto) r = *mayber; + if constexpr (std::is_same_v) { + std::equal_to<> op; + return stack::push(L, op(detail::ptr(l), detail::ptr(r))); + } + else { + if constexpr (std::is_same_v, Op> // cf-hack + || std::is_same_v, Op> // + || std::is_same_v, Op>) { // + if (detail::ptr(l) == detail::ptr(r)) { + return stack::push(L, true); + } + } + Op op; + return stack::push(L, op(detail::deref(l), detail::deref(r))); } - Op op; - return stack::push(L, op(detail::deref(l), detail::deref(r))); } } diff --git a/tests/runtime_tests/source/usertype_unique.cpp b/tests/runtime_tests/source/usertype_unique.cpp index 6be7bde7..f67faab3 100644 --- a/tests/runtime_tests/source/usertype_unique.cpp +++ b/tests/runtime_tests/source/usertype_unique.cpp @@ -158,3 +158,16 @@ TEST_CASE("usertype/unique_usertype-check", "make sure unique usertypes don't ge my_func(std::make_shared()); }()); } + +TEST_CASE("usertype/unique void pointers", "can compile shared_ptr types and not trip the compiler or sol2's internals") { + sol::state lua; + lua.set_function("f", [](std::shared_ptr d) { + int* pi = static_cast(d.get()); + REQUIRE(*pi == 567); + }); + + std::shared_ptr s = std::make_shared(567); + lua["s"] = std::move(s); + auto result = lua.safe_script("f(s)", sol::script_pass_on_error); + REQUIRE(result.valid()); +}