3 Adapts `boost::tuple` for use with Hana.
5 @copyright Louis Dionne 2013-2016
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)
10 #ifndef BOOST_HANA_EXT_BOOST_TUPLE_HPP
11 #define BOOST_HANA_EXT_BOOST_TUPLE_HPP
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>
24 #include <boost/tuple/tuple.hpp>
30 #ifdef BOOST_HANA_DOXYGEN_INVOKED
32 //! @ingroup group-ext-boost
33 //! Adapter for `boost::tuple`s.
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.
43 //! @include example/ext/boost/tuple.cpp
44 template <typename ...T>
50 BOOST_HANA_NAMESPACE_BEGIN
51 namespace ext { namespace boost { struct tuple_tag; }}
53 template <typename ...Xs>
54 struct tag_of<boost::tuple<Xs...>> {
55 using type = ext::boost::tuple_tag;
58 template <typename H, typename T>
59 struct tag_of<boost::tuples::cons<H, T>> {
60 using type = ext::boost::tuple_tag;
64 struct tag_of<boost::tuples::null_type> {
65 using type = ext::boost::tuple_tag;
68 //////////////////////////////////////////////////////////////////////////
70 //////////////////////////////////////////////////////////////////////////
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>();
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))...
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)>{});
99 struct is_empty_impl<ext::boost::tuple_tag> {
100 static constexpr auto apply(boost::tuples::null_type const&)
101 { return hana::true_c; }
103 template <typename H, typename T>
104 static constexpr auto apply(boost::tuples::cons<H, T> const&)
105 { return hana::false_c; }
108 //////////////////////////////////////////////////////////////////////////
110 //////////////////////////////////////////////////////////////////////////
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>;
119 //////////////////////////////////////////////////////////////////////////
121 //////////////////////////////////////////////////////////////////////////
123 struct Sequence<ext::boost::tuple_tag> {
124 static constexpr bool value = true;
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)...};
136 BOOST_HANA_NAMESPACE_END
138 #endif // !BOOST_HANA_EXT_BOOST_TUPLE_HPP