Mercurial > projects > orange
diff orange/util/Traits.d @ 1:11a31bd929f9
Removed dependency on private library
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Mon, 31 May 2010 16:06:36 +0200 |
parents | f7b078e85f7f |
children | c4e7e64ffb67 |
line wrap: on
line diff
--- a/orange/util/Traits.d Wed May 26 17:19:13 2010 +0200 +++ b/orange/util/Traits.d Mon May 31 16:06:36 2010 +0200 @@ -6,12 +6,172 @@ */ module orange.util.Traits; -public import mambo.util.Traits; - import orange.serialization.Serializable; import orange.serialization.archives.Archive; import orange.util._; +version (Tango) +{ + import Tango = tango.core.Traits; + alias Tango.BaseTypeTupleOf BaseTypeTupleOf; + alias Tango.ParameterTupleOf ParameterTupleOf; + alias Tango.ReturnTypeOf ReturnTypeOf; +} + +else +{ + import Phobos = std.traits; + alias Phobos.BaseTypeTuple BaseTypeTupleOf; + alias Phobos.ParameterTypeTuple ParameterTupleOf; + alias Phobos.ReturnType ReturnTypeOf; + + version = Phobos; +} + +template isPrimitive (T) +{ + const bool isPrimitive = is(T == bool) || + is(T == byte) || + is(T == cdouble) || + //is(T == cent) || + is(T == cfloat) || + is(T == char) || + is(T == creal) || + is(T == dchar) || + is(T == double) || + is(T == float) || + is(T == idouble) || + is(T == ifloat) || + is(T == int) || + is(T == ireal) || + is(T == long) || + is(T == real) || + is(T == short) || + is(T == ubyte) || + //is(T == ucent) || + is(T == uint) || + is(T == ulong) || + is(T == ushort) || + is(T == wchar); +} + +template isChar (T) +{ + const bool isChar = is(T == char) || is(T == wchar) || is(T == dchar); +} + +template isClass (T) +{ + const bool isClass = is(T == class); +} + +template isInterface (T) +{ + const bool isInterface = is(T == interface); +} + +template isObject (T) +{ + const bool isObject = isClass!(T) || isInterface!(T); +} + +template isStruct (T) +{ + const bool isStruct = is(T == struct); +} + +template isArray (T) +{ + static if (is(T U : U[])) + const bool isArray = true; + + else + const bool isArray = false; +} + +template isString (T) +{ + const bool isString = is(T : char[]) || is(T : wchar[]) || is(T : dchar[]); +} + +template isAssociativeArray (T) +{ + const bool isAssociativeArray = is(typeof(T.init.values[0])[typeof(T.init.keys[0])] == T); +} + +template isPointer (T) +{ + static if (is(T U : U*)) + const bool isPointer = true; + + else + const bool isPointer = false; +} + +template isFunctionPointer (T) +{ + const bool isFunctionPointer = is(typeof(*T) == function); +} + +template isEnum (T) +{ + const bool isEnum = is(T == enum); +} + +template isReference (T) +{ + const bool isReference = isObject!(T) || isPointer!(T); +} + +template isTypeDef (T) +{ + const bool isTypeDef = is(T == typedef); +} + +template isVoid (T) +{ + const bool isVoid = is(T == void); +} + +template BaseTypeOfArray (T) +{ + static if (is(T U : U[])) + alias BaseTypeOfArray!(U) BaseTypeOfArray; + + else + alias T BaseTypeOfArray; +} + +template BaseTypeOfPointer (T) +{ + static if (is(T U : U*)) + alias BaseTypeOfPointer!(U) BaseTypeOfPointer; + + else + alias T BaseTypeOfPointer; +} + +template BaseTypeOfTypeDef (T) +{ + static if (is(T U == typedef)) + alias BaseTypeOfTypeDef!(U) BaseTypeOfTypeDef; + + else + alias T BaseTypeOfTypeDef; +} + +template KeyTypeOfAssociativeArray (T) +{ + static assert(isAssociativeArray!(T), "The type needs to be an associative array"); + alias typeof(T.init.keys[0]) KeyTypeOfAssociativeArray; +} + +template ValueTypeOfAssociativeArray (T) +{ + static assert(isAssociativeArray!(T), "The type needs to be an associative array"); + alias typeof(T.init.values[0]) ValueTypeOfAssociativeArray; +} + template isArchive (T) { const isArchive = is(typeof({