| 1 |
/** |
| 2 |
* Templates for determining range return 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 |
|
| 8 |
module containers.internal.element_type; |
| 9 |
|
| 10 |
/** |
| 11 |
* Figures out the types that should be shown to users of the containers when |
| 12 |
* functions such as front and opIndex are called. |
| 13 |
* |
| 14 |
* Params: |
| 15 |
* ContainerType = The container type, usually taken from a `this This` |
| 16 |
* template argument |
| 17 |
* ElementType = The type of the elements of the container. |
| 18 |
* isRef = If the type is being determined for a function that returns `ref` |
| 19 |
*/ |
| 20 |
template ContainerElementType(ContainerType, ElementType, bool isRef = false) |
| 21 |
{ |
| 22 |
import std.traits : isMutable, hasIndirections, PointerTarget, isPointer, Unqual; |
| 23 |
|
| 24 |
template ET(bool isConst, T) |
| 25 |
{ |
| 26 |
static if (isPointer!ElementType) |
| 27 |
{ |
| 28 |
enum PointerIsConst = is(ElementType == const); |
| 29 |
enum PointerIsImmutable = is(ElementType == immutable); |
| 30 |
enum DataIsConst = is(PointerTarget!(ElementType) == const); |
| 31 |
enum DataIsImmutable = is(PointerTarget!(ElementType) == immutable); |
| 32 |
static if (isConst) |
| 33 |
{ |
| 34 |
static if (PointerIsConst) |
| 35 |
alias ET = ElementType; |
| 36 |
else static if (PointerIsImmutable) |
| 37 |
alias ET = ElementType; |
| 38 |
else |
| 39 |
alias ET = const(PointerTarget!(ElementType))*; |
| 40 |
} |
| 41 |
else |
| 42 |
{ |
| 43 |
static assert(DataIsImmutable, "An immutable container cannot reference const or mutable data"); |
| 44 |
static if (PointerIsConst) |
| 45 |
alias ET = immutable(PointerTarget!ElementType)*; |
| 46 |
else |
| 47 |
alias ET = ElementType; |
| 48 |
} |
| 49 |
} |
| 50 |
else |
| 51 |
{ |
| 52 |
static if (isConst) |
| 53 |
{ |
| 54 |
static if (is(ElementType == immutable)) |
| 55 |
alias ET = ElementType; |
| 56 |
else |
| 57 |
alias ET = const(Unqual!ElementType); |
| 58 |
} |
| 59 |
else |
| 60 |
alias ET = immutable(Unqual!ElementType); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
static if (isMutable!ContainerType) |
| 65 |
{ |
| 66 |
alias ContainerElementType = ElementType; |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
static if (isRef || hasIndirections!ElementType) |
| 71 |
alias ContainerElementType = ET!(is(ContainerType == const), ElementType); |
| 72 |
else |
| 73 |
alias ContainerElementType = ElementType; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
unittest |
| 78 |
{ |
| 79 |
static struct Container {} |
| 80 |
static struct Data1 { int* x; } |
| 81 |
static class Data2 { int* x; } |
| 82 |
|
| 83 |
static assert(is(ContainerElementType!(Container, int) == int)); |
| 84 |
static assert(is(ContainerElementType!(Container, const int) == const int)); |
| 85 |
static assert(is(ContainerElementType!(Container, immutable int) == immutable int)); |
| 86 |
static assert(is(ContainerElementType!(const Container, int) == int)); |
| 87 |
static assert(is(ContainerElementType!(const Container, const int) == const int)); |
| 88 |
static assert(is(ContainerElementType!(const Container, immutable int) == immutable int)); |
| 89 |
static assert(is(ContainerElementType!(immutable Container, int) == int)); |
| 90 |
static assert(is(ContainerElementType!(immutable Container, const int) == const int)); |
| 91 |
static assert(is(ContainerElementType!(immutable Container, immutable int) == immutable int)); |
| 92 |
|
| 93 |
static assert(is(ContainerElementType!(Container, Data1) == Data1)); |
| 94 |
static assert(is(ContainerElementType!(Container, const Data1) == const Data1)); |
| 95 |
static assert(is(ContainerElementType!(Container, immutable Data1) == immutable Data1)); |
| 96 |
static assert(is(ContainerElementType!(const Container, Data1) == const Data1)); |
| 97 |
static assert(is(ContainerElementType!(const Container, const Data1) == const Data1)); |
| 98 |
static assert(is(ContainerElementType!(const Container, immutable Data1) == immutable Data1)); |
| 99 |
static assert(is(ContainerElementType!(immutable Container, Data1) == immutable Data1)); |
| 100 |
static assert(is(ContainerElementType!(immutable Container, const Data1) == immutable Data1)); |
| 101 |
static assert(is(ContainerElementType!(immutable Container, immutable Data1) == immutable Data1)); |
| 102 |
|
| 103 |
static assert(is(ContainerElementType!(Container, Data2) == Data2)); |
| 104 |
static assert(is(ContainerElementType!(Container, const Data2) == const Data2)); |
| 105 |
static assert(is(ContainerElementType!(Container, immutable Data2) == immutable Data2)); |
| 106 |
static assert(is(ContainerElementType!(const Container, Data2) == const Data2)); |
| 107 |
static assert(is(ContainerElementType!(const Container, const Data2) == const Data2)); |
| 108 |
static assert(is(ContainerElementType!(const Container, immutable Data2) == immutable Data2)); |
| 109 |
static assert(is(ContainerElementType!(immutable Container, Data2) == immutable Data2)); |
| 110 |
static assert(is(ContainerElementType!(immutable Container, const Data2) == immutable Data2)); |
| 111 |
static assert(is(ContainerElementType!(immutable Container, immutable Data2) == immutable Data2)); |
| 112 |
|
| 113 |
static assert(is(ContainerElementType!(Container, int*) == int*)); |
| 114 |
static assert(is(ContainerElementType!(Container, const int*) == const int*)); |
| 115 |
static assert(is(ContainerElementType!(Container, const(int)*) == const(int)*)); |
| 116 |
static assert(is(ContainerElementType!(Container, immutable int*) == immutable int*)); |
| 117 |
static assert(is(ContainerElementType!(Container, immutable(int)*) == immutable(int)*)); |
| 118 |
|
| 119 |
static assert(is(ContainerElementType!(Container, Data1*) == Data1*)); |
| 120 |
static assert(is(ContainerElementType!(Container, const Data1*) == const Data1*)); |
| 121 |
static assert(is(ContainerElementType!(Container, const(Data1)*) == const(Data1)*)); |
| 122 |
static assert(is(ContainerElementType!(Container, immutable Data1*) == immutable Data1*)); |
| 123 |
static assert(is(ContainerElementType!(Container, immutable(Data1)*) == immutable(Data1)*)); |
| 124 |
|
| 125 |
static assert(is(ContainerElementType!(const Container, int*) == const(int)*)); |
| 126 |
static assert(is(ContainerElementType!(const Container, const int*) == const int*)); |
| 127 |
static assert(is(ContainerElementType!(const Container, const(int)*) == const(int)*)); |
| 128 |
static assert(is(ContainerElementType!(const Container, immutable int*) == immutable int*)); |
| 129 |
static assert(is(ContainerElementType!(const Container, immutable(int)*) == immutable(int)*)); |
| 130 |
|
| 131 |
static assert(!__traits(compiles, ContainerElementType!(immutable Container, int*))); |
| 132 |
static assert(!__traits(compiles, ContainerElementType!(immutable Container, const int*))); |
| 133 |
|
| 134 |
static assert(is(ContainerElementType!(immutable Container, immutable int*) == immutable int*)); |
| 135 |
static assert(is(ContainerElementType!(immutable Container, immutable(int)*) == immutable(int)*)); |
| 136 |
} |