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