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 23 /** 24 Contains utilities for wrapping D structs. 25 */ 26 module pyd.struct_wrap; 27 28 import std.traits; 29 import deimos.python.Python; 30 31 import pyd.util.typeinfo; 32 import pyd.references; 33 import pyd.def; 34 import pyd.class_wrap; 35 import pyd.exception; 36 import pyd.make_object; 37 38 // It is intended that all of these templates accept a pointer-to-struct type 39 // as a template parameter, rather than the struct type itself. 40 41 template wrapped_member(T, string name, string mode, PropertyParts...) { 42 import std.algorithm: countUntil; 43 44 alias PydTypeObject!(T) type; 45 alias wrapped_class_object!(T) obj; 46 static if(PropertyParts.length != 0) { 47 alias PropertyParts[0] ppart0; 48 alias ppart0.Type M; 49 // const setters make no sense. getters though.. 50 static if(ppart0.isgproperty) { 51 alias ApplyConstness2!(T,constness!(FunctionTypeOf!(ppart0.GetterFn))) 52 GT; 53 } 54 }else { 55 alias T GT; 56 mixin("alias typeof(T."~name~") M;"); 57 } 58 59 static if(countUntil(mode, "r") != -1) { 60 61 enum isStructInStruct = isPointer!GT && 62 is(PointerTarget!GT == struct) && 63 is(typeof(mixin("GT.init."~name)) == struct); 64 65 extern(C) 66 PyObject* get(PyObject* self, void* closure) { 67 return exception_catcher(delegate PyObject*() { 68 GT t = get_d_reference!GT(self); 69 static if(isStructInStruct) { 70 mixin("return d_to_python(&t."~name~");"); 71 }else{ 72 mixin("return d_to_python(t."~name~");"); 73 } 74 }); 75 } 76 } 77 78 static if(countUntil(mode, "w") != -1) { 79 extern(C) 80 int set(PyObject* self, PyObject* value, void* closure) { 81 return exception_catcher(delegate int() { 82 T t = get_d_reference!T(self); 83 mixin("t."~name~" = python_to_d!(M)(value);"); 84 return 0; 85 }); 86 } 87 } 88 } 89 90 /** 91 Wrap a member variable of a class or struct. 92 93 $(BR) 94 Template Parameters: 95 $(BR) 96 name = The name of the member to wrap 97 $(BR) 98 Options = Optional parameters. Takes Docstring!(docstring), PyName!(pyname), 99 and Mode!(mode) 100 $(BR) 101 pyname = The name of the member as it will appear in Python. Defaults to name 102 $(BR) 103 mode = specifies whether this member is readable, writable. possible values 104 are "r", "w", "rw". Defaults to "rw". 105 $(BR) 106 docstring = The function's docstring. Defaults to "". 107 */ 108 struct Member(string name, Options...) { 109 alias Args!("","", name, "rw",Options) args; 110 mixin _Member!(name, args.pyname, args.mode, args.docstring); 111 } 112 113 template _Member(string realname, string pyname, string mode, string docstring, parts...) { 114 static const bool needs_shim = false; 115 template shim(size_t i, T) { 116 enum shim = ""; 117 } 118 static void call(string classname, T) () { 119 import std.algorithm: countUntil; 120 121 static PyGetSetDef empty = {null, null, null, null, null}; 122 alias wrapped_prop_list!(T) list; 123 list[$-1].name = (pyname ~ "\0").dup.ptr; 124 static if(countUntil(mode, "r") != -1) { 125 list[$-1].get = &wrapped_member!(T, realname, mode, parts).get; 126 } 127 static if(countUntil(mode, "w") != -1) { 128 list[$-1].set = &wrapped_member!(T, realname, mode, parts).set; 129 } 130 list[$-1].doc = (docstring~"\0").dup.ptr; 131 list[$-1].closure = null; 132 list ~= empty; 133 PydTypeObject!(T).tp_getset = list.ptr; 134 } 135 } 136 137 /// Wrap a struct. 138 alias wrap_class wrap_struct; 139