Debian Codegraph @

src/containers/internal/storage_type.d

raw
1 /**
2 * Templates for determining data storage types.
3 * Copyright: © 2015 Economic Modeling Specialists, Intl.
4 * Authors: Brian Schott
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
6 */
7 module containers.internal.storage_type;
8
9 /**
10 * This template is used to determine the type that a container should contain
11 * a given type T.
12 *
13 * $(P In many cases it is not possible for a Container!(T) to hold T if T is
14 * $(B const) or $(B immutable). For instance, a binary heap will need to
15 * rearrange items in its internal data storage, or an unrolled list will need
16 * to move items within a node.)
17 *
18 * $(P All containers must only return const references or const copies of data
19 * stored within them if the contained type is const. This template exists
20 * because containers may need to move entire items but not change internal
21 * state of those items and D's type system would otherwise not allow this.)
22 *
23 * $(UL
24 * $(LI If $(B T) is mutable (i.e. not $(B immutable) or $(B const)), this
25 * template aliases itself to T.)
26 * $(LI $(B Class and Interface types:) Rebindable is used. Using this properly
27 * in a container requires that $(LINK2 https://issues.dlang.org/show_bug.cgi?id=8663,
28 * issue 8663) be fixed. Without this fix it is not possible to implement
29 * containers such as a binary heap that must compare container elements
30 * using $(B opCmp()))
31 * $(LI $(B Struct and Union types:)
32 * $(UL
33 * $(LI Stuct and union types that have elaborate constructors,
34 * elaboriate opAssign, or a destructor cannot be stored in containers
35 * because there may be user-visible effects of discarding $(B const)
36 * or $(B immutable) on these struct types.)
37 * $(LI Other struct and union types will be stored as non-const
38 * versions of themselves.)
39 * ))
40 * $(LI $(B Basic types:) Basic types will have their const or immutable status
41 * removed.)
42 * $(LI $(B Pointers:) Pointers will have their type constructors shifted. E.g.
43 * const(int*) becomes const(int)*)
44 * )
45 */
46 template ContainerStorageType(T)
47 {
48 import std.traits : hasElaborateCopyConstructor, hasElaborateDestructor,
49 hasElaborateAssign, isBasicType, isDynamicArray, isPointer, Unqual;
50 import std.typecons : Rebindable;
51 static if (is (T == const) || is (T == immutable))
52 {
53 static if (isBasicType!T || isDynamicArray!T || isPointer!T)
54 alias ContainerStorageType = Unqual!T;
55 else static if (is (T == class) || is (T == interface))
56 alias ContainerStorageType = Rebindable!T;
57 else static if (is (T == struct))
58 {
59 alias U = Unqual!T;
60 static if (hasElaborateAssign!U || hasElaborateCopyConstructor!U || hasElaborateDestructor!U)
61 static assert (false, "Cannot store " ~ T.stringof ~ " because of postblit, opAssign, or ~this");
62 else
63 alias ContainerStorageType = U;
64 }
65 else
66 static assert (false, "Don't know how to handle type " ~ T.stringof);
67 }
68 else
69 alias ContainerStorageType = T;
70 }
71
72 ///
73 unittest
74 {
75 static assert (is (ContainerStorageType!(int) == int));
76 static assert (is (ContainerStorageType!(const int) == int));
77 }
78
79 ///
80 unittest
81 {
82 import std.typecons : Rebindable;
83 static assert (is (ContainerStorageType!(Object) == Object));
84 static assert (is (ContainerStorageType!(const(Object)) == Rebindable!(const(Object))));
85 }
86
87 ///
88 unittest
89 {
90 struct A { int foo; }
91 struct B { void opAssign(typeof(this)) { this.foo *= 2; } int foo;}
92
93 // A can be stored easily because it is plain data
94 static assert (is (ContainerStorageType!(A) == A));
95 static assert (is (ContainerStorageType!(const(A)) == A));
96
97 // const(B) cannot be stored in the container because of its
98 // opAssign. Casting away const could lead to some very unexpected
99 // behavior.
100 static assert (!is (typeof(ContainerStorageType!(const(B)))));
101 // Mutable B is not a problem
102 static assert (is (ContainerStorageType!(B) == B));
103
104 // Arrays can be stored because the entire pointer-length pair is moved as
105 // a unit.
106 static assert (is (ContainerStorageType!(const(int[])) == const(int)[]));
107 }
108
109 ///
110 unittest
111 {
112 static assert (is (ContainerStorageType!(const(int*)) == const(int)*));
113 }