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 std.traits;
25 import std.exception: enforce;
26 import util.typelist: Join;
27 import util.typeinfo;
28 import util.replace: Replace;
29 import deimos.python.Python;
30 import pyd.references;
31 import pyd.class_wrap;
32 import pyd.exception;
33 import pyd.func_wrap;
34 import pyd.make_object;
35 
36 template call_ctor(T, init) {
37     alias ParameterTypeTuple!(init.Inner!T.FN) paramtypes;
38     alias ParameterIdentifierTuple!(init.Inner!T.FN) paramids;
39     //https://issues.dlang.org/show_bug.cgi?id=17192
40     //alias ParameterDefaultValueTuple!(init.Inner!T.FN) dfs;
41     import util.typeinfo : WorkaroundParameterDefaults;
42     alias dfs = WorkaroundParameterDefaults!(init.Inner!T.FN);
43     enum params = getparams!(init.Inner!T.FN, "paramtypes", "dfs");
44     mixin(Replace!(q{
45     T func($params) {
46         return new $T($ids);
47     }
48     },"$params",params, "$ids", Join!(",",paramids),
49     "$T", (is(T == class)?"T":"PointerTarget!T")));
50 }
51 
52 // The default __init__ method calls the class's zero-argument constructor.
53 template wrapped_init(T) {
54     extern(C)
55     int init(PyObject* self, PyObject* args, PyObject* kwds) {
56         return exception_catcher({
57             set_pyd_mapping(self, new T);
58             return 0;
59         });
60     }
61 }
62 
63 // The __init__ slot for wrapped structs.
64 template wrapped_struct_init(T) if (is(T == struct)){
65     extern(C)
66     int init(PyObject* self, PyObject* args, PyObject* kwds) {
67         return exception_catcher({
68                 T* t = new T;
69                 set_pyd_mapping(self, t);
70                 return 0;
71         });
72     }
73 }
74 
75 //import std.stdio;
76 // This template accepts a tuple of function pointer types, which each describe
77 // a ctor of T, and  uses them to wrap a Python tp_init function.
78 template wrapped_ctors(string classname, T,Shim, C ...)
79 if(is(T == class) || (isPointer!T && is(PointerTarget!T == struct))) {
80     //alias shim_class T;
81     alias wrapped_class_object!(T) wrap_object;
82     alias NewParamT!T U;
83 
84     extern(C)
85     static int func(PyObject* self, PyObject* args, PyObject* kwargs) {
86         Py_ssize_t arglen = PyObject_Length(args);
87         Py_ssize_t kwlen = kwargs is null?-1:PyObject_Length(kwargs);
88         enforce(arglen != -1);
89         Py_ssize_t len = arglen + ((kwlen == -1) ? 0:kwlen);
90 
91         return exception_catcher({
92             // Default ctor
93             static if (is(typeof(new U))) {
94                 if (len == 0) {
95                     set_pyd_mapping(self, new U);
96                     return 0;
97                 }
98             }
99             // find another Ctor
100             foreach(i, init; C) {
101                 if (supportsNArgs!(init.Inner!T.FN)(len)) {
102                     alias call_ctor!(T, init).func fn;
103                     T t = applyPyTupleToAlias!(fn, classname)(args, kwargs);
104                     if (t is null) {
105                         PyErr_SetString(PyExc_RuntimeError, "Class ctor redirect didn't return a class instance!");
106                         return -1;
107                     }
108                     set_pyd_mapping(self, t);
109                     return 0;
110                 }
111             }
112             // No ctor found
113             PyErr_SetString(PyExc_TypeError, "Unsupported number of constructor arguments.");
114             return -1;
115         });
116     }
117 }
118