forked from user1095108/generic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmany.hpp
189 lines (151 loc) · 3.45 KB
/
many.hpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#ifndef GNR_MANY_HPP
# define GNR_MANY_HPP
# pragma once
#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>
namespace gnr
{
namespace detail
{
namespace many
{
enum TupleValue { Scalar, Reference, Class };
template<std::size_t I,
class T,
TupleValue = std::is_class<T>{} ?
TupleValue::Class :
std::is_reference<T>{} ?
TupleValue::Reference :
TupleValue::Scalar
>
struct tuple_base
{
T value;
tuple_base() = default;
template <typename A>
constexpr tuple_base(A&& value) : value{std::forward<A>(value)}
{
}
};
template<std::size_t I, class T>
struct tuple_base<I, T, TupleValue::Reference>
{
std::add_pointer_t<std::remove_reference_t<T>> value;
tuple_base() = delete;
constexpr tuple_base(T value) : value{&value}
{
}
tuple_base(tuple_base&&) = default;
tuple_base(tuple_base const&) = default;
tuple_base& operator=(tuple_base&&) = default;
tuple_base& operator=(tuple_base const&) = default;
};
template<std::size_t I, class T>
struct tuple_base<I, T, TupleValue::Class> : T
{
tuple_base() = default;
template <typename A>
constexpr tuple_base(A&& value) : T{std::forward<A>(value)}
{
}
};
template <std::size_t I, class T>
auto& get(tuple_base<I, T, TupleValue::Scalar>& obj)
{
return obj.value;
}
template <std::size_t I, class T>
auto& get(tuple_base<I, T, TupleValue::Scalar> const& obj)
{
return obj.value;
}
template <std::size_t I, class T>
auto& get(tuple_base<I, T, TupleValue::Reference> const& obj)
{
return *obj.value;
}
template <std::size_t I, class T>
auto get(tuple_base<I, T, TupleValue::Class>& obj) -> T&
{
return obj;
}
template <std::size_t I, class T>
auto get(tuple_base<I, T, TupleValue::Class> const& obj) -> T const&
{
return obj;
}
template <typename ...>
struct many_impl;
template <std::size_t... Is, typename ...Types>
struct many_impl<std::index_sequence<Is...>, Types...> :
tuple_base<Is, Types>...
{
many_impl() = default;
~many_impl() = default;
many_impl(many_impl&&) = default;
many_impl(many_impl const&) = default;
many_impl& operator=(many_impl&&) = default;
many_impl& operator=(many_impl const&) = default;
#if !__cpp_aggregate_bases
template<typename ...U>
constexpr many_impl(U&& ...u) :
tuple_base<Is, Types>{std::forward<U>(u)}...
{
}
#endif
};
}
}
template <typename ...Types>
struct many : detail::many::many_impl<
std::make_index_sequence<sizeof...(Types)>,
Types...
>
{
many() = default;
~many() = default;
many(many&&) = default;
many(many const&) = default;
many& operator=(many&&) = default;
many& operator=(many const&) = default;
#if !__cpp_aggregate_bases
template<class... U>
constexpr many(U&& ...u) : many::many_impl{std::forward<U>(u)...}
{
}
#endif
};
}
namespace std
{
template <typename ...Types>
class tuple_size<gnr::many<Types...>> :
public std::integral_constant<size_t, sizeof...(Types)>
{
};
template <size_t I, class ...Types>
class tuple_element<I, gnr::many<Types...>>
{
public:
using type = tuple_element_t<I, tuple<Types...>>;
};
template <size_t I, typename ...Types>
auto& get(gnr::many<Types...>& m) noexcept
{
return gnr::detail::many::get<I>(m);
}
template <size_t I, typename ...Types>
auto& get(gnr::many<Types...> const& m) noexcept
{
return gnr::detail::many::get<I>(m);
}
template<size_t I, typename ...Types>
auto& get(gnr::many<Types...>&& m) noexcept
{
// m is now a lvalue
return gnr::detail::many::get<I>(m);
}
}
#endif // GNR_MANY_HPP