Changeset 68

Show
Ignore:
Timestamp:
12/19/06 18:07:17 (2 years ago)
Author:
KirkMcDonald
Message:

Polymorphism-related improvements.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/examples/inherit/inherit.d

    r64 r68  
    3131} 
    3232 
     33Base b1, b2, b3; 
     34 
     35Base return_poly_base() { 
     36    if (b1 is null) b1 = new Base; 
     37    return b1; 
     38} 
     39 
     40Base return_poly_derived() { 
     41    if (b2 is null) b2 = new Derived; 
     42    return b2; 
     43} 
     44 
     45Base return_poly_wrap() { 
     46    if (b3 is null) b3 = new WrapDerive; 
     47    return b3; 
     48} 
     49 
    3350extern(C) void PydMain() { 
    3451    def!(call_poly); 
     52    def!(return_poly_base); 
     53    def!(return_poly_derived); 
     54    def!(return_poly_wrap); 
    3555 
    3656    module_init(); 
  • trunk/examples/inherit/test.py

    r57 r68  
    3636#print "The basic inheritance support breaks down here:" 
    3737inherit.call_poly(p) 
     38 
     39print 
     40 
     41b1 = inherit.return_poly_base() 
     42print "inherit.return_poly_base returned instance of Base" 
     43assert type(b1) == inherit.Base 
     44b2 = inherit.return_poly_derived() 
     45b2a = inherit.return_poly_derived() 
     46print "inherit.return_poly_derived returned instance of Derived" 
     47assert type(b2) == inherit.Derived 
     48print "inherit.return_poly_derived returned the same object twice" 
     49assert b2 is b2a 
     50b3 = inherit.return_poly_wrap() 
     51print "inherit.return_poly_wrap returned instance of WrapDerive" 
     52assert type(b3) == inherit.WrapDerive 
     53 
     54print 
     55print '-------' 
     56print 'SUCCESS' 
     57print '-------' 
  • trunk/infrastructure/pyd/class_wrap.d

    r66 r68  
    4040import std.traits; 
    4141 
    42 bool[TypeInfo] wrapped_classes; 
     42//bool[TypeInfo] wrapped_types; 
     43PyTypeObject*[ClassInfo] wrapped_classes; 
    4344 
    4445// This is split out in case I ever want to make a subtype of a wrapped class. 
     
    448449    PyModule_AddObject(Pyd_Module_p(modulename), name.ptr, cast(PyObject*)&type); 
    449450    is_wrapped!(T) = true; 
    450     wrapped_classes[typeid(T)] = true; 
     451    static if (is(T == class)) { 
     452        wrapped_classes[T.classinfo] = &type; 
     453    } 
    451454} 
    452455 
     
    475478/////////////////////// 
    476479 
    477 private union aa_reference(T) { 
    478     T aa; 
    479     void* ptr; 
    480 } 
    481  
    482 private void* get_voidptr(T)(T t) { 
    483     static if (isAA!(T)) { 
    484         aa_reference!(T) ref; 
    485         ref.aa = t; 
    486         return ref.ptr; 
    487     } else { 
    488         return cast(void*)t; 
    489     } 
    490 } 
    491  
    492480// If the passed D reference has an existing Python object, return a borrowed 
    493481// reference to it. Otherwise, return null. 
     
    522510} 
    523511 
     512PyObject* WrapPyObject_FromObject(T) (T t) { 
     513    return WrapPyObject_FromTypeAndObject(&wrapped_class_type!(T), t); 
     514} 
     515 
    524516/** 
    525517 * Returns a new Python object of a wrapped type. 
    526518 */ 
    527 PyObject* WrapPyObject_FromObject(T) (T t) { 
    528     alias wrapped_class_object!(T) wrapped_object; 
    529     alias wrapped_class_type!(T) type; 
     519PyObject* WrapPyObject_FromTypeAndObject(T) (PyTypeObject* type, T t) { 
     520    //alias wrapped_class_object!(T) wrapped_object; 
     521    //alias wrapped_class_type!(T) type; 
    530522    if (is_wrapped!(T)) { 
    531523        // If this object is already wrapped, get the existing object. 
     
    536528        } 
    537529        // Otherwise, allocate a new object 
    538         PyObject* obj = type.tp_new(&type, null, null); 
     530        PyObject* obj = type.tp_new(type, null, null); 
    539531        // Set the contained instance 
    540532        WrapPyObject_SetObj(obj, t); 
     
    562554 * Sets the contained object in self to t. 
    563555 */ 
    564 void WrapPyObject_SetObj(PY, T) (PY* _self, T t) { 
     556void WrapPyObject_SetObj(T) (PyObject* _self, T t) { 
    565557    alias wrapped_class_object!(T) obj; 
    566558    obj* self = cast(obj*)_self; 
  • trunk/infrastructure/pyd/func_wrap.d

    r66 r68  
    5252        PyType_Ready(&type); 
    5353        is_wrapped!(T) = true; 
    54         wrapped_classes[typeid(T)] = true; 
     54        //wrapped_classes[typeid(T)] = true; 
    5555    } 
    5656} 
  • trunk/infrastructure/pyd/iteration.d

    r52 r68  
    124124        PyType_Ready(&type); 
    125125        is_wrapped!(StackContext) = true; 
    126         wrapped_classes[typeid(StackContext)] = true; 
     126        //wrapped_classes[typeid(StackContext)] = true; 
    127127    } 
    128128} 
  • trunk/infrastructure/pyd/make_object.d

    r66 r68  
    142142    } else static if (is(T == class)) { 
    143143        // But only if it actually is a wrapped type. :-) 
    144         if (is_wrapped!(T)) { 
    145             return WrapPyObject_FromObject(t); 
     144        PyTypeObject** type = t.classinfo in wrapped_classes; 
     145        if (type) { 
     146            return WrapPyObject_FromTypeAndObject(*type, t); 
    146147        } 
    147148        // If it's not a wrapped type, fall through to the exception.