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 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 Params:
94 name = The name of the member to wrap
95 Options = Optional parameters. Takes Docstring!(docstring), PyName!(pyname),
96 and Mode!(mode)
97 pyname = The name of the member as it will appear in Python. Defaults to name
98 mode = specifies whether this member is readable, writable. possible values
99 are "r", "w", "rw". Defaults to "rw".
100 docstring = The function's docstring. Defaults to "".
101 */
102 struct Member(string name, Options...) {
103     alias Args!("","", name, "rw",Options) args;
104     mixin _Member!(name, args.pyname, args.mode, args.docstring);
105 }
106 
107 template _Member(string realname, string pyname, string mode, string docstring, parts...) {
108     static const bool needs_shim = false;
109     static void call(string classname, T) () {
110         import std.algorithm: countUntil;
111 
112         static PyGetSetDef empty = {null, null, null, null, null};
113         alias wrapped_prop_list!(T) list;
114         list[$-1].name = (pyname ~ "\0").dup.ptr;
115         static if(countUntil(mode, "r") != -1) {
116             list[$-1].get = &wrapped_member!(T, realname, mode, parts).get;
117         }
118         static if(countUntil(mode, "w") != -1) {
119             list[$-1].set = &wrapped_member!(T, realname, mode, parts).set;
120         }
121         list[$-1].doc = (docstring~"\0").dup.ptr;
122         list[$-1].closure = null;
123         list ~= empty;
124         PydTypeObject!(T).tp_getset = list.ptr;
125     }
126 }
127 
128 /// Wrap a struct.
129 alias wrap_class wrap_struct;
130