From 28755b4d0196f64528dde54514405f4fe3dc1314 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 22 Dec 2016 22:05:06 +0100 Subject: Rename module traits into enums --- source/tanya/enums.d | 49 +++++++++++++++++++++++++++++++++++++++++++ source/tanya/traits.d | 57 --------------------------------------------------- 2 files changed, 49 insertions(+), 57 deletions(-) create mode 100644 source/tanya/enums.d delete mode 100644 source/tanya/traits.d (limited to 'source') diff --git a/source/tanya/enums.d b/source/tanya/enums.d new file mode 100644 index 0000000..8fdbf8d --- /dev/null +++ b/source/tanya/enums.d @@ -0,0 +1,49 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Generic enum templates. + * + * Copyright: Eugene Wissner 2016. + * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, + * Mozilla Public License, v. 2.0). + * Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner) + */ +module tanya.enums; + +import std.traits; + +/** + * Initializer list. + * + * Generates a static array with elements from $(D_PARAM args). All elements + * should have the same type. It can be used in constructors which accept a + * list of the elements of the same type in the situations where variadic + * functions and templates can't be used. + * + * Params: + * Args = Argument type. + * args = Arguments. + */ +enum IL(Args...)(Args args) + if (Args.length > 0) +{ + alias BaseType = typeof(args[0]); + + BaseType[args.length] result; + + foreach (i, a; args) + { + static assert(isImplicitlyConvertible!(typeof(a), BaseType)); + result[i] = a; + } + return result; +} + +/// +unittest +{ + static assert(IL(1, 5, 8).length == 3); + static assert(IL(1, 5, 8).sizeof == 3 * int.sizeof); +} diff --git a/source/tanya/traits.d b/source/tanya/traits.d deleted file mode 100644 index 3f7dfcf..0000000 --- a/source/tanya/traits.d +++ /dev/null @@ -1,57 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -/** - * Copyright: Eugene Wissner 2016. - * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, - * Mozilla Public License, v. 2.0). - * Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner) - */ -module tanya.traits; - -import std.traits; -import std.meta; - -/** - * Params: - * T = Type. - * - * Returns: $(D_KEYWORD true) if $(D_PARAM T) is a reference type or a pointer, - * $(D_KEYWORD false) otherwise. - */ -enum bool isReference(T) = isDynamicArray!T || isPointer!T - || is(T == class) || is(T == interface); - -/** - * Initializer list. - * - * Generates a static array with elements from $(D_PARAM args). All elements - * should have the same type. It can be used in constructors which accept a - * list of the elements of the same type in the situations where variadic - * functions and templates can't be used. - * - * Params: - * Args = Argument type. - * args = Arguments. - */ -static enum IL(Args...)(Args args) - if (Args.length > 0) -{ - alias BaseType = typeof(args[0]); - - BaseType[args.length] result; - - foreach (i, a; args) - { - static assert(isImplicitlyConvertible!(typeof(a), BaseType)); - result[i] = a; - } - return result; -} - -/// -unittest -{ - static assert(IL(1, 5, 8).length == 3); -} -- cgit v1.2.3