-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagged_union.d
178 lines (154 loc) · 5.04 KB
/
tagged_union.d
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
173
174
175
176
177
178
// Copyright (C) 2021 by tspike (github.com/tspike2k)
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/+
DESCRIPTION:
A simple implementation of a tagged union in D. The types the tagged union can hold are determined by template arguments.
The "set" method is used to store a value in the tagged union so long as the union has been configured to support data
of that type. Once the "set" method is called, the union is tagged as holding data of the supplied type. The "get" method
is used to retrieve data from the union. If the union is not currently tagged as holding data of the type requested by the
"get" method, an assertion is fired. The isType method is used to test which data type the union is tagged as storing.
Immutable instances are also supported!
+/
nothrow @nogc:
import std.traits : Unqual;
import std.meta: NoDuplicates;
uint indexOfType(T, Types...)()
if(Types.length > 0)
{
uint result = uint.max;
static foreach(i; 0 .. Types.length)
{
if(is(Types[i] == T))
{
result = i;
}
}
return result;
}
struct TaggedUnion(Types...)
if(Types.length > 0 && is(NoDuplicates!Types == Types))
{
union Members
{
static foreach(i; 0 .. Types.length)
{
mixin(Types[i].stringof ~ " m_" ~ i.stringof ~ ";");
}
}
alias types = Types;
uint typeIndex = uint.max;
private Members members;
nothrow @nogc:
static foreach(type; Types)
{
this(type t)
{
set(t);
}
}
inout(T) get(T)() inout
{
static assert(indexOfType!(T, Types) != uint.max, "Type " ~ T.stringof ~ " is not a valid type for " ~ typeof(this).stringof);
foreach(ref m; members.tupleof)
{
static if(is(Unqual!(typeof(m)) == T))
{
assert(isType!T, Unqual!(typeof(this)).stringof ~ " is not currently tagged as a " ~ T.stringof ~ ".");
return m;
}
}
}
void set(T)(in T t)
{
enum index = indexOfType!(T, Types);
static assert(index != uint.max, "Type " ~ T.stringof ~ " is not a valid type for " ~ typeof(this).stringof);
typeIndex = index;
foreach(ref m; members.tupleof)
{
static if(is(typeof(m) == T))
{
m = t;
}
}
}
bool isType(T)() const
{
enum index = indexOfType!(T, Types);
return typeIndex == index;
}
}
extern(C) int main()
{
import core.stdc.stdio;
TaggedUnion!(int, string) test;
test.set("Abba");
printf("%s\n", test.get!(string).ptr);
assert(!test.isType!int);
assert(test.isType!string);
assert(!test.isType!ulong);
test.set(14);
assert(test.isType!int);
assert(!test.isType!string);
assert(!test.isType!ulong);
// NOTE: Any of the following three lines will cause an error, as desired:
//auto s = test.get!string;
//test.set('c');
//auto a = test.get!char;
static foreach(type; test.types)
{
if(test.isType!type)
{
auto val = test.get!type;
static if(is(type == string))
{
printf("%s\n", val.ptr);
}
else static if(is(type == int))
{
printf("%d\n", val);
}
}
}
alias TU_Test = TaggedUnion!(int, string);
immutable TU_Test const_test = TU_Test("Test string");
//immutable TU_Test const_test = TU_Test(192);
static foreach(type; const_test.types)
{
if(const_test.isType!type)
{
auto val = const_test.get!type;
static if(is(type == string))
{
printf("%s\n", val.ptr);
}
else static if(is(type == int))
{
printf("%d\n", val);
}
}
}
// NOTE: More verbose, but more efficient means of processing members
outer: switch(const_test.typeIndex)
{
static foreach(i, type; const_test.types)
{
case i:
{
auto val = const_test.get!type;
static if(is(type == string))
{
printf("%s\n", val.ptr);
}
else static if(is(type == int))
{
printf("%d\n", val);
}
} break outer;
}
default: assert(0); break;
}
return 0;
}