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     static if(countUntil(mode, "r") != -1) {
59         static if(PropertyParts.length != 0) {
60         }
61         extern(C)
62             PyObject* get(PyObject* self, void* closure) {
63             return exception_catcher(delegate PyObject*() {
64                 GT t = get_d_reference!GT(self);
65                 mixin("return d_to_python(t."~name~");");
66             });
67         }
68     }
69 
70     static if(countUntil(mode, "w") != -1) {
71         extern(C)
72         int set(PyObject* self, PyObject* value, void* closure) {
73             return exception_catcher(delegate int() {
74                 T t = get_d_reference!T(self);
75                 mixin("t."~name~" = python_to_d!(M)(value);");
76                 return 0;
77             });
78         }
79     }
80 }
81 
82 /**
83 Wrap a member variable of a class or struct.
84 
85 Params:
86 name = The name of the member to wrap
87 Options = Optional parameters. Takes Docstring!(docstring), PyName!(pyname),
88 and Mode!(mode)
89 pyname = The name of the member as it will appear in Python. Defaults to name
90 mode = specifies whether this member is readable, writable. possible values
91 are "r", "w", "rw". Defaults to "rw".
92 docstring = The function's docstring. Defaults to "".
93 */
94 struct Member(string name, Options...) {
95     alias Args!("","", name, "rw",Options) args;
96     mixin _Member!(name, args.pyname, args.mode, args.docstring);
97 }
98 
99 template _Member(string realname, string pyname, string mode, string docstring, parts...) {
100     static const bool needs_shim = false;
101     static void call(string classname, T) () {
102         import std.algorithm: countUntil;
103 
104         static PyGetSetDef empty = {null, null, null, null, null};
105         alias wrapped_prop_list!(T) list;
106         list[$-1].name = (pyname ~ "\0").dup.ptr;
107         static if(countUntil(mode, "r") != -1) {
108             list[$-1].get = &wrapped_member!(T, realname, mode, parts).get;
109         }
110         static if(countUntil(mode, "w") != -1) {
111             list[$-1].set = &wrapped_member!(T, realname, mode, parts).set;
112         }
113         list[$-1].doc = (docstring~"\0").dup.ptr;
114         list[$-1].closure = null;
115         list ~= empty;
116         PydTypeObject!(T).tp_getset = list.ptr;
117     }
118 }
119 
120 /// Wrap a struct.
121 alias wrap_class wrap_struct;
122