-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionSet.h
More file actions
53 lines (45 loc) · 1.03 KB
/
OptionSet.h
File metadata and controls
53 lines (45 loc) · 1.03 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
#pragma once
#include <tuple>
// workaround until we have constexpr function arguments
#define cex(x) []{ return x; }
template<typename T, T val>
struct Tag
{
using type = T;
T value = val;
};
template<typename L>
constexpr auto makeTag(L l) {
return Tag<decltype(l()), l()>{};
}
template<typename T, int Idx, typename Tuple>
decltype(auto) getFirst(Tuple&& t)
{
using Target = std::decay_t<decltype(std::get<Idx>(t))>;
if constexpr (std::is_same<T, typename Target::type>()) {
return std::get<Idx>(t);
}
else {
return getFirst<T, Idx+1>(std::forward<Tuple>(t));
}
}
template<typename... T>
struct OptionSet
{
template<typename U>
auto get() {
return getFirst<U, 0>(options_).value;
}
template<typename U>
OptionSet& set(U val) {
getFirst<U, 0>(options_).value = val;
return *this;
}
private:
std::tuple<T...> options_;
};
template<typename... T>
constexpr auto MakeOptionSet(T... vals)
{
return OptionSet<decltype(makeTag(vals))...>{};
}