Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3810

Show
Ignore:
Timestamp:
07/26/08 15:33:57 (4 months ago)
Author:
sean
Message:

Added support for storing and retrieving interfaces. This closes #1057

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/core/Variant.d

    r3200 r3810  
    9595    } 
    9696 
     97    template isInterface(T) 
     98    { 
     99        static if( is( T == interface ) ) 
     100            const isInterface = true; 
     101        else 
     102            const isInterface = false; 
     103    } 
     104 
    97105    template isStaticArray(T) 
    98106    { 
     
    279287                this.value.obj = value; 
    280288            } 
     289            else static if( isInterface!(T) ) 
     290            { 
     291                this.value.obj = cast(Object) value; 
     292            } 
    281293            else 
    282294            { 
     
    366378                // Let D do runtime check itself 
    367379                && !isObject!(T) 
     380                && !isInterface!(T) 
    368381                // Allow implicit upcasts 
    369382                && !canImplicitCastTo!(T)(type) 
     
    411424        } 
    412425        else static if( isObject!(T) ) 
     426        { 
     427            return cast(T)this.value.obj; 
     428        } 
     429        else static if( isInterface!(T) ) 
    413430        { 
    414431            return cast(T)this.value.obj; 
     
    614631        } 
    615632 
     633        // Test interface support 
     634        { 
     635            interface A {} 
     636            interface B : A {} 
     637            class C : B {} 
     638            class D : C {} 
     639 
     640            A a = new D; 
     641            Variant v = a; 
     642            B b = v.get!(B); 
     643            C c = v.get!(C); 
     644            D d = v.get!(D); 
     645        } 
     646 
    616647        // Test doubles and implicit casting 
    617648        v = 3.1413; 
     
    704735    } 
    705736} 
    706