setTypeInfo???

bearophile bearophileHUGS at lycos.com
Sat Aug 30 14:45:55 PDT 2008


dsimcha Wrote:

> I've noticed something strange with the setTypeInfo() function in gc.d. Ran
> into it trying to write some custom allocation functions for dynamic arrays.
> writefln(typeid(uint).flags);  //Returns 0.
> writefln(typeid(float).flags);  //Returns 0.
> writefln(typeid(uint*).flags);  //Returns 1.
> writefln(typeid(void*).flags);  //Returns 1.

Ah, for that purpose I have used the following monster (take a look at my last post too http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=75811
):


import std.traits: FieldTypeTuple;

template IsType(T, Types...) {
    // Original idea by Burton Radons, modified
    static if (Types.length == 0)
        const bool IsType = false;
    else
        const bool IsType = is(T == Types[0]) || IsType!(T, Types[1 .. $]);
}

template ArrayType1(T: T[]) {
    alias T ArrayType1;
}

template IsReferenceType(Types...) {
    static if (Types.length == 0) {
        const bool IsReferenceType = false;
    } else static if (Types.length == 1) {
        static if (IsType!(Types[0], bool, byte, ubyte, short, ushort, int, uint,
                           long, ulong, float, double, real, ifloat, idouble,
                           ireal, cfloat, cdouble, creal, char, dchar, wchar) ) {
            const bool IsReferenceType = false;
        } else static if ( is(Types[0] == struct) ) {
            const bool IsReferenceType = IsReferenceType!(FieldTypeTuple!(Types[0]));
        } else static if (IsStaticArray!(Types[0])) {
            const bool IsReferenceType = IsReferenceType!(ArrayType1!(Types[0]));
        } else
            const bool IsReferenceType = true;
    } else
        const bool IsReferenceType = IsReferenceType!(Types[0]) |
                                     IsReferenceType!(Types[1 .. $]);
} // end IsReferenceType!()


template IsArray(T) {
    const bool IsArray = is(typeof(T.length)) && is(typeof(T.sort)) &&
                         is(typeof(T.reverse)) && is(typeof(T.dup));
}

template IsDynamicArray(T) {
    // Adapted from the module tango.core.Traits, (C) 2005-2006 Sean Kelly, BSD style licence
    const bool IsDynamicArray = is( typeof(T.init[0])[] == T );
}

template IsStaticArray(T) {
    const bool IsStaticArray = IsArray!(T) && (!IsDynamicArray!(T));
}

Bye,
bearophile



More information about the Digitalmars-d mailing list