From a65c32c09474d265e9487488a846d24cefc7b26b Mon Sep 17 00:00:00 2001 From: ZXShady <153229951+ZXShady@users.noreply.github.com> Date: Thu, 11 Sep 2025 09:12:09 +0300 Subject: [PATCH 1/2] Fix clang format dupe --- .clang-format | 1 - 1 file changed, 1 deletion(-) diff --git a/.clang-format b/.clang-format index f75a5542..9a25bae9 100644 --- a/.clang-format +++ b/.clang-format @@ -74,7 +74,6 @@ BraceWrapping: AfterControlStatement: false AfterClass: false AfterNamespace: false - AfterStruct: false AfterUnion: false BeforeElse: true BeforeCatch: true From 9814d0ca89def618a48bc625a53750a1e687a768 Mon Sep 17 00:00:00 2001 From: ZXShady <153229951+ZXShady@users.noreply.github.com> Date: Thu, 11 Sep 2025 09:11:39 +0300 Subject: [PATCH 2/2] Simplify traits implementation --- include/sol/base_traits.hpp | 34 +++-- include/sol/bind_traits.hpp | 19 +-- include/sol/traits.hpp | 278 ++++++++++++++++++------------------ 3 files changed, 169 insertions(+), 162 deletions(-) diff --git a/include/sol/base_traits.hpp b/include/sol/base_traits.hpp index 204afc27..9c0498e0 100644 --- a/include/sol/base_traits.hpp +++ b/include/sol/base_traits.hpp @@ -36,8 +36,13 @@ namespace sol { using sfinae_yes_t = std::true_type; using sfinae_no_t = std::false_type; - template - using void_t = void; + template + using constant = std::integral_constant; + + template + using boolean = std::bool_constant; + + using std::void_t; template using unqualified = std::remove_cv>; @@ -115,39 +120,40 @@ namespace sol { namespace meta_detail { template class Templ> - struct is_specialization_of : std::false_type { }; - template class Templ> - struct is_specialization_of, Templ> : std::true_type { }; + inline constexpr bool is_specialization_of_v = false; + + template class Templ> + inline constexpr bool is_specialization_of_v, Templ> = true; + } // namespace meta_detail template class Templ> - using is_specialization_of = meta_detail::is_specialization_of, Templ>; + inline constexpr bool is_specialization_of_v = meta_detail::is_specialization_of_v, Templ>; template class Templ> - inline constexpr bool is_specialization_of_v = is_specialization_of, Templ>::value; + using is_specialization_of = boolean>; template struct identity { - typedef T type; + using type = T; }; template using identity_t = typename identity::type; template - using is_builtin_type = std::integral_constant::value || std::is_pointer::value || std::is_array::value>; + using is_builtin_type = boolean || std::is_pointer_v || std::is_array_v>; namespace meta_detail { template - struct has_internal_marker_impl : std::false_type { }; - template - struct has_internal_marker_impl> : std::true_type { }; + constexpr inline bool has_internal_marker_v = false; template - using has_internal_marker = has_internal_marker_impl; + constexpr inline bool has_internal_marker_v> = true; template - constexpr inline bool has_internal_marker_v = has_internal_marker::value; + using has_internal_marker = boolean>; + } // namespace meta_detail } // namespace meta diff --git a/include/sol/bind_traits.hpp b/include/sol/bind_traits.hpp index a85bba61..d61c1106 100644 --- a/include/sol/bind_traits.hpp +++ b/include/sol/bind_traits.hpp @@ -27,6 +27,8 @@ #include #include #include +#include +#include namespace sol { namespace meta { namespace meta_detail { @@ -47,7 +49,7 @@ namespace sol { namespace meta { template struct void_tuple_element> { - typedef void type; + using type = void; }; template @@ -510,13 +512,14 @@ namespace sol { namespace meta { inline static constexpr bool is_member_function = false; inline static constexpr std::size_t arity = 1; inline static constexpr std::size_t free_arity = 2; - typedef std::tuple args_tuple; - typedef types args_list; - typedef types free_args_list; - typedef meta::tuple_types returns_list; - typedef return_type(function_type)(T&, return_type); - typedef return_type (*function_pointer_type)(T&, Arg); - typedef return_type (*free_function_pointer_type)(T&, Arg); + using args_tuple = std::tuple; + using args_list = types; + using free_args_list = types; + using returns_list = meta::tuple_types; + // This is probably a mistake prolly meant (T& , Arg) + using function_type = return_type(T&, return_type); + using function_pointer_type = return_type (*)(T&, Arg); + using free_function_pointer_type = function_pointer_type; template using arg_at = void_tuple_element_t; }; diff --git a/include/sol/traits.hpp b/include/sol/traits.hpp index 372c97be..3681a170 100644 --- a/include/sol/traits.hpp +++ b/include/sol/traits.hpp @@ -45,12 +45,12 @@ namespace sol { namespace meta { template struct unwrapped { - typedef T type; + using type = T; }; template struct unwrapped> { - typedef T type; + using type = T; }; template @@ -71,33 +71,44 @@ namespace sol { namespace meta { }; template - struct remove_member_pointer { + struct remove_member_pointer { typedef R type; }; template using remove_member_pointer_t = remove_member_pointer; - template - struct all_same : std::true_type { }; + namespace meta_detail { + constexpr bool any_of(const std::initializer_list list) { + for (const bool b : list) + if (b) + return true; + return false; + } + + constexpr bool all_of(const std::initializer_list list) { + for (const bool b : list) + if (!b) + return false; + return true; + } + + } // namespace meta_detail - template - struct all_same : std::integral_constant::value && all_same::value> { }; - template - struct any_same : std::false_type { }; - template - struct any_same : std::integral_constant::value || any_same::value> { }; + template + constexpr inline bool any_same_v = meta_detail::any_of({ std::is_same_v... }); + + template + using any_same = boolean>; template - constexpr inline bool any_same_v = any_same::value; + constexpr inline bool all_same_v = meta_detail::all_of({ std::is_same_v... }); - template - using boolean = std::integral_constant; + template + using all_same = boolean>; - template - constexpr inline bool boolean_v = boolean::value; template using neg = boolean; @@ -142,29 +153,36 @@ namespace sol { namespace meta { template using disable_any = std::enable_if_t>::value, enable_t>; - template - struct find_in_pack_v : boolean { }; - template - struct find_in_pack_v : any, find_in_pack_v> { }; namespace meta_detail { - template - struct index_in_pack : std::integral_constant { }; - template - struct index_in_pack - : conditional_t::value, std::integral_constant, index_in_pack> { }; + constexpr std::ptrdiff_t index_of(const std::initializer_list list) { + std::ptrdiff_t i = 0; + for (const bool b : list) { + if (b) + return i; + ++i; + } + return -1; + } + } // namespace meta_detail + + template - struct index_in_pack : meta_detail::index_in_pack<0, T, Args...> { }; + using index_in_pack = constant<(meta_detail::index_of({ std::is_same_v... }))>; + template + using find_in_pack_v = boolean<(index_in_pack::value != -1)>; + + // this is probably wrong template - struct index_in : meta_detail::index_in_pack<0, T, List> { }; + struct index_in : index_in_pack { }; template - struct index_in> : meta_detail::index_in_pack<0, T, Args...> { }; + struct index_in> : index_in_pack { }; template struct at_in_pack { }; @@ -177,7 +195,7 @@ namespace sol { namespace meta { template struct at_in_pack<0, Arg, Args...> { - typedef Arg type; + using type = Arg; }; namespace meta_detail { @@ -190,18 +208,38 @@ namespace sol { namespace meta { template using on_always = std::true_type; - template