Changeset 118
- Timestamp:
- 07/10/07 15:14:05 (1 year ago)
- Files:
-
- trunk/examples/testdll/testdll.d (modified) (3 diffs)
- trunk/infrastructure/pyd/class_wrap.d (modified) (18 diffs)
- trunk/infrastructure/pyd/ctor_wrap.d (modified) (2 diffs)
- trunk/infrastructure/pyd/make_wrapper.d (modified) (1 diff)
- trunk/infrastructure/pyd/pyd.d (modified) (1 diff)
- trunk/infrastructure/pyd/struct_wrap.d (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/examples/testdll/testdll.d
r114 r118 59 59 int i() { return m_i; } 60 60 void i(int j) { m_i = j; } 61 void a() {} 62 void b() {} 63 void c() {} 64 void d() {} 65 void e() {} 66 void f() {} 67 void g() {} 68 void h() {} 69 void j() {} 70 void k() {} 71 void l() {} 72 void m() {} 73 void n() {} 74 void o() {} 75 void p() {} 76 void q() {} 77 void r() {} 78 void s() {} 79 void t() {} 80 void u() {} 81 void v() {} 82 void w() {} 83 void x() {} 84 void y() {} 85 void z() {} 61 86 } 62 87 … … 114 139 writefln(a.i); 115 140 } 141 142 mixin _wrap_class!( 143 Foo, 144 "Foo", 145 Init!(void delegate(int), void delegate(int, int)), 146 Property!(Foo.i, "A sample property of Foo."), 147 Def!(Foo.foo, "A sample method of Foo."), 148 Def!(Foo.a), 149 Def!(Foo.b), 150 Def!(Foo.c), 151 Def!(Foo.d), 152 Def!(Foo.e), 153 Def!(Foo.f), 154 Def!(Foo.g), 155 Def!(Foo.h), 156 Def!(Foo.j), 157 Def!(Foo.k), 158 Def!(Foo.l), 159 Def!(Foo.m), 160 Def!(Foo.n)/*, // Maximum length 161 Def!(Foo.o), 162 Def!(Foo.p), 163 Def!(Foo.q), 164 Def!(Foo.r), 165 Def!(Foo.s), 166 Def!(Foo.t), 167 Def!(Foo.u), 168 Def!(Foo.v), 169 Def!(Foo.w), 170 Def!(Foo.x), 171 Def!(Foo.y), 172 Def!(Foo.z)*/ 173 ) F; 116 174 117 175 extern(C) void PydMain() { … … 138 196 module_init(); 139 197 140 wrap_class!( 141 Foo, 142 Init!(void delegate(int), void delegate(int, int)), 143 Property!(Foo.i, "A sample property of Foo."), 144 Def!(Foo.foo, "A sample method of Foo.") 145 ) ("A sample class."); 198 F.wrap_class("A sample class."); 146 199 147 200 wrap_struct!( trunk/infrastructure/pyd/class_wrap.d
r113 r118 26 26 import pyd.ctor_wrap; 27 27 import pyd.def; 28 import pyd.dg_convert; 28 29 import pyd.exception; 29 30 import pyd.func_wrap; … … 272 273 if more than one function has the same name as this one. 273 274 */ 274 //template Def(alias fn, char[] name = symbolnameof!(fn), fn_t=typeof(&fn), uint MIN_ARGS=minArgs!(fn), char[] docstring="") { 275 // alias Def!(fn, symbolnameof!(fn), name, fn_t, MIN_ARGS, docstring) Def; 276 //} 277 template Def(alias fn, char[] docstring="") { 278 alias Def!(fn, symbolnameof!(fn), symbolnameof!(fn), typeof(&fn), minArgs!(fn), docstring) Def; 279 } 280 template Def(alias fn, char[] name, char[] docstring) { 281 alias Def!(fn, symbolnameof!(fn), name, typeof(&fn), minArgs!(fn), docstring) Def; 282 } 283 template Def(alias fn, char[] name, fn_t, char[] docstring) { 284 alias Def!(fn, symbolnameof!(fn), name, fn_t, minArgs!(fn), docstring) Def; 285 } 286 template Def(alias fn, fn_t, char[] docstring="") { 287 alias Def!(fn, symbolnameof!(fn), symbolnameof!(fn), fn_t, minArgs!(fn), docstring) Def; 288 } 289 template Def(alias fn, char[] name, fn_t, uint MIN_ARGS=minArgs!(fn), char[] docstring="") { 290 alias Def!(fn, symbolnameof!(fn), name, fn_t, MIN_ARGS, docstring) Def; 291 } 292 struct Def(alias fn, char[] _realname, char[] name, fn_t, uint MIN_ARGS, char[] docstring) { 275 struct Def(alias fn) { 276 mixin _Def!(fn, symbolnameof!(fn), typeof(&fn), ""); 277 } 278 struct Def(alias fn, char[] docstring) { 279 mixin _Def!(fn, /*symbolnameof!(fn),*/ symbolnameof!(fn), typeof(&fn)/+, minArgs!(fn)+/, docstring); 280 } 281 struct Def(alias fn, char[] name, char[] docstring) { 282 mixin _Def!(fn, /*symbolnameof!(fn),*/ name, typeof(&fn)/+, minArgs!(fn)+/, docstring); 283 } 284 struct Def(alias fn, char[] name, fn_t) { 285 mixin _Def!(fn, /*symbolnameof!(fn),*/ name, fn_t/+, minArgs!(fn)+/, ""); 286 } 287 struct Def(alias fn, fn_t) { 288 mixin _Def!(fn, /*symbolnameof!(fn),*/ symbolnameof!(fn), fn_t/+, minArgs!(fn)+/, ""); 289 } 290 struct Def(alias fn, fn_t, char[] docstring) { 291 mixin _Def!(fn, /*symbolnameof!(fn),*/ symbolnameof!(fn), fn_t/+, minArgs!(fn)+/, docstring); 292 } 293 struct Def(alias fn, char[] name, fn_t, char[] docstring) { 294 mixin _Def!(fn, /*symbolnameof!(fn),*/ name, fn_t/+, minArgs!(fn)+/, docstring); 295 } 296 /+ 297 template Def(alias fn, char[] name, fn_t, uint MIN_ARGS=minArgs!(fn)/+, char[] docstring=""+/) { 298 alias Def!(fn, /*symbolnameof!(fn),*/ name, fn_t, MIN_ARGS/+, docstring+/) Def; 299 } 300 +/ 301 template _Def(alias fn, /*char[] _realname,*/ char[] name, fn_t/+, uint MIN_ARGS=minArgs!(fn)+/, char[] docstring) { 293 302 //static const type = ParamType.Def; 294 303 alias fn func; 295 304 alias fn_t func_t; 296 static const char[] realname = _realname;305 static const char[] realname = symbolnameof!(fn);//_realname; 297 306 static const char[] funcname = name; 298 static const uint min_args = MIN_ARGS; 299 300 static void call(T, shim) () { 307 static const uint min_args = minArgs!(fn); 308 static const bool needs_shim = false; 309 310 static void call(T) () { 301 311 pragma(msg, "class.def: " ~ name); 302 312 static PyMethodDef empty = { null, null, 0, null }; … … 314 324 const char[] shim = 315 325 " alias Params["~ToString!(i)~"] __pyd_p"~ToString!(i)~";\n" 316 " ReturnType!(__pyd_p"~ToString!(i)~".func_t) "~ _realname~"(ParameterTypeTuple!(__pyd_p"~ToString!(i)~".func_t) t) {\n"317 " return __pyd_get_overload!(\""~ _realname~"\", __pyd_p"~ToString!(i)~".func_t).func(\""~name~"\", t);\n"326 " ReturnType!(__pyd_p"~ToString!(i)~".func_t) "~realname~"(ParameterTypeTuple!(__pyd_p"~ToString!(i)~".func_t) t) {\n" 327 " return __pyd_get_overload!(\""~realname~"\", __pyd_p"~ToString!(i)~".func_t).func(\""~name~"\", t);\n" 318 328 " }\n"; 319 329 } … … 323 333 Wraps a static member function of the class. Identical to pyd.def.def 324 334 */ 325 template StaticDef(alias fn, char[] docstring="") { 326 alias StaticDef!(fn, symbolnameof!(fn), symbolnameof!(fn), typeof(&fn), minArgs!(fn), docstring) StaticDef; 327 } 328 template StaticDef(alias fn, char[] name, char[] docstring) { 329 alias StaticDef!(fn, symbolnameof!(fn), name, typeof(&fn), minArgs!(fn), docstring) StaticDef; 330 } 331 template StaticDef(alias fn, char[] name, fn_t, char[] docstring) { 332 alias StaticDef!(fn, symbolnameof!(fn), name, fn_t, minArgs!(fn), docstring) StaticDef; 333 } 334 template StaticDef(alias fn, fn_t, char[] docstring="") { 335 alias StaticDef!(fn, symbolnameof!(fn), symbolnameof!(fn), fn_t, minArgs!(fn), docstring) StaticDef; 336 } 337 template StaticDef(alias fn, char[] name, fn_t, uint MIN_ARGS=minArgs!(fn), char[] docstring="") { 338 alias StaticDef!(fn, symbolnameof!(fn), name, fn_t, MIN_ARGS, docstring) StaticDef; 339 } 340 struct StaticDef(alias fn, char[] _realname, char[] name, fn_t, uint MIN_ARGS, char[] docstring) { 335 struct StaticDef(alias fn) { 336 mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ symbolnameof!(fn), typeof(&fn), minArgs!(fn), ""); 337 } 338 struct StaticDef(alias fn, char[] docstring) { 339 mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ symbolnameof!(fn), typeof(&fn), minArgs!(fn), docstring); 340 } 341 struct StaticDef(alias _fn, char[] name, char[] docstring) { 342 mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ name, typeof(&fn), minArgs!(fn), docstring); 343 } 344 struct StaticDef(alias _fn, char[] name, fn_t, char[] docstring) { 345 mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ name, fn_t, minArgs!(fn), docstring); 346 } 347 struct StaticDef(alias _fn, fn_t) { 348 mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ symbolnameof!(fn), fn_t, minArgs!(fn), ""); 349 } 350 struct StaticDef(alias _fn, fn_t, char[] docstring) { 351 mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ symbolnameof!(fn), fn_t, minArgs!(fn), docstring); 352 } 353 struct StaticDef(alias _fn, char[] name, fn_t) { 354 mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ name, fn_t, minArgs!(fn), ""); 355 } 356 struct StaticDef(alias _fn, char[] name, fn_t, uint MIN_ARGS) { 357 mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ name, fn_t, MIN_ARGS, ""); 358 } 359 struct StaticDef(alias _fn, char[] name, fn_t, uint MIN_ARGS, char[] docstring) { 360 mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ name, fn_t, MIN_ARGS, docstring); 361 } 362 template _StaticDef(alias fn,/+ char[] _realname,+/ char[] name, fn_t, uint MIN_ARGS, char[] docstring) { 341 363 //static const type = ParamType.StaticDef; 342 364 alias fn func; … … 344 366 static const char[] funcname = name; 345 367 static const uint min_args = MIN_ARGS; 346 static void call(T, shim) () { 368 static const bool needs_shim = false; 369 static void call(T) () { 347 370 pragma(msg, "class.static_def: " ~ name); 348 371 static PyMethodDef empty = { null, null, 0, null }; … … 371 394 // alias Property!(fn, symbolnameof!(fn), name, RO, docstring) Property; 372 395 //} 373 template Property(alias fn, char[] docstring="") { 374 alias Property!(fn, symbolnameof!(fn), symbolnameof!(fn), false, docstring) Property; 375 } 376 template Property(alias fn, char[] name, char[] docstring) { 377 alias Property!(fn, symbolnameof!(fn), name, false, docstring) Property; 378 } 379 template Property(alias fn, char[] name, bool RO, char[] docstring="") { 380 alias Property!(fn, symbolnameof!(fn), name, RO, docstring) Property; 381 } 382 template Property(alias fn, bool RO, char[] docstring="") { 383 alias Property!(fn, symbolnameof!(fn), symbolnameof!(fn), RO, docstring) Property; 384 } 385 struct Property(alias fn, char[] _realname, char[] name, bool RO, char[] docstring) { 396 struct Property(alias fn) { 397 mixin _Property!(fn, symbolnameof!(fn), symbolnameof!(fn), false, ""); 398 } 399 struct Property(alias fn, char[] docstring) { 400 mixin _Property!(fn, symbolnameof!(fn), symbolnameof!(fn), false, docstring); 401 } 402 struct Property(alias fn, char[] name, char[] docstring) { 403 mixin _Property!(fn, symbolnameof!(fn), name, false, docstring); 404 } 405 struct Property(alias fn, char[] name, bool RO) { 406 mixin _Property!(fn, symbolnameof!(fn), name, RO, ""); 407 } 408 struct Property(alias fn, char[] name, bool RO, char[] docstring) { 409 mixin _Property!(fn, symbolnameof!(fn), name, RO, docstring); 410 } 411 struct Property(alias fn, bool RO) { 412 mixin _Property!(fn, symbolnameof!(fn), symbolnameof!(fn), RO, ""); 413 } 414 struct Property(alias fn, bool RO, char[] docstring) { 415 mixin _Property!(fn, symbolnameof!(fn), symbolnameof!(fn), RO, docstring); 416 } 417 template _Property(alias fn, char[] _realname, char[] name, bool RO, char[] docstring) { 386 418 alias property_parts!(fn).getter_type get_t; 387 419 alias property_parts!(fn).setter_type set_t; … … 389 421 static const char[] funcname = name; 390 422 static const bool readonly = RO; 391 static void call(T, shim) () { 423 static const bool needs_shim = false; 424 static void call(T) () { 392 425 pragma(msg, "class.prop: " ~ name); 393 426 static PyGetSetDef empty = { null, null, null, null, null }; … … 431 464 */ 432 465 struct Repr(alias fn) { 433 static void call(T, shim)() { 466 static const bool needs_shim = false; 467 static void call(T)() { 434 468 alias wrapped_class_type!(T) type; 435 469 type.tp_repr = &wrapped_repr!(T, fn).repr; … … 454 488 struct Init(C ...) { 455 489 alias C ctors; 456 static void call(T, shim) () { 457 wrapped_class_type!(T).tp_init = 458 &wrapped_ctors!(shim, C).init_func; 490 static const bool needs_shim = true; 491 template call(T) { 492 mixin wrapped_ctors!(param.ctors) Ctors; 493 static void call() { 494 wrapped_class_type!(T).tp_init = 495 &Ctors.init_func; 496 } 459 497 } 460 498 template shim_impl(uint i, uint c=0) { … … 488 526 */ 489 527 struct Iter(iter_t) { 528 static const bool needs_shim = false; 490 529 alias iter_t iterator_t; 491 static void call(T , shim) () {530 static void call(T) () { 492 531 PydStackContext_Ready(); 493 532 // This strange bit of hackery is needed since we operate on pointer- … … 507 546 */ 508 547 struct AltIter(alias fn, char[] name = symbolnameof!(fn), iter_t = ParameterTypeTuple!(fn)[0]) { 509 static void call(T, shim) () { 548 static const bool needs_shim = false; 549 static void call(T) () { 510 550 static PyMethodDef empty = { null, null, 0, null }; 511 551 alias wrapped_method_list!(T) list; … … 525 565 526 566 void wrap_class(T, Params...) (char[] docstring="", char[] modulename="") { 527 wrap_class!(T, symbolnameof!(T), Params)(docstring, modulename); 528 } 529 void wrap_class(_T, char[] name, Params...) (char[] docstring="", char[] modulename="") { 530 //alias CLS.wrapped_type T; 531 //const char[] name = CLS._name; 567 _wrap_class!(T, symbolnameof!(T), Params).wrap_class(docstring, modulename); 568 } 569 /+ 570 template _wrap_class(T, Params...) { 571 mixin _wrap_class!(T, symbolnameof!(T), Params); 572 } 573 +/ 574 template _wrap_class(_T, char[] name, Params...) { 532 575 static if (is(_T == class)) { 533 576 pragma(msg, "wrap_class: " ~ name); 534 alias make_wrapper!(_T, Params).wrapper shim_class; 577 mixin pyd.make_wrapper.make_wrapper!(_T, Params); 578 alias wrapper shim_class; 535 579 alias _T T; 536 580 // } else static if (is(_T == interface)) { … … 543 587 alias _T* T; 544 588 } 589 void wrap_class(char[] docstring="", char[] modulename="") { 590 pragma(msg, "shim.mangleof: " ~ shim_class.mangleof); 545 591 alias wrapped_class_type!(T) type; 546 592 //pragma(msg, "wrap_class, T is " ~ prettytypeof!(T)); … … 548 594 //Params params; 549 595 foreach (param; Params) { 550 param.call!(T, shim_class)(); 596 static if (param.needs_shim) { 597 mixin param.call!(T) PCall; 598 PCall.call(); 599 } else { 600 param.call!(T)(); 601 } 551 602 } 552 603 553 604 assert(Pyd_Module_p(modulename) !is null, "Must initialize module before wrapping classes."); 554 char[] module_name = toString( PyModule_GetName(Pyd_Module_p(modulename)));605 char[] module_name = toString(python.PyModule_GetName(Pyd_Module_p(modulename))); 555 606 556 607 ////////////////// 557 608 // Basic values // 558 609 ////////////////// 559 type.ob_type = PyType_Type_p();610 type.ob_type = python.PyType_Type_p(); 560 611 type.tp_basicsize = (wrapped_class_object!(T)).sizeof; 561 612 type.tp_doc = (docstring ~ \0).ptr; 562 type.tp_flags = Py_TPFLAGS_DEFAULT |Py_TPFLAGS_BASETYPE;613 type.tp_flags = python.Py_TPFLAGS_DEFAULT | python.Py_TPFLAGS_BASETYPE; 563 614 //type.tp_repr = &wrapped_repr!(T).repr; 564 615 type.tp_methods = wrapped_method_list!(T).ptr; … … 583 634 //////////////////////// 584 635 // Numerical operator overloads 585 if ( wrapped_class_as_number!(T) !=PyNumberMethods.init) {586 type.tp_as_number = & wrapped_class_as_number!(T);636 if (pyd.op_wrap.wrapped_class_as_number!(T) != python.PyNumberMethods.init) { 637 type.tp_as_number = &pyd.op_wrap.wrapped_class_as_number!(T); 587 638 } 588 639 // Sequence operator overloads 589 if ( wrapped_class_as_sequence!(T) !=PySequenceMethods.init) {590 type.tp_as_sequence = & wrapped_class_as_sequence!(T);640 if (pyd.op_wrap.wrapped_class_as_sequence!(T) != python.PySequenceMethods.init) { 641 type.tp_as_sequence = &pyd.op_wrap.wrapped_class_as_sequence!(T); 591 642 } 592 643 // Mapping operator overloads 593 if ( wrapped_class_as_mapping!(T) !=PyMappingMethods.init) {594 type.tp_as_mapping = & wrapped_class_as_mapping!(T);644 if (pyd.op_wrap.wrapped_class_as_mapping!(T) != python.PyMappingMethods.init) { 645 type.tp_as_mapping = &pyd.op_wrap.wrapped_class_as_mapping!(T); 595 646 } 596 647 … … 607 658 // opCmp 608 659 static if (is(typeof(&T.opCmp))) { 609 type.tp_compare = & opcmp_wrap!(T).func;660 type.tp_compare = &pyd.op_wrap.opcmp_wrap!(T).func; 610 661 } 611 662 // opCall … … 637 688 throw new Exception("Couldn't ready wrapped type!"); 638 689 } 639 Py_INCREF(cast(PyObject*)&type);640 PyModule_AddObject(Pyd_Module_p(modulename), (name~\0).ptr, cast(PyObject*)&type);690 python.Py_INCREF(cast(PyObject*)&type); 691 python.PyModule_AddObject(Pyd_Module_p(modulename), (name~\0).ptr, cast(PyObject*)&type); 641 692 642 693 is_wrapped!(T) = true; … … 647 698 } 648 699 } 649 700 } 650 701 //////////////// 651 702 // DOCSTRINGS // trunk/infrastructure/pyd/ctor_wrap.d
r101 r118 67 67 // This template accepts a tuple of function pointer types, which each describe 68 68 // a ctor of T, and uses them to wrap a Python tp_init function. 69 template wrapped_ctors(T, C ...) { 69 template wrapped_ctors(/*T,*/ C ...) { 70 alias shim_class T; 70 71 alias wrapped_class_object!(T) wrap_object; 71 72 72 73 extern(C) 73 int init_func(PyObject* self, PyObject* args, PyObject* kwds) {74 static int init_func(PyObject* self, PyObject* args, PyObject* kwds) { 74 75 int len = PyObject_Length(args); 75 76 … … 92 93 return -1; 93 94 } 95 alias typeof(fn) dg_t; 96 mixin applyPyTupleToDelegate!(dg_t); 94 97 T t = applyPyTupleToDelegate(fn, args); 95 98 if (t is null) { trunk/infrastructure/pyd/make_wrapper.d
r110 r118 182 182 "class wrapper : T {\n"~ 183 183 " mixin OverloadShim;\n"~ 184 class_decls!(0, Params)~"\n"~184 pyd.make_wrapper.class_decls!(0, Params)~"\n"~ 185 185 // op_shims!(0, T)~ 186 186 "}\n"; trunk/infrastructure/pyd/pyd.d
r100 r118 44 44 import pyd.iteration; 45 45 } 46 import pyd.make_wrapper; 46 47 } 47 48 trunk/infrastructure/pyd/struct_wrap.d
r103 r118 63 63 } 64 64 65 template Member(char[] realname, char[] docstring="") { 66 alias Member!(realname, realname, docstring) Member; 65 struct Member(char[] realname) { 66 mixin _Member!(realname, realname, ""); 67 } 68 struct Member(char[] realname, char[] docstring) { 69 mixin _Member!(realname, realname, docstring); 67 70 } 68 71 struct Member(char[] realname, char[] name, char[] docstring) { 69 static void call(T, dummy) () { 72 mixin _Member!(realname, name, docstring); 73 } 74 template _Member(char[] realname, char[] name, char[] docstring) { 75 static const bool needs_shim = false; 76 static void call(T) () { 70 77 pragma(msg, "struct.member: " ~ name); 71 78 static PyGetSetDef empty = {null, null, null, null, null};
