| 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>Advanced type conversion</title> |
|---|
| 7 |
</head> |
|---|
| 8 |
|
|---|
| 9 |
<body> |
|---|
| 10 |
%(nav)s |
|---|
| 11 |
<div id="content"> |
|---|
| 12 |
|
|---|
| 13 |
<h1>Advanced type conversion</h1> |
|---|
| 14 |
|
|---|
| 15 |
<p>It is frequently useful to extend Pyd's type conversion mechanisms. The usual way to do this is to wrap classes or structs. Pyd has two additional mechanisms for more complex situations.</p> |
|---|
| 16 |
|
|---|
| 17 |
<dl> |
|---|
| 18 |
<dt><code>void d_to_python(<span class="t_arg">dg_t</span>) (<span class="t_arg">dg_t</span> <span class="arg">dg</span>);</code></dt> |
|---|
| 19 |
<dd>This allows the user to define a function for returning a D type to Python. The <span class="arg">dg</span> may be either a function pointer or a delegate. The argument to the function pointer is of the type to convert. The return type of the function pointer can be any convertible type.</dd> |
|---|
| 20 |
|
|---|
| 21 |
<dt><code>void python_to_d(<span class="t_arg">dg_t</span>) (<span class="t_arg">dg_t</span> <span class="arg">dg</span>);</code></dt> |
|---|
| 22 |
<dd>This allows the user to define a function for converting a Python object to a D type. The <span class="arg">dg</span> may be either a function pointer or a delegate. The argument to the function pointer can be any convertible type. The return type of the function pointer is the type to convert.</dd> |
|---|
| 23 |
</dl> |
|---|
| 24 |
|
|---|
| 25 |
<p>Conversion functions defined with either of the above functions only take effect if Pyd's regular type conversion mechanisms fail. This would usually happen if a wrapped function returns or has a parameter of some un-wrapped class or struct type.</p> |
|---|
| 26 |
|
|---|
| 27 |
<h3><a class="anchor" name="examples">Examples</a></h3> |
|---|
| 28 |
|
|---|
| 29 |
<pre class="code"><span class="keyword">import</span> std.stdio; |
|---|
| 30 |
|
|---|
| 31 |
<span class="keyword">struct</span> S { |
|---|
| 32 |
<span class="keyword">int</span> i; |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
S foo() { |
|---|
| 36 |
S s; |
|---|
| 37 |
s.i = <span class="number">12</span>; |
|---|
| 38 |
} |
|---|
| 39 |
<span class="keyword">void</span> bar(S s) { |
|---|
| 40 |
writefln(s); |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
<span class="keyword">extern</span> (C) <span class="keyword">void</span> PydMain() { |
|---|
| 44 |
d_to_python(<span class="keyword">delegate int</span>(S s) { <span class="keyword">return</span> s.i; }); |
|---|
| 45 |
python_to_d(<span class="keyword">delegate</span> S(<span class="keyword">int</span> i) { S s; s.i = i; <span class="keyword">return</span> s; }); |
|---|
| 46 |
|
|---|
| 47 |
def!(foo); |
|---|
| 48 |
def!(bar); |
|---|
| 49 |
module_init(); |
|---|
| 50 |
}</pre> |
|---|
| 51 |
|
|---|
| 52 |
<p>And in Python:</p> |
|---|
| 53 |
|
|---|
| 54 |
<pre class="code">>>> foo() |
|---|
| 55 |
12 |
|---|
| 56 |
>>> bar(<span class="number">20</span>) |
|---|
| 57 |
20</pre> |
|---|
| 58 |
|
|---|
| 59 |
</div> |
|---|
| 60 |
|
|---|
| 61 |
</body> |
|---|
| 62 |
</html> |
|---|