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

Changeset 1722

Show
Ignore:
Timestamp:
07/04/10 21:49:57 (14 years ago)
Author:
andrei
Message:

Implemented hasIndirections

Files:

Legend:

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

    r1709 r1722  
    831831// { 
    832832//     struct S1 { char c; int i; } 
    833833//     alias RepresentationOffsets!(S1) Offsets; 
    834834//     static assert(Offsets[0] == 0); 
    835835//     //pragma(msg, Offsets[1]); 
    836836//     static assert(Offsets[1] == 4); 
    837837// } 
    838838 
    839839// hasRawAliasing 
    840840 
    841 private template HasRawPointerImpl(T...) 
     841private template hasRawPointerImpl(T...) 
    842842{ 
    843843    static if (T.length == 0) 
    844844    { 
    845845        enum result = false; 
    846846    } 
    847847    else 
    848848    { 
    849849        static if (is(T[0] foo : U*, U)) 
    850850            enum hasRawAliasing = !is(U == immutable); 
    851851        else static if (is(T[0] foo : U[], U) && !isStaticArray!(T[0])) 
    852852            enum hasRawAliasing = !is(U == immutable); 
    853853        else 
    854854            enum hasRawAliasing = false; 
    855         enum result = hasRawAliasing || HasRawPointerImpl!(T[1 .. $]).result; 
     855        enum result = hasRawAliasing || hasRawPointerImpl!(T[1 .. $]).result; 
    856856    } 
    857857} 
    858858 
    859859private template HasRawLocalPointerImpl(T...) 
    860860{ 
    861861    static if (T.length == 0) 
    862862    { 
    863863        enum result = false; 
    864864    } 
    865865    else 
     
    899899struct S3 { int a; double * b; } 
    900900static assert(hasRawAliasing!(S3)); 
    901901// struct with an indirect pointer member 
    902902struct S4 { S3 a; double b; } 
    903903static assert(hasRawAliasing!(S4)); 
    904904---- 
    905905*/ 
    906906private template hasRawAliasing(T...) 
    907907{ 
    908908    enum hasRawAliasing 
    909         = HasRawPointerImpl!(RepresentationTypeTuple!(T)).result; 
     909        = hasRawPointerImpl!(RepresentationTypeTuple!(T)).result; 
    910910} 
    911911 
    912912unittest 
    913913{ 
    914914// simple types 
    915915    static assert(!hasRawAliasing!(int)); 
    916916    static assert(hasRawAliasing!(char*)); 
    917917// references aren't raw pointers 
    918918    static assert(!hasRawAliasing!(Object)); 
    919919    static assert(!hasRawAliasing!(int)); 
     
    11001100unittest 
    11011101{ 
    11021102    struct S1 { int a; Object b; } 
    11031103    static assert(hasAliasing!(S1)); 
    11041104    struct S2 { string a; } 
    11051105    static assert(!hasAliasing!(S2)); 
    11061106    struct S3 { int a; immutable Object b; } 
    11071107    static assert(!hasAliasing!(S3)); 
    11081108    struct X { float[3] vals; } 
    11091109    static assert(!hasAliasing!X); 
     1110} 
     1111 
     1112/** 
     1113Returns $(D true) if and only if $(D T)'s representation includes at 
     1114least one of the following: $(OL $(LI a raw pointer $(D U*);) $(LI an 
     1115array $(D U[]);) $(LI a reference to a class type $(D C).)) 
     1116 */ 
     1117 
     1118template hasIndirections(T) 
     1119{ 
     1120    enum hasIndirections = hasIndirectionsImpl!(RepresentationTypeTuple!T); 
     1121} 
     1122 
     1123template hasIndirectionsImpl(T...) 
     1124{ 
     1125    static if (!T.length) 
     1126    { 
     1127        enum hasIndirectionsImpl = false; 
     1128    } 
     1129    else 
     1130    { 
     1131        enum hasIndirectionsImpl = isPointer!(T[0]) || isDynamicArray!(T[0]) || 
     1132            is (T[0] : const(Object)) || hasIndirectionsImpl!(T[1 .. $]); 
     1133    } 
     1134} 
     1135 
     1136unittest 
     1137{ 
     1138    struct S1 { int a; Object b; } 
     1139    static assert(hasIndirections!(S1)); 
     1140    struct S2 { string a; } 
     1141    static assert(hasIndirections!(S2)); 
     1142    struct S3 { int a; immutable Object b; } 
     1143    static assert(hasIndirections!(S3)); 
    11101144} 
    11111145 
    11121146/** 
    11131147Returns $(D true) if and only if $(D T)'s representation includes at 
    11141148least one of the following: $(OL $(LI a raw pointer $(D U*) and $(D U) 
    11151149is not immutable or shared;) $(LI an array $(D U[]) and $(D U) is not 
    11161150immutable or shared;) $(LI a reference to a class type $(D C) and 
    11171151$(D C) is not immutable or shared.)) 
    11181152*/ 
    11191153