Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changeset 1756

Show
Ignore:
Timestamp:
07/14/10 01:17:00 (14 years ago)
Author:
andrei
Message:

Added hasMember

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/phobos/std/traits.d

    r1742 r1756  
    12081208 
    12091209unittest 
    12101210{ 
    12111211    static assert(!hasElaborateAssign!int); 
    12121212    struct S { void opAssign(S) {} } 
    12131213    static assert(hasElaborateAssign!S); 
    12141214    struct S1 { void opAssign(ref S1) {} } 
    12151215    static assert(hasElaborateAssign!S1); 
    12161216    struct S2 { void opAssign(S1) {} } 
    12171217    static assert(!hasElaborateAssign!S2); 
     1218} 
     1219 
     1220/** 
     1221   Yields $(D true) if and only if $(D T) is a $(D struct) or a $(D 
     1222   class) that defines a symbol called $(D name). 
     1223 */ 
     1224template hasMember(T, string name) 
     1225{ 
     1226    static if (is(T == struct) || is(T == class)) 
     1227    { 
     1228        enum bool hasMember = 
     1229            staticIndexOf!(name, __traits(allMembers, T)) != -1; 
     1230    } 
     1231    else 
     1232    { 
     1233        enum bool hasMember = false; 
     1234    } 
     1235} 
     1236 
     1237unittest 
     1238{ 
     1239    //pragma(msg, __traits(allMembers, void delegate())); 
     1240    static assert(!hasMember!(int, "blah")); 
     1241    struct S1 { int blah; } 
     1242    static assert(hasMember!(S1, "blah")); 
     1243    struct S2 { int blah(); } 
     1244    static assert(hasMember!(S2, "blah")); 
     1245    struct C1 { int blah; } 
     1246    static assert(hasMember!(C1, "blah")); 
     1247    struct C2 { int blah(); } 
     1248    static assert(hasMember!(C2, "blah")); 
    12181249} 
    12191250 
    12201251//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::// 
    12211252// Classes and Interfaces 
    12221253//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::// 
    12231254 
    12241255/*** 
    12251256 * Get a $(D_PARAM TypeTuple) of the base class and base interfaces of 
    12261257 * this class or interface. $(D_PARAM BaseTypeTuple!(Object)) returns 
    12271258 * the empty type tuple.