From cd1a38f402016dfc1b96ba1a47a2130c1c57d150 Mon Sep 17 00:00:00 2001 From: Eugene Wissner Date: Wed, 2 May 2018 15:50:28 +0200 Subject: [PATCH] Move Smallest and Largest to meta.transform Smallest and Largest choose the smallest or largest (according to .sizeof property) type in the list of types. These templates get a list of types and produce a type, so they are transformations. --- source/tanya/meta/trait.d | 45 +++----------------- source/tanya/meta/transform.d | 78 +++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 40 deletions(-) diff --git a/source/tanya/meta/trait.d b/source/tanya/meta/trait.d index 3091fea..5490f5d 100644 --- a/source/tanya/meta/trait.d +++ b/source/tanya/meta/trait.d @@ -70,17 +70,7 @@ enum bool isWideString(T) = is(T : const dchar[]) && !isStaticArray!T; static assert(!isWideString!(dchar[10])); } -/** - * Finds the type with the smallest size in the $(D_PARAM Args) list. If - * several types have the same type, the leftmost is returned. - * - * Params: - * Args = Type list. - * - * Returns: The smallest type. - * - * See_Also: $(D_PSYMBOL Largest). - */ +deprecated("Use tanya.meta.transform.Smallest instead") template Smallest(Args...) if (Args.length >= 1) { @@ -100,15 +90,6 @@ if (Args.length >= 1) } } -/// -@nogc nothrow pure @safe unittest -{ - static assert(is(Smallest!(int, ushort, uint, short) == ushort)); - static assert(is(Smallest!(short) == short)); - static assert(is(Smallest!(ubyte[8], ubyte[5]) == ubyte[5])); - static assert(!is(Smallest!(short, 5))); -} - /** * Determines whether $(D_PARAM T) is a complex type. * @@ -185,6 +166,9 @@ enum bool isPolymorphicType(T) = is(T == class) || is(T == interface); } /** + * Determines whether the type $(D_PARAM T) has a static method + * named $(D_PARAM member). + * * Params: * T = Aggregate type. * member = Symbol name. @@ -944,17 +928,7 @@ template mostNegative(T) static assert(mostNegative!cfloat == -cfloat.max); } -/** - * Finds the type with the largest size in the $(D_PARAM Args) list. If several - * types have the same type, the leftmost is returned. - * - * Params: - * Args = Type list. - * - * Returns: The largest type. - * - * See_Also: $(D_PSYMBOL Smallest). - */ +deprecated("Use tanya.meta.transform.Largest instead") template Largest(Args...) if (Args.length >= 1) { @@ -974,15 +948,6 @@ if (Args.length >= 1) } } -/// -@nogc nothrow pure @safe unittest -{ - static assert(is(Largest!(int, short, uint) == int)); - static assert(is(Largest!(short) == short)); - static assert(is(Largest!(ubyte[8], ubyte[5]) == ubyte[8])); - static assert(!is(Largest!(short, 5))); -} - /** * Determines whether the type $(D_PARAM T) is copyable. * diff --git a/source/tanya/meta/transform.d b/source/tanya/meta/transform.d index 82eb2a7..5697e69 100644 --- a/source/tanya/meta/transform.d +++ b/source/tanya/meta/transform.d @@ -901,3 +901,81 @@ if (allSatisfy!(isType, Args)) static assert(is(CommonType!(S, int) == void)); static assert(is(CommonType!(const S, S) == const S)); } + +/** + * Finds the type with the smallest size in the $(D_PARAM Args) list. If + * several types have the same type, the leftmost is returned. + * + * Params: + * Args = Type list. + * + * Returns: The smallest type. + * + * See_Also: $(D_PSYMBOL Largest). + */ +template Smallest(Args...) +if (Args.length >= 1) +{ + static assert(is(Args[0]), T.stringof ~ " doesn't have .sizeof property"); + + static if (Args.length == 1) + { + alias Smallest = Args[0]; + } + else static if (Smallest!(Args[1 .. $]).sizeof < Args[0].sizeof) + { + alias Smallest = Smallest!(Args[1 .. $]); + } + else + { + alias Smallest = Args[0]; + } +} + +/// +@nogc nothrow pure @safe unittest +{ + static assert(is(Smallest!(int, ushort, uint, short) == ushort)); + static assert(is(Smallest!(short) == short)); + static assert(is(Smallest!(ubyte[8], ubyte[5]) == ubyte[5])); + static assert(!is(Smallest!(short, 5))); +} + +/** + * Finds the type with the largest size in the $(D_PARAM Args) list. If several + * types have the same type, the leftmost is returned. + * + * Params: + * Args = Type list. + * + * Returns: The largest type. + * + * See_Also: $(D_PSYMBOL Smallest). + */ +template Largest(Args...) +if (Args.length >= 1) +{ + static assert(is(Args[0]), T.stringof ~ " doesn't have .sizeof property"); + + static if (Args.length == 1) + { + alias Largest = Args[0]; + } + else static if (Largest!(Args[1 .. $]).sizeof > Args[0].sizeof) + { + alias Largest = Largest!(Args[1 .. $]); + } + else + { + alias Largest = Args[0]; + } +} + +/// +@nogc nothrow pure @safe unittest +{ + static assert(is(Largest!(int, short, uint) == int)); + static assert(is(Largest!(short) == short)); + static assert(is(Largest!(ubyte[8], ubyte[5]) == ubyte[8])); + static assert(!is(Largest!(short, 5))); +}