|
| 1 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | + |
| 3 | +#ifndef BEMAN_ANY_VIEW_CONCEPTS_HPP |
| 4 | +#define BEMAN_ANY_VIEW_CONCEPTS_HPP |
| 5 | + |
| 6 | +#include <beman/any_view/range_traits.hpp> |
| 7 | +#include <iterator> |
| 8 | + |
| 9 | +namespace beman::any_view { |
| 10 | +namespace detail { |
| 11 | + |
| 12 | +template <class IterT, class IterConceptT> |
| 13 | +concept input_iterator_compatible_with = |
| 14 | + std::input_or_output_iterator<IterT> and |
| 15 | + (not std::derived_from<IterConceptT, std::input_iterator_tag> or std::input_iterator<IterT>); |
| 16 | + |
| 17 | +template <class IterT, class IterConceptT> |
| 18 | +concept forward_iterator_compatible_with = |
| 19 | + input_iterator_compatible_with<IterT, IterConceptT> and |
| 20 | + (not std::derived_from<IterConceptT, std::forward_iterator_tag> or std::forward_iterator<IterT>); |
| 21 | + |
| 22 | +template <class IterT, class IterConceptT> |
| 23 | +concept bidirectional_iterator_compatible_with = |
| 24 | + forward_iterator_compatible_with<IterT, IterConceptT> and |
| 25 | + (not std::derived_from<IterConceptT, std::bidirectional_iterator_tag> or std::bidirectional_iterator<IterT>); |
| 26 | + |
| 27 | +template <class IterT, class IterConceptT> |
| 28 | +concept random_access_iterator_compatible_with = |
| 29 | + bidirectional_iterator_compatible_with<IterT, IterConceptT> and |
| 30 | + (not std::derived_from<IterConceptT, std::random_access_iterator_tag> or std::random_access_iterator<IterT>); |
| 31 | + |
| 32 | +template <class IterT, class IterConceptT> |
| 33 | +concept contiguous_iterator_compatible_with = |
| 34 | + random_access_iterator_compatible_with<IterT, IterConceptT> and |
| 35 | + (not std::derived_from<IterConceptT, std::contiguous_iterator_tag> or std::contiguous_iterator<IterT>); |
| 36 | + |
| 37 | +template <class RangeRefT, class TraitsRefT, class IterConceptT> |
| 38 | +concept contiguous_reference_convertible_to = |
| 39 | + std::convertible_to<RangeRefT, TraitsRefT> and |
| 40 | + (not std::derived_from<IterConceptT, std::contiguous_iterator_tag> or |
| 41 | + std::convertible_to<std::remove_reference_t<RangeRefT> (*)[], std::remove_reference_t<TraitsRefT> (*)[]>); |
| 42 | + |
| 43 | +template <class RangeT, class RangeTraitsT> |
| 44 | +concept sized_range_compatible_with = not RangeTraitsT::sized or std::ranges::sized_range<RangeT>; |
| 45 | + |
| 46 | +template <class RangeT, class RangeTraitsT> |
| 47 | +concept borrowed_range_compatible_with = not RangeTraitsT::borrowed or std::ranges::borrowed_range<RangeT>; |
| 48 | + |
| 49 | +template <class RangeT, class RangeTraitsT> |
| 50 | +concept range_compatible_with = |
| 51 | + std::ranges::range<RangeT> and |
| 52 | + contiguous_iterator_compatible_with<std::ranges::iterator_t<RangeT>, iterator_concept_t<RangeTraitsT>> and |
| 53 | + contiguous_reference_convertible_to<std::ranges::range_reference_t<RangeT>, |
| 54 | + reference_type_t<RangeTraitsT>, |
| 55 | + iterator_concept_t<RangeTraitsT>> and |
| 56 | + std::convertible_to<std::ranges::range_rvalue_reference_t<RangeT>, rvalue_reference_type_t<RangeTraitsT>> and |
| 57 | + std::convertible_to<std::ranges::range_difference_t<RangeT>, difference_type_t<RangeTraitsT>> and |
| 58 | + sized_range_compatible_with<RangeT, RangeTraitsT> and borrowed_range_compatible_with<RangeT, RangeTraitsT>; |
| 59 | + |
| 60 | +template <class ViewT, class RangeTraitsT> |
| 61 | +concept copyable_view_compatible_with = |
| 62 | +#if BEMAN_ANY_VIEW_USE_COPYABLE() |
| 63 | + not |
| 64 | +#endif |
| 65 | + RangeTraitsT::BEMAN_ANY_VIEW_OPTION() or |
| 66 | + std::copyable<ViewT>; |
| 67 | + |
| 68 | +template <bool SimpleV, class ViewT> |
| 69 | +using const_if_t = std::conditional_t<SimpleV, const ViewT, ViewT>; |
| 70 | + |
| 71 | +template <class ViewT, class RangeTraitsT> |
| 72 | +concept view_compatible_with = copyable_view_compatible_with<ViewT, RangeTraitsT> and |
| 73 | + range_compatible_with<const_if_t<RangeTraitsT::simple, ViewT>, RangeTraitsT>; |
| 74 | + |
| 75 | +} // namespace detail |
| 76 | + |
| 77 | +template <class RangeT, class RangeTraitsT> |
| 78 | +concept ext_viewable_range_compatible_with = |
| 79 | + std::ranges::viewable_range<RangeT> and detail::view_compatible_with<std::views::all_t<RangeT>, RangeTraitsT>; |
| 80 | + |
| 81 | +} // namespace beman::any_view |
| 82 | + |
| 83 | +#endif // BEMAN_ANY_VIEW_CONCEPTS_HPP |
0 commit comments