root/trunk/infrastructure/pyd/struct_wrap.d

Revision 120, 3.2 kB (checked in by KirkMcDonald, 1 year ago)

* Pyd now requires D 2.003 or later.
* Pyd now compiles with D 2.003.
* Resolved long-standing string-copying annoyance (thanks to const).

Line 
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.struct_wrap;
23
24 import python;
25
26 import pyd.class_wrap;
27 import pyd.exception;
28 version(Pyd_with_StackThreads) {
29     import pyd.iteration;
30 }
31 import pyd.make_object;
32 import pyd.lib_abstract :
33     symbolnameof
34 ;
35
36 // It is intended that all of these templates accept a pointer-to-struct type
37 // as a template parameter, rather than the struct type itself.
38
39 template wrapped_member(T, string name, _M=void) {
40     alias wrapped_class_type!(T) type;
41     alias wrapped_class_object!(T) obj;
42     static if (is(_M == void)) {
43         mixin("alias typeof(T."~name~") M;");
44     } else {
45         alias _M M;
46     }
47     extern(C)
48     PyObject* get(PyObject* self, void* closure) {
49         return exception_catcher(delegate PyObject*() {
50             T t = (cast(obj*)self).d_obj;
51             mixin("return _py(t."~name~");");
52         });
53     }
54
55     extern(C)
56     int set(PyObject* self, PyObject* value, void* closure) {
57         return exception_catcher(delegate int() {
58             T t = (cast(obj*)self).d_obj;
59             mixin("t."~name~" = d_type!(M)(value);");
60             return 0;
61         });
62     }
63 }
64
65 struct Member(string realname) {
66     mixin _Member!(realname, realname, "");
67 }
68 struct Member(string realname, string docstring) {
69     mixin _Member!(realname, realname, docstring);
70 }
71 struct Member(string realname, string name, string docstring) {
72     mixin _Member!(realname, name, docstring);
73 }
74 template _Member(string realname, string name, string docstring) {
75     static const bool needs_shim = false;
76     static void call(T) () {
77         pragma(msg, "struct.member: " ~ name);
78         static PyGetSetDef empty = {null, null, null, null, null};
79         alias wrapped_prop_list!(T) list;
80         list[length-1].name = (name ~ \0).ptr;
81         list[length-1].get = &wrapped_member!(T, realname).get;
82         list[length-1].set = &wrapped_member!(T, realname).set;
83         list[length-1].doc = (docstring~\0).ptr;
84         list[length-1].closure = null;
85         list ~= empty;
86         wrapped_class_type!(T).tp_getset = list.ptr;
87     }
88 }
89
90 alias wrap_class wrap_struct;
Note: See TracBrowser for help on using the browser.