-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCorePrelude.h
More file actions
60 lines (51 loc) · 1.6 KB
/
CorePrelude.h
File metadata and controls
60 lines (51 loc) · 1.6 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
#pragma once
#include <optional>
#include "Kernel/Bag.h"
#include "Kernel/CoreWrapper.h"
#include "Kernel/WitnessSet.h"
#include "Kernel/WitnessWrapper.h"
namespace detail {
template <std::size_t index, class... Ts>
auto try_unpack_typed_args_impl(std::tuple<Ts...> &result, const auto &vec)
-> bool {
if constexpr (index == sizeof...(Ts)) {
return true;
} else {
using T = std::tuple_element_t<index, std::tuple<Ts...>>;
if (auto ptr = std::get_if<T>(&vec[index])) {
std::get<index>(result) = *ptr;
return try_unpack_typed_args_impl<index + 1, Ts...>(result, vec);
} else {
return false;
}
}
}
} // namespace detail
/// Given a vector of variants,
/// try to unpack them into a tuple
/// that is no longer variant (i.e. typed).
///
/// Returns nullopt if the vector is not of the right size,
/// or if any of the variants are not of the right type.
template <class... Ts>
auto try_unpack_typed_args(const auto &vec)
-> std::optional<std::tuple<Ts...>> {
if (vec.size() != sizeof...(Ts)) return std::nullopt;
auto result = std::tuple<Ts...>{};
if (!detail::try_unpack_typed_args_impl<0, Ts...>(result, vec))
return std::nullopt;
return result;
}
/// Given a vector of variants,
/// try to unpack them into a tuple
/// that is no longer variant (i.e. typed).
///
/// Throws if the vector is not of the right size,
/// or if any of the variants are not of the right type.
template <class... Ts>
auto unpack_typed_args(const auto &vec) -> std::tuple<Ts...> {
if (auto result = try_unpack_typed_args<Ts...>(vec))
return *result;
else
throw std::runtime_error("unpack_typed: failed to unpack");
}