Changeset 68
- Timestamp:
- 12/19/06 18:07:17 (5 years ago)
- Files:
-
- trunk/examples/inherit/inherit.d (modified) (1 diff)
- trunk/examples/inherit/test.py (modified) (1 diff)
- trunk/infrastructure/pyd/class_wrap.d (modified) (6 diffs)
- trunk/infrastructure/pyd/func_wrap.d (modified) (1 diff)
- trunk/infrastructure/pyd/iteration.d (modified) (1 diff)
- trunk/infrastructure/pyd/make_object.d (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/examples/inherit/inherit.d
r64 r68 31 31 } 32 32 33 Base b1, b2, b3; 34 35 Base return_poly_base() { 36 if (b1 is null) b1 = new Base; 37 return b1; 38 } 39 40 Base return_poly_derived() { 41 if (b2 is null) b2 = new Derived; 42 return b2; 43 } 44 45 Base return_poly_wrap() { 46 if (b3 is null) b3 = new WrapDerive; 47 return b3; 48 } 49 33 50 extern(C) void PydMain() { 34 51 def!(call_poly); 52 def!(return_poly_base); 53 def!(return_poly_derived); 54 def!(return_poly_wrap); 35 55 36 56 module_init(); trunk/examples/inherit/test.py
r57 r68 36 36 #print "The basic inheritance support breaks down here:" 37 37 inherit.call_poly(p) 38 39 print 40 41 b1 = inherit.return_poly_base() 42 print "inherit.return_poly_base returned instance of Base" 43 assert type(b1) == inherit.Base 44 b2 = inherit.return_poly_derived() 45 b2a = inherit.return_poly_derived() 46 print "inherit.return_poly_derived returned instance of Derived" 47 assert type(b2) == inherit.Derived 48 print "inherit.return_poly_derived returned the same object twice" 49 assert b2 is b2a 50 b3 = inherit.return_poly_wrap() 51 print "inherit.return_poly_wrap returned instance of WrapDerive" 52 assert type(b3) == inherit.WrapDerive 53 54 print 55 print '-------' 56 print 'SUCCESS' 57 print '-------' trunk/infrastructure/pyd/class_wrap.d
r66 r68 40 40 import std.traits; 41 41 42 bool[TypeInfo] wrapped_classes; 42 //bool[TypeInfo] wrapped_types; 43 PyTypeObject*[ClassInfo] wrapped_classes; 43 44 44 45 // This is split out in case I ever want to make a subtype of a wrapped class. … … 448 449 PyModule_AddObject(Pyd_Module_p(modulename), name.ptr, cast(PyObject*)&type); 449 450 is_wrapped!(T) = true; 450 wrapped_classes[typeid(T)] = true; 451 static if (is(T == class)) { 452 wrapped_classes[T.classinfo] = &type; 453 } 451 454 } 452 455 … … 475 478 /////////////////////// 476 479 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 492 480 // If the passed D reference has an existing Python object, return a borrowed 493 481 // reference to it. Otherwise, return null. … … 522 510 } 523 511 512 PyObject* WrapPyObject_FromObject(T) (T t) { 513 return WrapPyObject_FromTypeAndObject(&wrapped_class_type!(T), t); 514 } 515 524 516 /** 525 517 * Returns a new Python object of a wrapped type. 526 518 */ 527 PyObject* WrapPyObject_From Object(T) (T t) {528 alias wrapped_class_object!(T) wrapped_object;529 alias wrapped_class_type!(T) type;519 PyObject* WrapPyObject_FromTypeAndObject(T) (PyTypeObject* type, T t) { 520 //alias wrapped_class_object!(T) wrapped_object; 521 //alias wrapped_class_type!(T) type; 530 522 if (is_wrapped!(T)) { 531 523 // If this object is already wrapped, get the existing object. … … 536 528 } 537 529 // Otherwise, allocate a new object 538 PyObject* obj = type.tp_new( &type, null, null);530 PyObject* obj = type.tp_new(type, null, null); 539 531 // Set the contained instance 540 532 WrapPyObject_SetObj(obj, t); … … 562 554 * Sets the contained object in self to t. 563 555 */ 564 void WrapPyObject_SetObj( PY, T) (PY* _self, T t) {556 void WrapPyObject_SetObj(T) (PyObject* _self, T t) { 565 557 alias wrapped_class_object!(T) obj; 566 558 obj* self = cast(obj*)_self; trunk/infrastructure/pyd/func_wrap.d
r66 r68 52 52 PyType_Ready(&type); 53 53 is_wrapped!(T) = true; 54 wrapped_classes[typeid(T)] = true;54 //wrapped_classes[typeid(T)] = true; 55 55 } 56 56 } trunk/infrastructure/pyd/iteration.d
r52 r68 124 124 PyType_Ready(&type); 125 125 is_wrapped!(StackContext) = true; 126 wrapped_classes[typeid(StackContext)] = true;126 //wrapped_classes[typeid(StackContext)] = true; 127 127 } 128 128 } trunk/infrastructure/pyd/make_object.d
r66 r68 142 142 } else static if (is(T == class)) { 143 143 // 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); 146 147 } 147 148 // If it's not a wrapped type, fall through to the exception.
