1 /*******************************************************************************
2 3 Utilities to manipulate array types
4 5 NB: because this module is often used as purely compile-time dependency it
6 used built-in asserts instead of `ocean.core.Test` to reduce amount of
7 cyclic imports. `ocean.meta` modules in general are not supposed to
8 import anything outside of `ocean.meta`.
9 10 Copyright:
11 Copyright (c) 2017 dunnhumby Germany GmbH.
12 All rights reserved.
13 14 License:
15 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
16 Alternatively, this file may be distributed under the terms of the Tango
17 3-Clause BSD License (see LICENSE_BSD.txt for details).
18 19 *******************************************************************************/20 21 moduleocean.meta.types.Arrays;
22 23 importocean.meta.traits.Basic;
24 25 /*******************************************************************************
26 27 Params:
28 T = any array type
29 30 Returns:
31 If T is static or dynamic array, evaluates to single type which is the
32 element type of that array. If T is an associative array, return
33 templated struct with separate type aliases for `Key` and `Value`.
34 35 *******************************************************************************/36 37 publictemplateElementTypeOf ( T )
38 {
39 staticif (isArrayType!(T) == ArrayKind.Dynamic40 || isArrayType!(T) == ArrayKind.Static)
41 {
42 staticif (is(TU : U[]))
43 {
44 aliasUElementTypeOf;
45 }
46 }
47 elsestaticif (isArrayType!(T) == ArrayKind.Associative)
48 {
49 aliasAAElementType!(typeof(T.init.keys[0]), typeof(T.init.values[0]))
50 ElementTypeOf;
51 }
52 else53 {
54 staticassert (false, "T must be some array type");
55 }
56 }
57 58 ///59 unittest60 {
61 staticassert (is(ElementTypeOf!(int[]) == int));
62 staticassert (is(ElementTypeOf!(int[5]) == int));
63 staticassert (is(ElementTypeOf!(int[][10]) == int[]));
64 65 aliasElementTypeOf!(double[int]) ElementType;
66 staticassert (is(ElementType.Key == int));
67 staticassert (is(ElementType.Value == double));
68 }
69 70 /// see `ElementTypeOf`71 publicstructAAElementType ( TKey, TValue )
72 {
73 aliasTKeyKey;
74 aliasTValueValue;
75 }
76 77 /*******************************************************************************
78 79 Params:
80 T = any type
81 82 Returns:
83 If T is static or dynamic array, evaluates to single type which is the
84 element type of that array. If such element type would also be an array,
85 evaluates to its element type instead and so on recursively. In all
86 other cases evaluates to just T.
87 88 *******************************************************************************/89 90 publictemplateStripAllArrays ( T )
91 {
92 staticif (isArrayType!(T) == ArrayKind.Dynamic93 || isArrayType!(T) == ArrayKind.Static)
94 {
95 aliasStripAllArrays!(ElementTypeOf!(T)) StripAllArrays;
96 }
97 else98 {
99 aliasTStripAllArrays;
100 }
101 }
102 103 ///104 unittest105 {
106 staticassert (is(StripAllArrays!(int[][4][][]) == int));
107 staticassert (is(StripAllArrays!(int[float][4][][]) == int[float]));
108 staticassert (is(StripAllArrays!(double) == double));
109 }