| 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 python; |
|---|
| 25 |
|
|---|
| 26 |
import pyd.class_wrap; |
|---|
| 27 |
import pyd.dg_convert; |
|---|
| 28 |
import pyd.exception; |
|---|
| 29 |
import pyd.func_wrap; |
|---|
| 30 |
import pyd.lib_abstract : |
|---|
| 31 |
ReturnType, |
|---|
| 32 |
ParameterTypeTuple, |
|---|
| 33 |
ToString |
|---|
| 34 |
; |
|---|
| 35 |
|
|---|
| 36 |
template T(A ...) { |
|---|
| 37 |
alias A T; |
|---|
| 38 |
} |
|---|
| 39 |
template opFuncs() { |
|---|
| 40 |
alias T!("opNeg", "opPos", "opCom", "opAdd", "opSub", "opMul", "opDiv", "opMod", "opAnd", "opOr", "opXor", "opShl", "opShr", "opCat", "opAddAssign", "opSubAssign", "opMulAssign", "opDivAssign", "opModAssign", "opAndAssign", "opOrAssign", "opXorAssign", "opShlAssign", "opShrAssign", "opCatAssign", "opIn_r", "opCmp", "opCall", "opApply", "opIndex", "opIndexAssign", "opSlice", "opSliceAssign", "length") opFuncs; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
template funcTypes() { |
|---|
| 44 |
alias T!("UNI", "UNI", "UNI", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "BIN", "UNSUPPORTED", "CMP", "CALL", "APPLY", "INDEX", "INDEXASS", "SLICE", "SLICEASS", "LEN") funcTypes; |
|---|
| 45 |
} |
|---|
| 46 |
template pyOpFuncs() { |
|---|
| 47 |
alias T!("__neg__", "__pos__", "__invert__", "__add__", "__sub__", "__mul__", "__div__", "__mod__", "__and__", "__or__", "__xor__", "__lshift__", "__rshift__", "__add__", "__iadd__", "__isub__", "__imul__", "__idiv__", "__imod__", "__iand__", "__ior__", "__ixor__", "__ilshift__", "__irshift__", "__iadd__", "UNSUPPORTED", "__cmp__", "__call__", "__iter__", "__getitem__", "__setitem__", "UNSUPPORTED", "UNSUPPORTED", "__len__") pyOpFuncs; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
template opFunc(uint i) { |
|---|
| 51 |
const char[] opFunc = opFuncs!()[i]; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
template funcType(uint i) { |
|---|
| 55 |
const char[] funcType = funcTypes!()[i]; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
template pyOpFunc(uint i) { |
|---|
| 59 |
const char[] pyOpFunc = pyOpFuncs!()[i]; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
template op_shim(uint i) { |
|---|
| 63 |
static if (funcType!(i) == "UNI" || funcType!(i) == "BIN" || funcType!(i) == "CMP" || funcType!("CALL")) { |
|---|
| 64 |
const char[] op_shim = |
|---|
| 65 |
" ReturnType!(T."~opFunc!(i)~") "~opFunc!(i)~"(ParameterTypeTuple!(T."~opFunc!(i)~") t) {\n" |
|---|
| 66 |
" return __pyd_get_overload!(\""~opFunc!(i)~"\", typeof(&T."~opFunc!(i)~")).func(\""~pyOpFunc!(i)~"\", t);\n" |
|---|
| 67 |
" }\n"; |
|---|
| 68 |
} else static if (funcType!(i) == "APPLY") { |
|---|
| 69 |
const char[] op_shim = |
|---|
| 70 |
" int opApply(ParameterTypeTuple!(T.opApply)[0] dg) {\n" |
|---|
| 71 |
" return __pyd_apply_wrapper(dg);\n" |
|---|
| 72 |
" }\n"; |
|---|
| 73 |
} else static assert(false, "Unsupported operator overload " ~ opFunc!(i)); |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
template op_shims(uint i, T) { |
|---|
| 77 |
static if (i < opFuncs!().length) { |
|---|
| 78 |
static if (is(typeof(mixin("&T."~opFunc!(i)))) && opFunc!(i) != "UNSUPPORTED") { |
|---|
| 79 |
const char[] op_shims = op_shim!(i) ~ op_shims!(i+1, T); |
|---|
| 80 |
} else { |
|---|
| 81 |
const char[] op_shims = op_shims(i+1, T); |
|---|
| 82 |
} |
|---|
| 83 |
} else { |
|---|
| 84 |
const char[] op_shims = ""; |
|---|
| 85 |
} |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
template OverloadShim() { |
|---|
| 89 |
// If this is actually an instance of a Python subclass, return the |
|---|
| 90 |
// PyObject associated with the object. Otherwise, return null. |
|---|
| 91 |
PyObject* __pyd_get_pyobj() { |
|---|
| 92 |
PyObject** _pyobj = cast(void*)this in wrapped_gc_objects; |
|---|
| 93 |
PyTypeObject** _pytype = this.classinfo in wrapped_classes; |
|---|
| 94 |
if (_pyobj is null || _pytype is null || (*_pyobj).ob_type != *_pytype) { |
|---|
| 95 |
return *_pyobj; |
|---|
| 96 |
} else { |
|---|
| 97 |
return null; |
|---|
| 98 |
} |
|---|
| 99 |
} |
|---|
| 100 |
template __pyd_abstract_call(fn_t) { |
|---|
| 101 |
ReturnType!(fn_t) func(T ...) (char[] name, T t) { |
|---|
| 102 |
PyObject* _pyobj = this.__pyd_get_pyobj(); |
|---|
| 103 |
if (_pyobj !is null) { |
|---|
| 104 |
PyObject* method = PyObject_GetAttrString(_pyobj, (name ~ \0).ptr); |
|---|
| 105 |
if (method is null) handle_exception(); |
|---|
| 106 |
auto pydg = PydCallable_AsDelegate!(fn_to_dg!(fn_t))(method); |
|---|
| 107 |
Py_DECREF(method); |
|---|
| 108 |
return pydg(t); |
|---|
| 109 |
} else { |
|---|
| 110 |
PyErr_SetNone(PyExc_NotImplementedError); |
|---|
| 111 |
handle_exception(); |
|---|
| 112 |
//return ReturnType!(fn_t).init; |
|---|
| 113 |
} |
|---|
| 114 |
} |
|---|
| 115 |
} |
|---|
| 116 |
template __pyd_get_overload(string realname, fn_t) { |
|---|
| 117 |
ReturnType!(fn_t) func(T ...) (string name, T t) { |
|---|
| 118 |
PyObject* _pyobj = this.__pyd_get_pyobj(); |
|---|
| 119 |
if (_pyobj !is null) { |
|---|
| 120 |
// If this object's type is not the wrapped class's type (that is, |
|---|
| 121 |
// if this object is actually a Python subclass of the wrapped |
|---|
| 122 |
// class), then call the Python object. |
|---|
| 123 |
PyObject* method = PyObject_GetAttrString(_pyobj, (name ~ \0).ptr); |
|---|
| 124 |
if (method is null) handle_exception(); |
|---|
| 125 |
auto pydg = PydCallable_AsDelegate!(fn_to_dg!(fn_t))(method); |
|---|
| 126 |
Py_DECREF(method); |
|---|
| 127 |
return pydg(t); |
|---|
| 128 |
} else { |
|---|
| 129 |
mixin("return super."~realname~"(t);"); |
|---|
| 130 |
} |
|---|
| 131 |
} |
|---|
| 132 |
} |
|---|
| 133 |
int __pyd_apply_wrapper(dg_t) (dg_t dg) { |
|---|
| 134 |
alias ParameterTypeTuple!(dg_t)[0] arg_t; |
|---|
| 135 |
const uint args = ParameterTypeTuple!(dg_t).length; |
|---|
| 136 |
PyObject* _pyobj = this.__pyd_get_pyobj(); |
|---|
| 137 |
if (_pyobj !is null) { |
|---|
| 138 |
PyObject* iter = PyObject_GetIter(_pyobj); |
|---|
| 139 |
if (iter is null) handle_exception(); |
|---|
| 140 |
PyObject* item; |
|---|
| 141 |
int result = 0; |
|---|
| 142 |
|
|---|
| 143 |
item = PyIter_Next(iter); |
|---|
| 144 |
while (item) { |
|---|
| 145 |
static if (args == 1 && is(arg_t == PyObject*)) { |
|---|
| 146 |
result = dg(item); |
|---|
| 147 |
} else { |
|---|
| 148 |
if (PyTuple_Check(item)) { |
|---|
| 149 |
result = applyPyTupleToDelegate(dg, item); |
|---|
| 150 |
} else { |
|---|
| 151 |
static if (args == 1) { |
|---|
| 152 |
arg_t t = d_type!(typeof(arg_t))(item); |
|---|
| 153 |
result = dg(t); |
|---|
| 154 |
} else { |
|---|
| 155 |
throw new Exception("Tried to override opApply with wrong number of args..."); |
|---|
| 156 |
} |
|---|
| 157 |
} |
|---|
| 158 |
} |
|---|
| 159 |
Py_DECREF(item); |
|---|
| 160 |
if (result) break; |
|---|
| 161 |
item = PyIter_Next(iter); |
|---|
| 162 |
} |
|---|
| 163 |
Py_DECREF(iter); |
|---|
| 164 |
handle_exception(); |
|---|
| 165 |
return result; |
|---|
| 166 |
} else { |
|---|
| 167 |
return super.opApply(dg); |
|---|
| 168 |
} |
|---|
| 169 |
} |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
template class_decls(uint i, Params...) { |
|---|
| 173 |
static if (i < Params.length) { |
|---|
| 174 |
const char[] class_decls = Params[i].shim!(i) ~ class_decls!(i+1, Params); |
|---|
| 175 |
} else { |
|---|
| 176 |
const char[] class_decls = ""; |
|---|
| 177 |
} |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
template make_wrapper(T, Params...) { |
|---|
| 181 |
const char[] cls = |
|---|
| 182 |
"class wrapper : T {\n"~ |
|---|
| 183 |
" mixin OverloadShim;\n"~ |
|---|
| 184 |
pyd.make_wrapper.class_decls!(0, Params)~"\n"~ |
|---|
| 185 |
// op_shims!(0, T)~ |
|---|
| 186 |
"}\n"; |
|---|
| 187 |
pragma(msg, cls); |
|---|
| 188 |
mixin(cls); |
|---|
| 189 |
} |
|---|