| 1 | /*! |
| 2 | @file |
| 3 | Adapts `boost::tuple` for use with Hana. |
| 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_EXT_BOOST_TUPLE_HPP |
| 11 | #define BOOST_HANA_EXT_BOOST_TUPLE_HPP |
| 12 | |
| 13 | #include <boost/hana/bool.hpp> |
| 14 | #include <boost/hana/config.hpp> |
| 15 | #include <boost/hana/detail/decay.hpp> |
| 16 | #include <boost/hana/fwd/at.hpp> |
| 17 | #include <boost/hana/fwd/core/make.hpp> |
| 18 | #include <boost/hana/fwd/core/tag_of.hpp> |
| 19 | #include <boost/hana/fwd/drop_front.hpp> |
| 20 | #include <boost/hana/fwd/is_empty.hpp> |
| 21 | #include <boost/hana/fwd/length.hpp> |
| 22 | #include <boost/hana/integral_constant.hpp> |
| 23 | |
| 24 | #include <boost/tuple/tuple.hpp> |
| 25 | |
| 26 | #include <cstddef> |
| 27 | #include <utility> |
| 28 | |
| 29 | |
| 30 | #ifdef BOOST_HANA_DOXYGEN_INVOKED |
| 31 | namespace boost { |
| 32 | //! @ingroup group-ext-boost |
| 33 | //! Adapter for `boost::tuple`s. |
| 34 | //! |
| 35 | //! |
| 36 | //! Modeled concepts |
| 37 | //! ---------------- |
| 38 | //! A `boost::tuple` is a model of the `Sequence` concept, and all the |
| 39 | //! concepts it refines. That makes it essentially the same as a Hana |
| 40 | //! tuple, although the complexity of some operations might differ from |
| 41 | //! that of Hana's tuple. |
| 42 | //! |
| 43 | //! @include example/ext/boost/tuple.cpp |
| 44 | template <typename ...T> |
| 45 | struct tuple { }; |
| 46 | } |
| 47 | #endif |
| 48 | |
| 49 | |
| 50 | namespace boost { namespace hana { |
| 51 | namespace ext { namespace boost { struct tuple_tag; }} |
| 52 | |
| 53 | template <typename ...Xs> |
| 54 | struct tag_of<boost::tuple<Xs...>> { |
| 55 | using type = ext::boost::tuple_tag; |
| 56 | }; |
| 57 | |
| 58 | template <typename H, typename T> |
| 59 | struct tag_of<boost::tuples::cons<H, T>> { |
| 60 | using type = ext::boost::tuple_tag; |
| 61 | }; |
| 62 | |
| 63 | template <> |
| 64 | struct tag_of<boost::tuples::null_type> { |
| 65 | using type = ext::boost::tuple_tag; |
| 66 | }; |
| 67 | |
| 68 | ////////////////////////////////////////////////////////////////////////// |
| 69 | // Iterable |
| 70 | ////////////////////////////////////////////////////////////////////////// |
| 71 | template <> |
| 72 | struct at_impl<ext::boost::tuple_tag> { |
| 73 | template <typename Xs, typename N> |
| 74 | static constexpr decltype(auto) apply(Xs&& xs, N const&) { |
| 75 | constexpr std::size_t n = N::value; |
| 76 | return static_cast<Xs&&>(xs).template get<n>(); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | template <> |
| 81 | struct drop_front_impl<ext::boost::tuple_tag> { |
| 82 | template <std::size_t n, typename Xs, std::size_t ...i> |
| 83 | static constexpr auto drop_front_helper(Xs&& xs, std::index_sequence<i...>) { |
| 84 | return hana::make<ext::boost::tuple_tag>( |
| 85 | hana::at_c<n + i>(static_cast<Xs&&>(xs))... |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | template <typename Xs, typename N> |
| 90 | static constexpr auto apply(Xs&& xs, N const&) { |
| 91 | constexpr std::size_t n = N::value; |
| 92 | constexpr std::size_t len = decltype(hana::length(xs))::value; |
| 93 | return drop_front_helper<n>(static_cast<Xs&&>(xs), |
| 94 | std::make_index_sequence<(n < len ? len - n : 0)>{}); |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | template <> |
| 99 | struct is_empty_impl<ext::boost::tuple_tag> { |
| 100 | static constexpr auto apply(boost::tuples::null_type const&) |
| 101 | { return hana::true_c; } |
| 102 | |
| 103 | template <typename H, typename T> |
| 104 | static constexpr auto apply(boost::tuples::cons<H, T> const&) |
| 105 | { return hana::false_c; } |
| 106 | }; |
| 107 | |
| 108 | ////////////////////////////////////////////////////////////////////////// |
| 109 | // Foldable |
| 110 | ////////////////////////////////////////////////////////////////////////// |
| 111 | template <> |
| 112 | struct length_impl<ext::boost::tuple_tag> { |
| 113 | template <typename Xs> |
| 114 | static constexpr auto apply(Xs const&) { |
| 115 | return hana::size_c<boost::tuples::length<Xs>::value>; |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | ////////////////////////////////////////////////////////////////////////// |
| 120 | // Sequence |
| 121 | ////////////////////////////////////////////////////////////////////////// |
| 122 | template <> |
| 123 | struct Sequence<ext::boost::tuple_tag> { |
| 124 | static constexpr bool value = true; |
| 125 | }; |
| 126 | |
| 127 | template <> |
| 128 | struct make_impl<ext::boost::tuple_tag> { |
| 129 | template <typename ...Xs> |
| 130 | static constexpr auto apply(Xs&& ...xs) { |
| 131 | return boost::tuples::tuple< |
| 132 | typename detail::decay<Xs>::type... |
| 133 | >{static_cast<Xs&&>(xs)...}; |
| 134 | } |
| 135 | }; |
| 136 | }} // end namespace boost::hana |
| 137 | |
| 138 | #endif // !BOOST_HANA_EXT_BOOST_TUPLE_HPP |
| 139 | |