-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnionDriver.cpp
101 lines (92 loc) · 2.23 KB
/
UnionDriver.cpp
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
//===- UnionDriver.cpp ----------------------------------------------===//
//
// Copyright (C) 2024 Eightfold
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------===//
#include <Common/TaggedEnum.hpp>
#include <Common/TaggedUnion.hpp>
#include <Meta/Refl.hpp>
#include <Meta/Traits.hpp>
#include <Meta/Unwrap.hpp>
#pragma push_macro("NDEBUG")
#undef NDEBUG
#include <cassert>
#include <cstdio>
#pragma pop_macro("NDEBUG")
using namespace hc;
namespace C = hc::common;
namespace M = hc::meta;
struct MoveOnly {
MoveOnly() = default;
MoveOnly(const MoveOnly&) = delete;
MoveOnly(MoveOnly&&) = default;
MoveOnly& operator=(const MoveOnly&) = delete;
MoveOnly& operator=(MoveOnly&&) = default;
constexpr ~MoveOnly() {
assert(this->b);
this->b = false;
}
public:
bool b = true;
};
$StrongEnum((WindowsSubsystemType, u16),
(eSubsystemUnknown, 0),
(eSubsystemNative, 1),
(eSubsystemWindowsGUI, 2),
(eSubsystemWindowsCUI, 3),
(eSubsystemPosixCUI, 7),
// ...
(eSubsystemEFIApp, 10),
(eSubsystemEFIBoot, 11),
(eSubsystemEFIRuntime, 12),
(eSubsystemEFIROM, 13)
// ...
);
$Union(Foo,
(X),
(Y, i32),
(Z, f32, f32)
);
$Union(Bar,
(A),
(B, Foo),
(C, MoveOnly, MoveOnly)
);
void foo_disp(Foo& foo) {
$match(foo) {
$arm(X) {
std::printf("X\n");
}
$arm(Y, e) {
std::printf("Y: %i\n", e);
}
$arm(Z, (f1, f2)) {
std::printf("Z: [%f, %f]\n", f1, f2);
}
}
}
int main() {
Bar bar = Bar::A();
{
Foo foo = Foo::X();
foo_disp(foo);
foo = Foo::Y(7);
foo_disp(foo);
foo = Foo::Z(3.0f, 9.0f);
bar = Bar::B($mv(foo));
foo_disp(foo);
}
foo_disp(bar.as_B());
}