From 10c358cfdd9689852ad76aeec34204cf10db5fa6 Mon Sep 17 00:00:00 2001 From: Patrick Roberts Date: Sun, 9 Feb 2025 17:07:30 -0600 Subject: [PATCH] Reduce macro complexity for copyable vs. move_only --- include/beman/any_view/any_view.hpp | 21 +++-- include/beman/any_view/any_view_options.hpp | 36 ++++--- include/beman/any_view/concepts.hpp | 7 +- include/beman/any_view/config.hpp.in | 10 -- .../any_view/detail/iterator_adaptor.hpp | 4 +- .../beman/any_view/detail/view_adaptor.hpp | 4 +- include/beman/any_view/range_traits.hpp | 27 +++--- tests/beman/any_view/options.hpp | 24 +++-- tests/beman/any_view/sfinae.test.cpp | 94 ++++++++++--------- 9 files changed, 124 insertions(+), 103 deletions(-) diff --git a/include/beman/any_view/any_view.hpp b/include/beman/any_view/any_view.hpp index ac998e1..18b871f 100644 --- a/include/beman/any_view/any_view.hpp +++ b/include/beman/any_view/any_view.hpp @@ -52,10 +52,11 @@ class any_view : public std::ranges::view_interface; // inplace storage sufficient for a vtable pointer and a std::vector @@ -111,10 +112,11 @@ class any_view : public std::ranges::view_interface; static constexpr bool copyable = -#if BEMAN_ANY_VIEW_USE_MOVE_ONLY() - not +#if BEMAN_ANY_VIEW_USE_COPYABLE() + detail::copyable_or_v; +#elif BEMAN_ANY_VIEW_USE_MOVE_ONLY() + not detail::move_only_or_v; #endif - detail::BEMAN_ANY_VIEW_OPTION_(or_v); using interface_type = detail::view_interface; @@ -174,10 +176,11 @@ class any_view : public std::ranges::view_interface static constexpr bool sized = OptionsV.sized; static constexpr bool copyable = -#if BEMAN_ANY_VIEW_USE_MOVE_ONLY() - not +#if BEMAN_ANY_VIEW_USE_COPYABLE() + OptionsV.copyable; +#elif BEMAN_ANY_VIEW_USE_MOVE_ONLY() + not OptionsV.move_only; #endif - OptionsV.BEMAN_ANY_VIEW_OPTION(); using interface_type = detail::view_interface; diff --git a/include/beman/any_view/any_view_options.hpp b/include/beman/any_view/any_view_options.hpp index 016e039..1e356f9 100644 --- a/include/beman/any_view/any_view_options.hpp +++ b/include/beman/any_view/any_view_options.hpp @@ -22,14 +22,18 @@ namespace beman::any_view { #if BEMAN_ANY_VIEW_USE_ENUM() enum class any_view_options { - input = 0b0000000, - forward = 0b0000001, - bidirectional = 0b0000011, - random_access = 0b0000111, - contiguous = 0b0001111, - sized = 0b0010000, - borrowed = 0b0100000, - BEMAN_ANY_VIEW_OPTION() = 0b1000000, + input = 0b0000000, + forward = 0b0000001, + bidirectional = 0b0000011, + random_access = 0b0000111, + contiguous = 0b0001111, + sized = 0b0010000, + borrowed = 0b0100000, +#if BEMAN_ANY_VIEW_USE_COPYABLE() + copyable = 0b1000000, +#elif BEMAN_ANY_VIEW_USE_MOVE_ONLY() + move_only = 0b1000000, +#endif }; [[nodiscard]] constexpr auto operator|(any_view_options l, any_view_options r) noexcept -> any_view_options { @@ -92,12 +96,16 @@ template struct any_view_options { type_t reference_type; - type_t iterator_concept = {}; - bool sized = false; - bool BEMAN_ANY_VIEW_OPTION() = false; - bool borrowed = false; - type_t rvalue_reference_type = {}; - type_t difference_type = {}; + type_t iterator_concept = {}; + bool sized = false; +#if BEMAN_ANY_VIEW_USE_COPYABLE() + bool copyable = false; +#elif BEMAN_ANY_VIEW_USE_MOVE_ONLY() + bool move_only = false; +#endif + bool borrowed = false; + type_t rvalue_reference_type = {}; + type_t difference_type = {}; }; #endif diff --git a/include/beman/any_view/concepts.hpp b/include/beman/any_view/concepts.hpp index 9f6f656..1f6ed67 100644 --- a/include/beman/any_view/concepts.hpp +++ b/include/beman/any_view/concepts.hpp @@ -27,10 +27,11 @@ concept borrowed_range_compatible_with = not RangeTraitsT::borrowed or std::rang template concept copyable_view_compatible_with = #if BEMAN_ANY_VIEW_USE_COPYABLE() - not + not RangeTraitsT::copyable +#elif BEMAN_ANY_VIEW_USE_MOVE_ONLY() + RangeTraitsT::move_only #endif - RangeTraitsT::BEMAN_ANY_VIEW_OPTION() or - std::copyable; + or std::copyable; template concept view_compatible_with = diff --git a/include/beman/any_view/config.hpp.in b/include/beman/any_view/config.hpp.in index d0b35ee..5352bed 100644 --- a/include/beman/any_view/config.hpp.in +++ b/include/beman/any_view/config.hpp.in @@ -10,14 +10,4 @@ #cmakedefine01 BEMAN_ANY_VIEW_USE_COPYABLE() #cmakedefine01 BEMAN_ANY_VIEW_USE_MOVE_ONLY() -#if BEMAN_ANY_VIEW_USE_COPYABLE() -#define BEMAN_ANY_VIEW_OPTION() copyable -#define BEMAN_ANY_VIEW_OPTION_(suffix) copyable_##suffix -#elif BEMAN_ANY_VIEW_USE_MOVE_ONLY() -#define BEMAN_ANY_VIEW_OPTION() move_only -#define BEMAN_ANY_VIEW_OPTION_(suffix) move_only_##suffix -#endif - -#define BEMAN_ANY_VIEW_CAT(a, b) a##b - #endif // BEMAN_ANY_VIEW_CONFIG_HPP diff --git a/include/beman/any_view/detail/iterator_adaptor.hpp b/include/beman/any_view/detail/iterator_adaptor.hpp index 9e58a88..362319d 100644 --- a/include/beman/any_view/detail/iterator_adaptor.hpp +++ b/include/beman/any_view/detail/iterator_adaptor.hpp @@ -18,7 +18,7 @@ template SentinelT> -class iterator_adaptor : public iterator_interface { +class iterator_adaptor final : public iterator_interface { [[no_unique_address]] IteratorT iterator; [[no_unique_address]] SentinelT sentinel; @@ -139,7 +139,7 @@ class iterator_adaptor : public iterator_interface -class view_adaptor : public view_interface { +class view_adaptor final : public view_interface { using iterator = detail::any_iterator; using size_type = std::make_unsigned_t; @@ -67,7 +67,7 @@ class view_adaptor : public view_interface \ - using name = std::bool_constant; \ - \ - template \ - inline constexpr bool BEMAN_ANY_VIEW_CAT(name, _or_v) = \ - detected_or_t, name, RangeTraitsT>::value +#define BEMAN_ANY_VIEW_BOOL_TRAIT(name) \ + template \ + using name = std::bool_constant; \ + \ + template \ + inline constexpr bool name##_or_v = detected_or_t, name, RangeTraitsT>::value BEMAN_ANY_VIEW_BOOL_TRAIT(sized); BEMAN_ANY_VIEW_BOOL_TRAIT(borrowed); -BEMAN_ANY_VIEW_BOOL_TRAIT(BEMAN_ANY_VIEW_OPTION()); +#if BEMAN_ANY_VIEW_USE_COPYABLE() +BEMAN_ANY_VIEW_BOOL_TRAIT(copyable); +#elif BEMAN_ANY_VIEW_USE_MOVE_ONLY() +BEMAN_ANY_VIEW_BOOL_TRAIT(move_only); +#endif #undef BEMAN_ANY_VIEW_BOOL_TRAIT @@ -80,11 +83,11 @@ struct range_traits { static constexpr bool sized = std::ranges::sized_range; static constexpr bool borrowed = std::ranges::borrowed_range; - static constexpr bool BEMAN_ANY_VIEW_OPTION() = -#if BEMAN_ANY_VIEW_USE_MOVE_ONLY() - not +#if BEMAN_ANY_VIEW_USE_COPYABLE() + static constexpr bool copyable = std::copyable; +#elif BEMAN_ANY_VIEW_USE_MOVE_ONLY() + static constexpr bool move_only = not std::copyable; #endif - std::copyable; }; } // namespace beman::any_view diff --git a/tests/beman/any_view/options.hpp b/tests/beman/any_view/options.hpp index 6698e31..2821d4a 100644 --- a/tests/beman/any_view/options.hpp +++ b/tests/beman/any_view/options.hpp @@ -90,9 +90,11 @@ inline constexpr any_view_options move_only_options{ template inline constexpr any_view_options iterator_options{ - .reference_type = default_reference_type, - .iterator_concept = type, - .BEMAN_ANY_VIEW_OPTION() = move_only_options.BEMAN_ANY_VIEW_OPTION(), + .reference_type = default_reference_type, + .iterator_concept = type, +#if BEMAN_ANY_VIEW_USE_MOVE_ONLY() + .move_only = true, +#endif }; inline constexpr auto input_options = iterator_options; @@ -102,15 +104,19 @@ inline constexpr auto random_access_options = iterator_options; inline constexpr any_view_options sized_options{ - .reference_type = default_reference_type, - .sized = true, - .BEMAN_ANY_VIEW_OPTION() = move_only_options.BEMAN_ANY_VIEW_OPTION(), + .reference_type = default_reference_type, + .sized = true, +#if BEMAN_ANY_VIEW_USE_MOVE_ONLY() + .move_only = true, +#endif }; inline constexpr any_view_options borrowed_options{ - .reference_type = default_reference_type, - .BEMAN_ANY_VIEW_OPTION() = move_only_options.BEMAN_ANY_VIEW_OPTION(), - .borrowed = true, + .reference_type = default_reference_type, +#if BEMAN_ANY_VIEW_USE_MOVE_ONLY() + .move_only = true, +#endif + .borrowed = true, }; #endif diff --git a/tests/beman/any_view/sfinae.test.cpp b/tests/beman/any_view/sfinae.test.cpp index 29da915..7796c44 100644 --- a/tests/beman/any_view/sfinae.test.cpp +++ b/tests/beman/any_view/sfinae.test.cpp @@ -92,48 +92,58 @@ TEST(SfinaeTest, vector_of_bool) { static_assert(std::constructible_from>, std::vector&>); static_assert(std::constructible_from>, std::vector&>); #elif BEMAN_ANY_VIEW_USE_NAMED() - static_assert( - std::constructible_from, - .iterator_concept = type, - .BEMAN_ANY_VIEW_OPTION() = move_only_options.BEMAN_ANY_VIEW_OPTION()}>, - std::vector>); - static_assert( - std::constructible_from, - .sized = true, - .BEMAN_ANY_VIEW_OPTION() = move_only_options.BEMAN_ANY_VIEW_OPTION()}>, - std::vector>); - static_assert( - std::constructible_from, - .BEMAN_ANY_VIEW_OPTION() = move_only_options.BEMAN_ANY_VIEW_OPTION()}>, - std::vector>); - static_assert( - not std::constructible_from, - .BEMAN_ANY_VIEW_OPTION() = move_only_options.BEMAN_ANY_VIEW_OPTION(), - .borrowed = true}>, - std::vector>); - static_assert( - not std::constructible_from, - .BEMAN_ANY_VIEW_OPTION() = copyable_options.BEMAN_ANY_VIEW_OPTION(), - .borrowed = true}>, - std::vector>); - - static_assert( - std::constructible_from, - .BEMAN_ANY_VIEW_OPTION() = move_only_options.BEMAN_ANY_VIEW_OPTION(), - .borrowed = true}>, - std::vector&>); - static_assert( - std::constructible_from, - .BEMAN_ANY_VIEW_OPTION() = copyable_options.BEMAN_ANY_VIEW_OPTION(), - .borrowed = true}>, - std::vector&>); + static_assert(std::constructible_from, + .iterator_concept = type, +#if BEMAN_ANY_VIEW_USE_MOVE_ONLY() + .move_only = true +#endif + }>, + std::vector>); + static_assert(std::constructible_from, + .sized = true, +#if BEMAN_ANY_VIEW_USE_MOVE_ONLY() + .move_only = true +#endif + }>, + std::vector>); + static_assert(std::constructible_from, +#if BEMAN_ANY_VIEW_USE_MOVE_ONLY() + .move_only = true +#endif + }>, + std::vector>); + static_assert(not std::constructible_from, +#if BEMAN_ANY_VIEW_USE_MOVE_ONLY() + .move_only = true, +#endif + .borrowed = true}>, + std::vector>); + static_assert(not std::constructible_from, +#if BEMAN_ANY_VIEW_USE_MOVE_ONLY() + .move_only = true, +#endif + .borrowed = true}>, + std::vector>); + + static_assert(std::constructible_from, +#if BEMAN_ANY_VIEW_USE_MOVE_ONLY() + .move_only = true, +#endif + .borrowed = true}>, + std::vector&>); + static_assert(std::constructible_from, +#if BEMAN_ANY_VIEW_USE_MOVE_ONLY() + .move_only = true, +#endif + .borrowed = true}>, + std::vector&>); #endif }