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.make_wrapper;
23 
24 import deimos.python.Python;
25 
26 import pyd.util.typeinfo;
27 import pyd.util.replace: Replace;
28 import pyd.references;
29 import pyd.class_wrap;
30 import pyd.exception;
31 import pyd.func_wrap;
32 import std.traits;
33 
34 template OverloadShim() {
35     // If this is actually an instance of a Python subclass, return the
36     // PyObject associated with the object. Otherwise, return null.
37     PyObject* __pyd_get_pyobj() const {
38         auto py = cast(PyObject*) get_python_reference(this);
39         PyTypeObject** _pytype = this.classinfo in wrapped_classes;
40         if (_pytype is null || py.ob_type != *_pytype) {
41             return null;
42         } else {
43             return py;
44         }
45     }
46     template __pyd_abstract_call(fn_t) {
47         ReturnType!(fn_t) func(T ...) (string name, T t) {
48             PyObject* _pyobj = this.__pyd_get_pyobj();
49             if (_pyobj !is null) {
50                 PyObject* method = PyObject_GetAttrString(_pyobj, (name ~ "\0").dup.ptr);
51                 if (method is null) handle_exception();
52                 auto pydg = PydCallable_AsDelegate!(fn_to_dg!(fn_t))(method);
53                 Py_DECREF(method);
54                 return pydg(t);
55             } else {
56                 PyErr_SetNone(PyExc_NotImplementedError);
57                 handle_exception();
58                 //return ReturnType!(fn_t).init;
59             }
60         }
61     }
62     template __pyd_get_overload(string realname, fn_t) {
63         enum attrs = functionAttributes!fn_t;
64             mixin(Replace!(q{
65         ReturnType!(fn_t) func(T ...) (string name, T t) $constness $attrs {
66             PyObject* _pyobj = this.__pyd_get_pyobj();
67             if (_pyobj !is null) {
68                 // If this object's type is not the wrapped class's type (that is,
69                 // if this object is actually a Python subclass of the wrapped
70                 // class), then call the Python object.
71                 PyObject* method = PyObject_GetAttrString(_pyobj, (name ~ "\0").dup.ptr);
72                 if (method is null) handle_exception();
73                 auto pydg = PydCallable_AsDelegate!(fn_to_dg!(fn_t))(method);
74                 Py_DECREF(method);
75                 return pydg(t);
76             } else {
77                 return super.$realname(t);
78             }
79         }
80         }, "$constness",
81             isImmutableFunction!fn_t ? "immutable" :
82             isConstFunction!fn_t ? "const" : "",
83             "$attrs", attrs_to_string(attrs),
84             "$realname", realname));
85     }
86     int __pyd_apply_wrapper(dg_t) (dg_t dg) {
87         alias ParameterTypeTuple!(dg_t)[0] arg_t;
88         const uint args = ParameterTypeTuple!(dg_t).length;
89         PyObject* _pyobj = this.__pyd_get_pyobj();
90         if (_pyobj !is null) {
91             PyObject* iter = PyObject_GetIter(_pyobj);
92             if (iter is null) handle_exception();
93             PyObject* item;
94             int result = 0;
95 
96             item = PyIter_Next(iter);
97             while (item) {
98                 static if (args == 1 && is(arg_t == PyObject*)) {
99                     result = dg(item);
100                 } else {
101                     if (PyTuple_Check(item)) {
102                         result = applyPyTupleToDelegate(dg, item);
103                     } else {
104                         static if (args == 1) {
105                             arg_t t = python_to_d!(typeof(arg_t))(item);
106                             result = dg(t);
107                         } else {
108                             throw new Exception("Tried to override opApply with wrong number of args...");
109                         }
110                     }
111                 }
112                 Py_DECREF(item);
113                 if (result) break;
114                 item = PyIter_Next(iter);
115             }
116             Py_DECREF(iter);
117             handle_exception();
118             return result;
119         } else {
120             return super.opApply(dg);
121         }
122     }
123 }
124 
125 template class_decls(uint i, T, Params...) {
126     static if (i < Params.length) {
127         enum string class_decls = Params[i].shim!(i,T) ~ class_decls!(i+1, T, Params);
128     } else {
129         enum string class_decls = "";
130     }
131 }
132 
133 template make_wrapper(T, Params...) {
134     enum string cls =
135     "class wrapper : T {\n"~
136     "    mixin OverloadShim;\n"~
137     pyd.make_wrapper.class_decls!(0, T, Params)~"\n"~
138     "}\n";
139     mixin(cls);
140 }
141