root/trunk/raw_html/struct_wrapping.html

Revision 113, 5.1 kB (checked in by KirkMcDonald, 1 year ago)

* Improved Pyd's handling of arrays.
* Added Repr
* Added arraytest

Line 
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3 <head>
4     <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
5     <link href="pyd.css" rel="stylesheet" type="text/css">
6     <title>Wrapping structs</title>
7 </head>
8
9 <body>
10 %(nav)s
11 <div id="content">
12
13 <h1>Struct wrapping</h1>
14
15 <p>Wrapping D's structs is similar to wrapping classes. In fact, many of the operations are identical.</p>
16
17 <p><code>void wrap_struct(<span class="t_arg">T</span>, char[] <span class="t_arg">structname</span> = symbolnameof!(T), <span class="t_arg">Params</span>...) ();</code></p>
18     <ul>
19     <li><span class="t_arg">T</span> is the struct being wrapped.</li>
20     <li><span class="t_arg">structname</span> is the name of the struct as it will appear in Python.</li>
21     <li><span class="t_arg">Params</span> is a series of struct types (defined below), which define the various members of the struct.</li>
22     </ul>
23
24 <p>As with calls to <a href="class_wrapping.html"><code>wrap_class</code></a>, calls to <code>wrap_struct</code> must occur <em>after</em> calling <code>module_init</code>.</p>
25
26 <p>To expose the data members, member functions, and properties of the struct, you must pass a series of struct template instantiations to <code>wrap_struct</code>.</p>
27
28 <dl>
29 <dt><code>struct Member(char[] <span class="t_arg">realname</span>, char[] <span class="t_arg">name</span>=realname);</code></dt>
30 <dd>This exposes a data member of the struct to Python. The member must be a <a href="conversion.html">convertible type</a>. The <span class="t_arg">realname</span> is the member's actual name. <span class="t_arg">name</span> is the name of the data member as it will be used in Python. This defaults to <span class="t_arg">realname</span>.</dd>
31
32 <dt><code>struct Def(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&amp;fn));</code></dt>
33 <dd>This wraps a member function of the struct. It is in fact exactly the same <code>Def</code> struct template used to <a href="class_wrapping.html">wrap class methods</a>, including the lack of support for default arguments.</dd>
34
35 <dt><code>struct StaticDef(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&amp;fn), uint <span class="t_arg">MIN_ARGS</span> = minArgs!(fn));</code></dt>
36 <dd>This wraps a static member function of the struct. It is the same <code>StaticDef</code> struct template used to wrap static class member functions, and also includes support for default arguments.</dd>
37
38 <dt><code>struct Property(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), bool <span class="t_arg">RO</span> = false);</code></dt>
39 <dd>This wraps a property. It is the same <code>Property</code> struct template used to <a href="class_wrapping.html">wrap class properties</a>.</dd>
40
41 <dt><code>struct Repr(alias <span class="t_arg">fn</span>);</code></dt>
42 <dd>This allows you to expose a member function of the struct as the Python type's <code>__repr__</code> function. The member function must have the signature <code>char[] function()</code>. It is the same <code>Repr</code> struct template used when <a href="class_wrapping.html">wrapping classes</a>.</dd>
43
44 <dt><code>struct Iter(<span class="t_arg">iter_t</span>);</code></dt>
45 <dd>This allows the user to specify a different overload of opApply than the default. (The default is always the one that is lexically first.) It is the same <code>Iter</code> struct template used in <a href="class_wrapping.html">class wrapping</a>.</dd>
46
47 <dt><code>struct AltIter(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">iter_t</span> = <i>implementationDetail</i>);</code></dt>
48 <dd>This wraps alternate iterator methods as Python methods that return iterator objects. It is the same <code>AltIter</code> struct template used in <a href="class_wrapping.html">class wrapping</a>.</dd>
49 </dl>
50
51 <!--<p><i>(Future enhancements: Support for struct ctors.)</i></p>-->
52
53 <p>The <code>is_wrapped</code> template is available for wrapped structs, just like it is for wrapped classes.</p>
54
55 <p>It is important to note that wrapping a struct <code>S</code> makes both <code>S</code> itself and <code>S*</code> available as <a href="conversion.html">convertible types</a>.</p>
56
57 <h3><a class="anchor" name="opwrap">Automatic operator overloading</a></h3>
58
59 <p>Support for operator overloading in structs is identical to that available for classes.</p>
60
61 <h3><a class="anchor" name="inherit">Inheritance</a></h3>
62
63 <p>D does not support struct inheritance. Therefore, Pyd does not provide any support for struct inheritance. However, the Python type wrapping the D struct can be subclassed from within Python. Users should not expect polymorphic behavior if they attempt to pass instances of any subclasses back to D.</p>
64
65 <h3><a class="anchor" name="examples">Examples</a></h3>
66
67 <p><i>(Todo.)</i></p>
68
69 </div>
70
71 </body>
72 </html>
Note: See TracBrowser for help on using the browser.