-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.hpp
97 lines (80 loc) · 3.23 KB
/
utils.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
#pragma once
#include "boost/pfr/precise.hpp"
#include "mutils/type_utils.hpp"
#include <type_traits>
#define CONSTVARY(name, body...) name body name const body
#define CONSTVARY2(name, name2, body...) name, name2 body name, name2 const body
namespace compile_time {
template <typename F> struct wrap_invocation {
static const constexpr DECT(F{}()) value{F{}()};
constexpr wrap_invocation() = default;
constexpr auto &operator()() const { return value; }
};
#define struct_wrap(name, invocation...) \
struct __##name { \
constexpr __##name() = default; \
constexpr decltype(auto) operator()() const { return invocation; } \
}; \
using name = wrap_invocation<__##name>;
template <typename T, typename fst, typename... rst>
constexpr std::size_t index_of_f() {
if constexpr (std::is_same_v<T, fst>)
return 0;
else
return 1 + index_of_f<T, rst...>();
}
struct do_not_call_this {};
template <std::size_t i, typename fst, typename... rst>
fst type_at_f(std::enable_if_t<i == 0> * = nullptr) {
throw do_not_call_this{};
}
template <std::size_t i, typename fst, typename... rst>
auto type_at_f(std::enable_if_t<(i > 0)> * = nullptr)
-> DECT(type_at_f<i + 1, rst...>()) {
throw do_not_call_this{};
}
template <typename T, typename... in>
inline constexpr std::size_t index_of = index_of_f<T, in...>();
template <std::size_t i, typename... in>
using type_at = DECT(type_at_f<i, in...>());
template <typename T>
inline constexpr std::size_t struct_size = boost::pfr::tuple_size<T>::value;
struct error_t {
char msg[1024] = {0};
constexpr error_t() = default;
};
template <typename T> struct maybe_error {
error_t error{};
bool error_set{false};
T value{};
constexpr maybe_error() = default;
constexpr maybe_error &operator=(const T &t) {
error_set = false;
value = t;
return *this;
}
constexpr maybe_error &operator=(const error_t &e) {
error_set = true;
error = e;
return *this;
}
};
} // namespace compile_time
namespace mutils {
template <class T, class Enable = void> struct is_defined : std::false_type {};
template <class T>
struct is_defined<T, std::enable_if_t<(sizeof(T) > 0)>> : std::true_type {};
template <class T> inline constexpr bool is_defined_v = is_defined<T>::value;
template <std::size_t N>
using num_as_ptr = std::integral_constant<std::size_t, N> *;
} // namespace mutils
#define MATCH2(s, e) s).match([&](auto &e) constexpr
#define MATCH3(s, a, e) s).match([&](auto &a, auto &e) constexpr
#define MATCH4(s, a, b, e) s).match([&](auto &a, auto &b, auto &e) constexpr
#define MATCH5(s, a, b, c, e) s).match([&](auto &a, auto &b, auto& c, auto &e) constexpr
#define MATCH6(s, a, b, c, d, e) s).match([&](auto &a, auto &b, auto& c, auto& d, auto &e) constexpr
#define MATCH_IMPL2(count, ...) MATCH##count(__VA_ARGS__)
#define MATCH_IMPL(count, ...) MATCH_IMPL2(count, __VA_ARGS__)
#define MATCH(...) MATCH_IMPL(VA_NARGS(__VA_ARGS__), __VA_ARGS__)
#define ASSIGN_SINGLE(s, f, expr...) \
s.match([&](auto &f) constexpr { f = expr; })