| 1 | /*! |
| 2 | @file |
| 3 | Forward declares `boost::hana::range`. |
| 4 | |
| 5 | Copyright Louis Dionne 2013-2022 |
| 6 | Distributed under the Boost Software License, Version 1.0. |
| 7 | (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) |
| 8 | */ |
| 9 | |
| 10 | #ifndef BOOST_HANA_FWD_RANGE_HPP |
| 11 | #define BOOST_HANA_FWD_RANGE_HPP |
| 12 | |
| 13 | #include <boost/hana/config.hpp> |
| 14 | #include <boost/hana/fwd/core/make.hpp> |
| 15 | #include <boost/hana/fwd/integral_constant.hpp> |
| 16 | |
| 17 | |
| 18 | namespace boost { namespace hana { |
| 19 | #ifdef BOOST_HANA_DOXYGEN_INVOKED |
| 20 | //! @ingroup group-datatypes |
| 21 | //! Compile-time half-open interval of `hana::integral_constant`s. |
| 22 | //! |
| 23 | //! A `range` represents a half-open interval of the form `[from, to)` |
| 24 | //! containing `hana::integral_constant`s of a given type. The `[from, to)` |
| 25 | //! notation represents the values starting at `from` (inclusively) up |
| 26 | //! to but excluding `from`. In other words, it is a bit like the list |
| 27 | //! `from, from+1, ..., to-1`. |
| 28 | //! |
| 29 | //! In particular, note that the bounds of the range can be any |
| 30 | //! `hana::integral_constant`s (negative numbers are allowed) and the |
| 31 | //! range does not have to start at zero. The only requirement is that |
| 32 | //! `from <= to`. |
| 33 | //! |
| 34 | //! @note |
| 35 | //! The representation of `hana::range` is implementation defined. In |
| 36 | //! particular, one should not take for granted the number and types |
| 37 | //! of template parameters. The proper way to create a `hana::range` |
| 38 | //! is to use `hana::range_c` or `hana::make_range`. More details |
| 39 | //! [in the tutorial](@ref tutorial-containers-types). |
| 40 | //! |
| 41 | //! |
| 42 | //! Modeled concepts |
| 43 | //! ---------------- |
| 44 | //! 1. `Comparable`\n |
| 45 | //! Two ranges are equal if and only if they are both empty or they both |
| 46 | //! span the same interval. |
| 47 | //! @include example/range/comparable.cpp |
| 48 | //! |
| 49 | //! 2. `Foldable`\n |
| 50 | //! Folding a `range` is equivalent to folding a list of the |
| 51 | //! `integral_constant`s in the interval it spans. |
| 52 | //! @include example/range/foldable.cpp |
| 53 | //! |
| 54 | //! 3. `Iterable`\n |
| 55 | //! Iterating over a `range` is equivalent to iterating over a list of |
| 56 | //! the values it spans. In other words, iterating over the range |
| 57 | //! `[from, to)` is equivalent to iterating over a list containing |
| 58 | //! `from, from+1, from+2, ..., to-1`. Also note that `operator[]` can |
| 59 | //! be used in place of the `at` function. |
| 60 | //! @include example/range/iterable.cpp |
| 61 | //! |
| 62 | //! 4. `Searchable`\n |
| 63 | //! Searching a `range` is equivalent to searching a list of the values |
| 64 | //! in the range `[from, to)`, but it is much more compile-time efficient. |
| 65 | //! @include example/range/searchable.cpp |
| 66 | template <typename T, T from, T to> |
| 67 | struct range { |
| 68 | //! Equivalent to `hana::equal` |
| 69 | template <typename X, typename Y> |
| 70 | friend constexpr auto operator==(X&& x, Y&& y); |
| 71 | |
| 72 | //! Equivalent to `hana::not_equal` |
| 73 | template <typename X, typename Y> |
| 74 | friend constexpr auto operator!=(X&& x, Y&& y); |
| 75 | |
| 76 | //! Equivalent to `hana::at` |
| 77 | template <typename N> |
| 78 | constexpr decltype(auto) operator[](N&& n); |
| 79 | }; |
| 80 | #else |
| 81 | template <typename T, T from, T to> |
| 82 | struct range; |
| 83 | #endif |
| 84 | |
| 85 | //! Tag representing a `hana::range`. |
| 86 | //! @relates hana::range |
| 87 | struct range_tag { }; |
| 88 | |
| 89 | #ifdef BOOST_HANA_DOXYGEN_INVOKED |
| 90 | //! Create a `hana::range` representing a half-open interval of |
| 91 | //! `integral_constant`s. |
| 92 | //! @relates hana::range |
| 93 | //! |
| 94 | //! Given two `IntegralConstant`s `from` and `to`, `make<range_tag>` |
| 95 | //! returns a `hana::range` representing the half-open interval of |
| 96 | //! `integral_constant`s `[from, to)`. `from` and `to` must form a |
| 97 | //! valid interval, which means that `from <= to` must be true. Otherwise, |
| 98 | //! a compilation error is triggered. Also note that if `from` and `to` |
| 99 | //! are `IntegralConstant`s with different underlying integral types, |
| 100 | //! the created range contains `integral_constant`s whose underlying |
| 101 | //! type is their common type. |
| 102 | //! |
| 103 | //! |
| 104 | //! Example |
| 105 | //! ------- |
| 106 | //! @include example/range/make.cpp |
| 107 | template <> |
| 108 | constexpr auto make<range_tag> = [](auto const& from, auto const& to) { |
| 109 | return range<implementation_defined>{implementation_defined}; |
| 110 | }; |
| 111 | #endif |
| 112 | |
| 113 | //! Alias to `make<range_tag>`; provided for convenience. |
| 114 | //! @relates hana::range |
| 115 | BOOST_HANA_INLINE_VARIABLE constexpr auto make_range = make<range_tag>; |
| 116 | |
| 117 | //! Shorthand to create a `hana::range` with the given bounds. |
| 118 | //! @relates hana::range |
| 119 | //! |
| 120 | //! This shorthand is provided for convenience only and it is equivalent |
| 121 | //! to `make_range`. Specifically, `range_c<T, from, to>` is such that |
| 122 | //! @code |
| 123 | //! range_c<T, from, to> == make_range(integral_c<T, from>, integral_c<T, to>) |
| 124 | //! @endcode |
| 125 | //! |
| 126 | //! |
| 127 | //! @tparam T |
| 128 | //! The underlying integral type of the `integral_constant`s in the |
| 129 | //! created range. |
| 130 | //! |
| 131 | //! @tparam from |
| 132 | //! The inclusive lower bound of the created range. |
| 133 | //! |
| 134 | //! @tparam to |
| 135 | //! The exclusive upper bound of the created range. |
| 136 | //! |
| 137 | //! |
| 138 | //! Example |
| 139 | //! ------- |
| 140 | //! @include example/range/range_c.cpp |
| 141 | #ifdef BOOST_HANA_DOXYGEN_INVOKED |
| 142 | template <typename T, T from, T to> |
| 143 | constexpr auto range_c = make_range(integral_c<T, from>, integral_c<T, to>); |
| 144 | #else |
| 145 | template <typename T, T from, T to> |
| 146 | BOOST_HANA_INLINE_VARIABLE constexpr range<T, from, to> range_c{}; |
| 147 | #endif |
| 148 | }} // end namespace boost::hana |
| 149 | |
| 150 | #endif // !BOOST_HANA_FWD_RANGE_HPP |
| 151 | |