| 1 | |
| 2 | // Copyright Oliver Kowalke 2014. |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | // http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #ifndef BOOST_CONTEXT_DETAIL_TUPLE_H |
| 8 | #define BOOST_CONTEXT_DETAIL_TUPLE_H |
| 9 | |
| 10 | #include <tuple> |
| 11 | #include <utility> |
| 12 | |
| 13 | #include <boost/config.hpp> |
| 14 | |
| 15 | #include <boost/context/detail/config.hpp> |
| 16 | #include <boost/context/detail/index_sequence.hpp> |
| 17 | |
| 18 | #ifdef BOOST_HAS_ABI_HEADERS |
| 19 | # include BOOST_ABI_PREFIX |
| 20 | #endif |
| 21 | |
| 22 | namespace boost { |
| 23 | namespace context { |
| 24 | namespace detail { |
| 25 | |
| 26 | template< typename ... S, typename ... T, std::size_t ... I > |
| 27 | void |
| 28 | head_impl( std::tuple< S ... > & s, |
| 29 | std::tuple< T ... > & t, index_sequence< I ... >) { |
| 30 | t = std::tuple< T ... >{ std::get< I >( s) ... }; |
| 31 | } |
| 32 | |
| 33 | template< typename ... S, typename ... T, std::size_t ... I > |
| 34 | void |
| 35 | head_impl( std::tuple< S ... > && s, |
| 36 | std::tuple< T ... > & t, index_sequence< I ... >) { |
| 37 | t = std::tuple< T ... >{ std::get< I >( std::move( s) ) ... }; |
| 38 | } |
| 39 | |
| 40 | template< typename ... S, std::size_t ... I1, typename ... T, std::size_t ... I2 > |
| 41 | void |
| 42 | tail_impl( std::tuple< S ... > & s, index_sequence< I1 ... >, |
| 43 | std::tuple< T ... > & t, index_sequence< I2 ... >) { |
| 44 | constexpr std::size_t Idx = (sizeof...(I1)) - (sizeof...(I2)); |
| 45 | t = std::tuple< T ... >{ std::get< (Idx + I2) >( s) ... }; |
| 46 | } |
| 47 | |
| 48 | template< typename ... S, std::size_t ... I1, typename ... T, std::size_t ... I2 > |
| 49 | void |
| 50 | tail_impl( std::tuple< S ... > && s, index_sequence< I1 ... >, |
| 51 | std::tuple< T ... > & t, index_sequence< I2 ... >) { |
| 52 | constexpr std::size_t Idx = (sizeof...(I1)) - (sizeof...(I2)); |
| 53 | t = std::tuple< T ... >{ std::get< (Idx + I2) >( std::move( s) ) ... }; |
| 54 | } |
| 55 | |
| 56 | template< typename ... T > |
| 57 | class tuple_head; |
| 58 | |
| 59 | template< typename ... T > |
| 60 | class tuple_head< std::tuple< T ... > > { |
| 61 | private: |
| 62 | std::tuple< T ... > & t_; |
| 63 | |
| 64 | public: |
| 65 | tuple_head( std::tuple< T ... > & t) noexcept : |
| 66 | t_( t) { |
| 67 | } |
| 68 | |
| 69 | template< typename ... S > |
| 70 | void operator=( std::tuple< S ... > & s) { |
| 71 | static_assert((sizeof...(T)) <= (sizeof...(S)), "invalid tuple size" ); |
| 72 | head_impl( s, |
| 73 | t_, index_sequence_for< T ... >{} ); |
| 74 | } |
| 75 | template< typename ... S > |
| 76 | void operator=( std::tuple< S ... > && s) { |
| 77 | static_assert((sizeof...(T)) <= (sizeof...(S)), "invalid tuple size" ); |
| 78 | head_impl( std::move( s), |
| 79 | t_, index_sequence_for< T ... >{} ); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | template< typename ... T > |
| 84 | class tuple_tail; |
| 85 | |
| 86 | template< typename ... T > |
| 87 | class tuple_tail< std::tuple< T ... > > { |
| 88 | private: |
| 89 | std::tuple< T ... > & t_; |
| 90 | |
| 91 | public: |
| 92 | tuple_tail( std::tuple< T ... > & t) noexcept : |
| 93 | t_( t) { |
| 94 | } |
| 95 | |
| 96 | template< typename ... S > |
| 97 | void operator=( std::tuple< S ... > & s) { |
| 98 | static_assert((sizeof...(T)) <= (sizeof...(S)), "invalid tuple size" ); |
| 99 | tail_impl( s, index_sequence_for< S ... >{}, |
| 100 | t_, index_sequence_for< T ... >{} ); |
| 101 | } |
| 102 | |
| 103 | template< typename ... S > |
| 104 | void operator=( std::tuple< S ... > && s) { |
| 105 | static_assert((sizeof...(T)) <= (sizeof...(S)), "invalid tuple size" ); |
| 106 | tail_impl( std::move( s), index_sequence_for< S ... >{}, |
| 107 | t_, index_sequence_for< T ... >{} ); |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | template< typename ... T > |
| 112 | detail::tuple_head< std::tuple< T ... > > |
| 113 | ( std::tuple< T ... > & tpl) { |
| 114 | return tuple_head< std::tuple< T ... > >{ tpl }; |
| 115 | } |
| 116 | |
| 117 | template< typename ... T > |
| 118 | detail::tuple_tail< std::tuple< T ... > > |
| 119 | tail( std::tuple< T ... > & tpl) { |
| 120 | return tuple_tail< std::tuple< T ... > >{ tpl }; |
| 121 | } |
| 122 | |
| 123 | }}} |
| 124 | |
| 125 | #ifdef BOOST_HAS_ABI_HEADERS |
| 126 | #include BOOST_ABI_SUFFIX |
| 127 | #endif |
| 128 | |
| 129 | #endif // BOOST_CONTEXT_DETAIL_TUPLE_H |
| 130 | |