WordVariant

A variant of Types packed into a word (size_t).

Suitable for use in tree-data containers, such as radix trees (tries), where hybrid value (sparsely packed sub-tree) and pointer (to dense sub-tree) packing of sub-nodes is needed.

Constructors

this
this(T value)
this(typeof(null) value)

Construction from value.

this
this(WordVariant!(SubTypes) value)

Construction from sub-variant value.

Members

Enums

canStore
eponymoustemplate canStore(T)

Is true iff a T can be stored.

Functions

opAssign
auto ref opAssign(typeof(this) value)
auto ref opAssign(T that)
auto ref opAssign(typeof(null) that)

Assignment from that.

opAssign
auto ref opAssign(WordVariant!(SubTypes) value)

Assignment from sub-variant value.

typeIx
Ix typeIx()

Get zero-offset index as Ix of current variant type.

Examples

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

Meta