-
Notifications
You must be signed in to change notification settings - Fork 0
/
packed.hpp
212 lines (168 loc) · 6.48 KB
/
packed.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Instead of tag types we could use BOOST_STRONG_TYPEDEF. This would
// give us an interface that looks a lot more like std::variant and
// std::tuple.
// #include <boost/serialization/strong_typedef.hpp>
#include <climits>
#include <cstdint>
#include <type_traits>
namespace packed
{
namespace detail
{
template<uint64_t N>
struct of_size {
using type = std::conditional_t<N - 1 <= UINT8_MAX, std::uint8_t,
std::conditional_t<N - 1 <= UINT16_MAX, std::uint16_t,
std::conditional_t<N - 1 <= UINT32_MAX, std::uint32_t,
std::conditional_t<N - 1 <= UINT64_MAX, std::uint64_t,
void>>>>;
};
template<uint64_t N>
using of_size_t = typename of_size<N>::type;
template<typename K, typename... _Ts>
struct lookup;
template<typename K, typename... _Ts>
using lookup_t = typename lookup<K, _Ts...>::type;
template<typename K, typename... _Ts>
constexpr auto lookup_v = lookup<K, _Ts...>::value;
// FIXME: Use is_same_v on a C++17 stdlib.
// TODO: Use enable_if to factor out is_same (and provide a nice error
// message via static_assert).
template<typename K, typename N, typename T>
struct lookup<K, N, T> {
using type = std::conditional_t<std::is_same<K, N>::value, T, void>;
static constexpr auto value = std::conditional_t<std::is_same<K, N>::value,
std::integral_constant<int, 0>,
// TODO: Compile error if not found.
std::integral_constant<int, -1>>::value;
// HINT: C++17 introduces typename F, previously it had to be class F.
template<template<typename...> typename F>
using with_tail = F<>;
};
template<typename K, typename N, typename T, typename... _Ts>
struct lookup<K, N, T, _Ts...> {
using type = std::conditional_t<std::is_same<K, N>::value,
T,
lookup_t<K, _Ts...>>;
static constexpr auto value = std::is_same<K, N>::value
? 0
: lookup_v<K, _Ts...> != -1
? 1 + lookup_v<K, _Ts...>
: -1;
template<template<typename...> typename F>
using with_tail = std::conditional_t<std::is_same<K, N>::value,
F<_Ts...>,
typename lookup<K, _Ts...>::template with_tail<F>>;
};
// FIXME: This shouldn't be necessary?
template<template<typename...> typename F, typename K, typename... _Ts>
using _with_tail_v = typename lookup<K, _Ts...>::template with_tail<F>;
template<template<typename...> typename F, typename K, typename... _Ts>
constexpr auto lookup_with_tail_v = _with_tail_v<F, K, _Ts...>::value;
template<typename... _Ts>
struct sum_size;
template<typename... _Ts>
constexpr auto sum_size_v = sum_size<_Ts...>::value;
template<>
struct sum_size<> {
static constexpr auto value = 0;
};
template<typename _, typename T, typename... _Ts>
struct sum_size<_, T, _Ts...> {
static constexpr auto value = T::values + sum_size_v<_Ts...>;
};
template<typename... _Ts>
struct product_size;
template<typename... _Ts>
constexpr auto product_size_v = product_size<_Ts...>::value;
template<>
struct product_size<> {
static constexpr auto value = 0;
};
template<typename _, typename T>
struct product_size<_, T> {
static constexpr auto value = T::values;
};
template<typename _, typename T, typename... _Ts>
struct product_size<_, T, _Ts...> {
static constexpr auto value = T::values * product_size_v<_Ts...>;
};
}
// TODO: Surely int64_t is more appropriate?
template<uint64_t I, uint64_t J=0>
struct range {
// HINT: Swap I and J to have range<N> == range<0, N>.
static constexpr auto values = (I < J ? J - I : I - J);
using underlying_type = detail::of_size_t<values>;
constexpr range(uint64_t v) : value(v) {}
constexpr operator underlying_type() const noexcept { return value; }
underlying_type value;
};
// TODO: Specialize for the common case of Vs being a contiguous range.
template<typename T, T... Vs>
class enum_ {
public:
static constexpr auto values = sizeof...(Vs);
using underlying_type = detail::of_size_t<values>;
constexpr enum_(T v) : index(indexof(v)) {}
constexpr operator T() const noexcept { return (T[]){Vs...}[index]; }
constexpr operator underlying_type() const noexcept { return index; }
// HINT: Used for tests.
//private:
underlying_type index;
private:
constexpr auto indexof(T v) {
constexpr T vs[] = { Vs... };
for (underlying_type i = 0; i < values; ++i) {
if (vs[i] == v) return i;
}
// TODO: Throw an exception in non-constexpr contexts.
}
};
template<typename... NTs>
class variant {
public:
static constexpr auto values = detail::sum_size_v<NTs...>;
using underlying_type = detail::of_size_t<values>;
template<typename K>
static constexpr variant make(detail::lookup_t<K, NTs...> t) {
// TODO: Use brace initialization (must silence warning about narrowing).
return detail::lookup_with_tail_v<detail::sum_size, K, NTs...> + underlying_type(t);
}
// HINT: Used for tests.
//private:
underlying_type value;
private:
constexpr variant(underlying_type v) : value(v) {}
};
template<typename N, typename T, typename... NTs>
class struct_ {
template<typename, typename, typename...>
friend class struct_;
public:
static constexpr auto values = detail::product_size_v<N, T, NTs...>;
using underlying_type = detail::of_size_t<values>;
template<typename... Ts>
static constexpr struct_ make(T t, Ts... ts) {
return _make(0, t, ts...);
}
// HINT: Used for tests.
//private:
underlying_type value;
private:
constexpr struct_(underlying_type v) : value(v) {}
template<typename... Ts>
static constexpr underlying_type _make(underlying_type a, T t, Ts... ts) {
return struct_<NTs...>::_make(a * T::values + underlying_type(t), ts...);
}
static constexpr underlying_type _make(underlying_type a, T t) {
static_assert(sizeof...(NTs) == 0);
return a * T::values + underlying_type(t);
}
};
// TODO: std::array-like.
// TODO: Support (ab)using spare bits in pointers with struct_<T*, U>?
// alignof tells us how many bits are required. Note this requires
// us to transform compacted values (i.e. shift), but that would
// be needed for correct enum support anyway.
}