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