root/trunk/infrastructure/pyd/ctor_wrap.d

Revision 122, 4.2 kB (checked in by KirkMcDonald, 1 year ago)

* Un-broke Init. (Oops.)
* Reverted some symbol-length-shortening code, which caused the above problem.

Line 
1 /*
2 Copyright 2006, 2007 Kirk McDonald
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22 module pyd.ctor_wrap;
23
24 import python;
25 import pyd.class_wrap;
26 import pyd.exception;
27 import pyd.func_wrap;
28 import pyd.make_object;
29 import pyd.lib_abstract :
30     prettynameof,
31     ParameterTypeTuple
32 ;
33 //import meta.Nameof;
34 //import std.traits;
35
36 T call_ctor(T, Tu ...)(Tu t) {
37     return new T(t);
38 }
39
40 // The default __init__ method calls the class's zero-argument constructor.
41 template wrapped_init(T) {
42     extern(C)
43     int init(PyObject* self, PyObject* args, PyObject* kwds) {
44         return exception_catcher({
45             WrapPyObject_SetObj(self, new T);
46             return 0;
47         });
48     }
49 }
50
51 // The __init__ slot for wrapped structs. T is of the type of a pointer to the
52 // struct.
53 template wrapped_struct_init(T) {
54     extern(C)
55     int init(PyObject* self, PyObject* args, PyObject* kwds) {
56         return exception_catcher({
57             static if (is(T S : S*)) {
58                 pragma(msg, "wrapped_struct_init, S is '" ~ prettynameof!(S) ~ "'");
59                 T t = new S;
60                 WrapPyObject_SetObj(self, t);
61             }
62             return 0;
63         });
64     }
65 }
66
67 //import std.stdio;
68 // This template accepts a tuple of function pointer types, which each describe
69 // a ctor of T, and  uses them to wrap a Python tp_init function.
70 template wrapped_ctors(T, C ...) {
71     //alias shim_class T;
72     alias wrapped_class_object!(T) wrap_object;
73
74     extern(C)
75     static int init_func(PyObject* self, PyObject* args, PyObject* kwds) {
76         int len = PyObject_Length(args);
77
78         return exception_catcher({
79             //writefln("in init_func: len=%s, T=%s, C.length=%s", len, typeid(T), C.length);
80             // Default ctor
81             static if (is(typeof(new T))) {
82                 if (len == 0) {
83                     WrapPyObject_SetObj(self, new T);
84                     return 0;
85                 }
86             }
87             // find another Ctor
88             C c;
89             foreach(i, arg; c) {
90                 alias ParameterTypeTuple!(typeof(arg)) Ctor;
91                 //writefln("  init_func: i=%s, Ctor.length=%s, Ctor=%s", i, Ctor.length, typeid(typeof(arg)));
92                 if (Ctor.length == len) {
93                     auto fn = &call_ctor!(T, ParameterTypeTuple!(typeof(arg)));
94                     if (fn is null) {
95                         PyErr_SetString(PyExc_RuntimeError, "Couldn't get pointer to class ctor redirect.");
96                         return -1;
97                     }
98                     alias typeof(fn) dg_t;
99                     //mixin applyPyTupleToDelegate!(dg_t) A;
100                     T t = applyPyTupleToDelegate(fn, args);
101                     if (t is null) {
102                         PyErr_SetString(PyExc_RuntimeError, "Class ctor redirect didn't return a class instance!");
103                         return -1;
104                     }
105                     WrapPyObject_SetObj(self, t);
106                     return 0;
107                 }
108             }
109             // No ctor found
110             PyErr_SetString(PyExc_TypeError, "Unsupported number of constructor arguments.");
111             return -1;
112         });
113     }
114 }
Note: See TracBrowser for help on using the browser.