1 /******************************************************************************* 2 3 Templates to define types with modified qualifiers based on some input 4 types. 5 6 Many of helper templates here have been added because of D1 to D2 migration 7 to hide behind them qualifiers not supported in D1 (const, immutable, 8 inout). Others, like `Unqual`, are generally useful even outside of 9 migration context. 10 11 NB: because this module is often used as purely compile-time dependency it 12 used built-in asserts instead of `ocean.core.Test` to reduce amount of 13 cyclic imports. `ocean.meta` modules in general are not supposed to 14 import anything outside of `ocean.meta`. 15 16 Copyright: 17 Copyright (C) 2017 dunnhumby Germany GmbH. All rights reserved. 18 19 License: 20 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. 21 Alternatively, this file may be distributed under the terms of the Tango 22 3-Clause BSD License (see LICENSE_BSD.txt for details). 23 24 *******************************************************************************/ 25 26 module ocean.meta.types.Qualifiers; 27 28 /******************************************************************************* 29 30 Convenience string type aliases. 31 32 Initially defined to help with D2 migration but proved themselves as useful 33 shortcuts to reduce visual code clutter. 34 35 *******************************************************************************/ 36 37 alias immutable(char)[] istring; 38 alias const(char)[] cstring; 39 alias char[] mstring; 40 41 /******************************************************************************* 42 43 Strips top-most type qualifier. 44 45 This is a small helper useful for adapting templated code where template 46 parameter can possibly be deduced as const or immutable. Using this type 47 directly in implementation will result in unmodifiable variables which isn't 48 always wanted. 49 50 Example: 51 52 --- 53 void foo(Element)(Element[] buf) 54 { 55 // this causes an error if element 56 // gets deduced as const 57 Element tmp; 58 tmp = Element.init; 59 60 // this is ok 61 Unqual!(Element) tmp; 62 tmp = Element.init; 63 } 64 --- 65 66 *******************************************************************************/ 67 68 template Unqual(T) 69 { 70 static if (is(T U == const U)) 71 { 72 alias U Unqual; 73 } 74 else static if (is(T U == immutable U)) 75 { 76 alias U Unqual; 77 } 78 else 79 { 80 alias T Unqual; 81 } 82 } 83 84 unittest 85 { 86 static assert (is(Unqual!(typeof("a"[0])) == char)); 87 }