-
Notifications
You must be signed in to change notification settings - Fork 0
/
disc_union.cpp
172 lines (129 loc) · 3.06 KB
/
disc_union.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
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
#if defined(DISC_UNION_TEST_MAIN)
#include <stdio.h>
#include <stdlib.h>
#include "disc_union.h"
#include "vector.h"
#include "strings.h"
struct MyClass{
char name[16];
int i;
float f;
Vector<int> it;
};
struct MyOtherClass{
void* data;
int g;
float h;
short header[6];
};
// Define a macro that takes a macro
#define DISC_LIST(mac) \
mac(MyClass) \
mac(MyOtherClass) \
mac(String)
// This generates the actual struct body
DEFINE_DISCRIMINATED_UNION(MyUnion, DISC_LIST)
// We don't need to undef it, but it's a good idea
#undef DISC_LIST
void DoSomething(const MyUnion& mu) {
ASSERT(mu.IsString());
}
struct IntBox{
int val;
};
struct CharBox{
char val;
};
#define DISC_LIST(mac) \
mac(IntBox) \
mac(CharBox) \
DEFINE_DISCRIMINATED_UNION(BasicUnion1, DISC_LIST)
#undef DISC_LIST
static_assert(BNS_ALIGNOF(BasicUnion1) == 4, "Alignment of DISC_UNION (assumes int is 32-bit aligned");
#define DISC_LIST(mac) \
mac(String) \
mac(IntBox) \
mac(SubString)
DEFINE_DISCRIMINATED_UNION(BasicUnion2, DISC_LIST)
#undef DISC_LIST
static_assert(BNS_ALIGNOF(BasicUnion2) == sizeof(int*), "Alignment of DISC_UNION (assumes pointers are word-aligned");
CREATE_TEST_CASE("Disc union basic usage"){
MyClass mc;
mc.it.PushBack(1);
MyClass mc2;
mc2.it.PushBack(2);
MyOtherClass moc;
MyUnion un;
ASSERT(un.type == MyUnion::UE_None);
ASSERT(!un.IsMyClass());
ASSERT(!un.IsMyOtherClass());
ASSERT(!un.IsString());
un = mc;
ASSERT(un.type == MyUnion::UE_MyClass);
ASSERT(un.IsMyClass());
ASSERT(!un.IsMyOtherClass());
ASSERT(!un.IsString());
un = moc;
ASSERT(un.type == MyUnion::UE_MyOtherClass);
ASSERT(!un.IsMyClass());
ASSERT(un.IsMyOtherClass());
ASSERT(!un.IsString());
un = mc;
ASSERT(un.type == MyUnion::UE_MyClass);
ASSERT(un.IsMyClass());
ASSERT(!un.IsMyOtherClass());
ASSERT(!un.IsString());
un = mc2;
ASSERT(un.type == MyUnion::UE_MyClass);
un = mc;
ASSERT(un.type == MyUnion::UE_MyClass);
String str1 = "LALA";
ASSERT(str1.GetRef() == 1);
un = str1;
ASSERT(un.type == MyUnion::UE_String);
ASSERT(un.AsString() == str1);
ASSERT(str1.GetRef() == 2);
un = mc2;
ASSERT(un.type == MyUnion::UE_MyClass);
ASSERT(str1.GetRef() == 1);
un = str1;
ASSERT(str1.GetRef() == 2);
{
MyUnion un2 = un;
ASSERT(un2.type == MyUnion::UE_String);
ASSERT(str1.GetRef() == 3);
MyUnion un3;
un3 = un2;
ASSERT(un3.type == MyUnion::UE_String);
ASSERT(str1.GetRef() == 4);
}
ASSERT(str1.GetRef() == 2);
{
MyUnion un2 = str1;
ASSERT(str1.GetRef() == 3);
DoSomething(str1);
ASSERT(str1.GetRef() == 3);
ASSERT(un2.IsString());
const MyUnion& un2_ref = un2;
ASSERT(un2_ref.IsString());
ASSERT(un2_ref.AsString().GetRef() == 3);
un2 = MyUnion();
ASSERT(!un2_ref.IsString());
ASSERT(str1.GetRef() == 2);
un2 = str1;
ASSERT(str1.GetRef() == 3);
}
ASSERT(str1.GetRef() == 2);
{
MyUnion un2 = str1;
ASSERT(str1.GetRef() == 3);
if (auto* str = un2.MaybeAsString()) {
ASSERT(str->string == str1.string);
} else {
ASSERT(false);
}
}
ASSERT(str1.GetRef() == 2);
return 0;
}
#endif