Sample

Undocumented in source.
version(debugMemoryUsage)
alias Sample = WordVariant!(S, T)

Examples

import std.meta : AliasSeq;

alias SubType = WordVariant!(byte*, short*);
alias SuperType = WordVariant!(bool*, byte*, short*, long*);

byte* byteValue = cast(byte*)(0x11);
short* shortValue = cast(short*)(0x22);

SubType sub = byteValue;
assert(sub.typeIndex == 1);
assert(sub.peek!(byte*));
assert(*(sub.peek!(byte*)) == byteValue);

SuperType sup = sub;
assert(sup.typeIndex == 2);
assert(sup.peek!(byte*));
assert(*(sup.peek!(byte*)) == byteValue);

sub = shortValue;
assert(sub.typeIndex == 2);
assert(sub.peek!(short*));
assert(*(sub.peek!(short*)) == shortValue);

sup = sub;
assert(sup.typeIndex == 3);
assert(sup.peek!(short*));
assert(*(sup.peek!(short*)) == shortValue);
1 import std.meta : AliasSeq;
2 
3 alias Types = AliasSeq!(byte*, short*, int*, long*,
4                         ubyte*, ushort*, uint*, ulong*,
5                         float*, double*, real*,
6                         char*, wchar*, dchar*);
7 
8 alias V = WordVariant!Types;
9 V v;
10 
11 try
12 {
13     assert(v.toString == "null");
14 }
15 catch (Exception e) {}
16 
17 assert(v.isNull);
18 v = null;
19 assert(v.isNull);
20 assert(!v);
21 
22 foreach (Tp; Types)
23 {
24     alias T = typeof(*Tp.init);
25 
26     static assert(!__traits(compiles, { T[] a; v = &a; }));
27     static assert(!__traits(compiles, { v.peek!(T[]*); }));
28 
29     // assignment from stack pointer
30     T a = 73;
31     T a_ = 73;
32 
33     v = &a;
34     assert(v);
35     assert(!v.isNull);
36     assert(v.typeIndex != 0);
37     assert(v.ofType!Tp);
38 
39     assert(v == &a);
40     assert(v != &a_);
41     assert(v);
42 
43     foreach (Up; Types)
44     {
45         alias U = typeof(*Up.init);
46         static if (is(T == U))
47         {
48             assert(v.peek!Up);
49             assert(*(v.peek!Up) == &a);
50             assert(v.as!Up == &a);
51         }
52         else
53             assert(!v.peek!Up);
54     }
55 
56     // assignment from heap pointer
57     T* b = new T;
58     T* b_ = new T;
59     *b = 73;
60     *b_ = 73;
61     v = b;
62     assert(v == b);
63     assert(v != b_);
64     assert(v);
65     foreach (Up; Types)
66     {
67         alias U = typeof(*Up.init);
68         static if (is(T == U))
69         {
70             assert(v.peek!Up);
71             assert(*(v.peek!Up) == b);
72         }
73         else
74             assert(!v.peek!Up);
75     }
76 
77 }

Meta