Skip to content

Commit

Permalink
Added advanced type conversion. Updated site CSS.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.dsource.org/projects/pyd/trunk@114 1df65b71-e716-0410-9316-ac55df2b1602
  • Loading branch information
KirkMcDonald authored and KirkMcDonald committed Jul 6, 2007
1 parent 46de312 commit 8ba97b8
Show file tree
Hide file tree
Showing 21 changed files with 493 additions and 243 deletions.
6 changes: 6 additions & 0 deletions examples/testdll/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ def foo():

print

print "Testing custom conversion function"
print testdll.conv1()
testdll.conv2(20)

print

print '--------'
print 'SUCCESS'
print '--------'
19 changes: 19 additions & 0 deletions examples/testdll/testdll.d
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ struct S {
}
}


struct A {
int i;
}

Foo spam(Foo f) {
f.foo();
Foo g = new Foo(f.i + 10);
Expand All @@ -100,8 +105,20 @@ void throws() {
throw new Exception("Yay! An exception!");
}

A conv1() {
A a;
a.i = 12;
return a;
}
void conv2(A a) {
writefln(a.i);
}

extern(C) void PydMain() {
pragma(msg, "testdll.PydMain");
d_to_python(delegate int(A a) { return a.i; });
python_to_d(delegate A(int i) { A a; a.i = i; return a; });

def!(foo);
// Python does not support function overloading. This allows us to wrap
// an overloading function under a different name. Note that if the
Expand All @@ -115,6 +132,8 @@ extern(C) void PydMain() {
def!(func_test);
def!(dg_test);
def!(throws);
def!(conv1);
def!(conv2);

module_init();

Expand Down
81 changes: 81 additions & 0 deletions html_doc/adv_conversion.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<link href="pyd.css" rel="stylesheet" type="text/css">
<title>Advanced type conversion</title>
</head>

<body>
<div id="nav">
<p><big>Contents</big></p>
<ul>
<li><a class="nav" href="index.html">Main</a></li>
<li><a class="nav" href="install.html">Installation</a></li>
<li><a class="nav" href="basics.html">The basics</a></li>
<li><a class="nav" href="celerid.html">CeleriD</a></li>
<li><a class="nav" href="conversion.html">Type conversion</a></li>
<li><a class="navcur" href="adv_conversion.html">Advanced type conversion</a></li>
<li><a class="nav" href="func_wrapping.html">Function wrapping</a></li>
<li><a class="nav" href="class_wrapping.html">Class wrapping</a></li>
<li><a class="nav" href="inherit.html">Inheritance</a></li>
<li><a class="nav" href="struct_wrapping.html">Struct wrapping</a></li>
<li><a class="nav" href="except_wrapping.html">Exception wrapping</a></li>
<li><a class="nav" href="pydobject.html">PydObject</a></li>
<li><a class="nav" href="vsboost.html">vs. Boost.Python</a></li>
<li><a class="nav" href="credits.html">Credits</a></li>
</ul>
</div>

<div id="content">

<h1>Advanced type conversion</h1>

<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>

<dl>
<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>
<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>

<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>
<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>
</dl>

<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>

<h3><a class="anchor" name="examples">Examples</a></h3>

<pre class="code"><span class="keyword">import</span> std.stdio;

<span class="keyword">struct</span> S {
<span class="keyword">int</span> i;
}

S foo() {
S s;
s.i = <span class="number">12</span>;
}
<span class="keyword">void</span> bar(S s) {
writefln(s);
}

<span class="keyword">extern</span> (C) <span class="keyword">void</span> PydMain() {
d_to_python(<span class="keyword">delegate int</span>(S s) { <span class="keyword">return</span> s.i; });
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; });

def!(foo);
def!(bar);
module_init();
}</pre>

<p>And in Python:</p>

<pre class="code">&gt;&gt;&gt; foo()
12
&gt;&gt;&gt; bar(<span class="number">20</span>)
20</pre>

</div>

</body>
</html>
29 changes: 16 additions & 13 deletions html_doc/basics.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
<body>
<div id="nav">
<p><big>Contents</big></p>
<a class="nav" href="index.html">Main</a><br />
<a class="nav" href="install.html">Installation</a><br />
<a class="navcur" href="basics.html">The basics</a><br />
<a class="nav" href="celerid.html">CeleriD</a><br />
<a class="nav" href="conversion.html">Type conversion</a><br />
<a class="nav" href="func_wrapping.html">Function wrapping</a><br />
<a class="nav" href="class_wrapping.html">Class wrapping</a><br />
<a class="nav" href="inherit.html">Inheritance</a><br />
<a class="nav" href="struct_wrapping.html">Struct wrapping</a><br />
<a class="nav" href="except_wrapping.html">Exception wrapping</a><br />
<a class="nav" href="pydobject.html">PydObject</a><br />
<a class="nav" href="vsboost.html">vs. Boost.Python</a><br />
<a class="nav" href="credits.html">Credits</a>
<ul>
<li><a class="nav" href="index.html">Main</a></li>
<li><a class="nav" href="install.html">Installation</a></li>
<li><a class="navcur" href="basics.html">The basics</a></li>
<li><a class="nav" href="celerid.html">CeleriD</a></li>
<li><a class="nav" href="conversion.html">Type conversion</a></li>
<li><a class="nav" href="adv_conversion.html">Advanced type conversion</a></li>
<li><a class="nav" href="func_wrapping.html">Function wrapping</a></li>
<li><a class="nav" href="class_wrapping.html">Class wrapping</a></li>
<li><a class="nav" href="inherit.html">Inheritance</a></li>
<li><a class="nav" href="struct_wrapping.html">Struct wrapping</a></li>
<li><a class="nav" href="except_wrapping.html">Exception wrapping</a></li>
<li><a class="nav" href="pydobject.html">PydObject</a></li>
<li><a class="nav" href="vsboost.html">vs. Boost.Python</a></li>
<li><a class="nav" href="credits.html">Credits</a></li>
</ul>
</div>

<div id="content">
Expand Down
29 changes: 16 additions & 13 deletions html_doc/celerid.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
<body>
<div id="nav">
<p><big>Contents</big></p>
<a class="nav" href="index.html">Main</a><br />
<a class="nav" href="install.html">Installation</a><br />
<a class="nav" href="basics.html">The basics</a><br />
<a class="navcur" href="celerid.html">CeleriD</a><br />
<a class="nav" href="conversion.html">Type conversion</a><br />
<a class="nav" href="func_wrapping.html">Function wrapping</a><br />
<a class="nav" href="class_wrapping.html">Class wrapping</a><br />
<a class="nav" href="inherit.html">Inheritance</a><br />
<a class="nav" href="struct_wrapping.html">Struct wrapping</a><br />
<a class="nav" href="except_wrapping.html">Exception wrapping</a><br />
<a class="nav" href="pydobject.html">PydObject</a><br />
<a class="nav" href="vsboost.html">vs. Boost.Python</a><br />
<a class="nav" href="credits.html">Credits</a>
<ul>
<li><a class="nav" href="index.html">Main</a></li>
<li><a class="nav" href="install.html">Installation</a></li>
<li><a class="nav" href="basics.html">The basics</a></li>
<li><a class="navcur" href="celerid.html">CeleriD</a></li>
<li><a class="nav" href="conversion.html">Type conversion</a></li>
<li><a class="nav" href="adv_conversion.html">Advanced type conversion</a></li>
<li><a class="nav" href="func_wrapping.html">Function wrapping</a></li>
<li><a class="nav" href="class_wrapping.html">Class wrapping</a></li>
<li><a class="nav" href="inherit.html">Inheritance</a></li>
<li><a class="nav" href="struct_wrapping.html">Struct wrapping</a></li>
<li><a class="nav" href="except_wrapping.html">Exception wrapping</a></li>
<li><a class="nav" href="pydobject.html">PydObject</a></li>
<li><a class="nav" href="vsboost.html">vs. Boost.Python</a></li>
<li><a class="nav" href="credits.html">Credits</a></li>
</ul>
</div>

<div id="content">
Expand Down
39 changes: 21 additions & 18 deletions html_doc/class_wrapping.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
<body>
<div id="nav">
<p><big>Contents</big></p>
<a class="nav" href="index.html">Main</a><br />
<a class="nav" href="install.html">Installation</a><br />
<a class="nav" href="basics.html">The basics</a><br />
<a class="nav" href="celerid.html">CeleriD</a><br />
<a class="nav" href="conversion.html">Type conversion</a><br />
<a class="nav" href="func_wrapping.html">Function wrapping</a><br />
<a class="navcur" href="class_wrapping.html">Class wrapping</a><br />
<a class="nav" href="inherit.html">Inheritance</a><br />
<a class="nav" href="struct_wrapping.html">Struct wrapping</a><br />
<a class="nav" href="except_wrapping.html">Exception wrapping</a><br />
<a class="nav" href="pydobject.html">PydObject</a><br />
<a class="nav" href="vsboost.html">vs. Boost.Python</a><br />
<a class="nav" href="credits.html">Credits</a>
<ul>
<li><a class="nav" href="index.html">Main</a></li>
<li><a class="nav" href="install.html">Installation</a></li>
<li><a class="nav" href="basics.html">The basics</a></li>
<li><a class="nav" href="celerid.html">CeleriD</a></li>
<li><a class="nav" href="conversion.html">Type conversion</a></li>
<li><a class="nav" href="adv_conversion.html">Advanced type conversion</a></li>
<li><a class="nav" href="func_wrapping.html">Function wrapping</a></li>
<li><a class="navcur" href="class_wrapping.html">Class wrapping</a></li>
<li><a class="nav" href="inherit.html">Inheritance</a></li>
<li><a class="nav" href="struct_wrapping.html">Struct wrapping</a></li>
<li><a class="nav" href="except_wrapping.html">Exception wrapping</a></li>
<li><a class="nav" href="pydobject.html">PydObject</a></li>
<li><a class="nav" href="vsboost.html">vs. Boost.Python</a></li>
<li><a class="nav" href="credits.html">Credits</a></li>
</ul>
</div>

<div id="content">
Expand Down Expand Up @@ -149,13 +152,13 @@ <h3><a class="anchor" name="examples">Examples</a></h3>
&gt;&gt;&gt; f = Foo()
&gt;&gt;&gt; f.i
0
&gt;&gt;&gt; f.i = 20
&gt;&gt;&gt; f.i = <span class="number">20</span>
&gt;&gt;&gt; f.foo(<span class="string">"Hello! i is "</span>)
Hello! i is 20
&gt;&gt;&gt; f = Foo(10, 10)
&gt;&gt;&gt; f = Foo(<span class="number">10</span>, <span class="number">10</span>)
&gt;&gt;&gt; f.i
20
&gt;&gt;&gt; g = Foo(30)
&gt;&gt;&gt; g = Foo(<span class="number">30</span>)
&gt;&gt;&gt; g.i
30
&gt;&gt;&gt; e = f + g
Expand All @@ -164,9 +167,9 @@ <h3><a class="anchor" name="examples">Examples</a></h3>
&gt;&gt;&gt; <span class="comment"># We can even subclass our D type</span>
&gt;&gt;&gt; <span class="keyword">class</span> MyFoo(Foo):
... <span class="keyword">def</span> bar(self):
... <span class="keyword">print</span> <span class="string">"Hey, i+3 is"</span>, self.i + 3
... <span class="keyword">print</span> <span class="string">"Hey, i+3 is"</span>, self.i + <span class="number">3</span>
...
&gt;&gt;&gt; h = MyFoo(3)
&gt;&gt;&gt; h = MyFoo(<span class="number">3</span>)
&gt;&gt;&gt; h.bar()
Hey, i+3 is 6
&gt;&gt;&gt; </pre>
Expand Down
29 changes: 16 additions & 13 deletions html_doc/conversion.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
<body>
<div id="nav">
<p><big>Contents</big></p>
<a class="nav" href="index.html">Main</a><br />
<a class="nav" href="install.html">Installation</a><br />
<a class="nav" href="basics.html">The basics</a><br />
<a class="nav" href="celerid.html">CeleriD</a><br />
<a class="navcur" href="conversion.html">Type conversion</a><br />
<a class="nav" href="func_wrapping.html">Function wrapping</a><br />
<a class="nav" href="class_wrapping.html">Class wrapping</a><br />
<a class="nav" href="inherit.html">Inheritance</a><br />
<a class="nav" href="struct_wrapping.html">Struct wrapping</a><br />
<a class="nav" href="except_wrapping.html">Exception wrapping</a><br />
<a class="nav" href="pydobject.html">PydObject</a><br />
<a class="nav" href="vsboost.html">vs. Boost.Python</a><br />
<a class="nav" href="credits.html">Credits</a>
<ul>
<li><a class="nav" href="index.html">Main</a></li>
<li><a class="nav" href="install.html">Installation</a></li>
<li><a class="nav" href="basics.html">The basics</a></li>
<li><a class="nav" href="celerid.html">CeleriD</a></li>
<li><a class="navcur" href="conversion.html">Type conversion</a></li>
<li><a class="nav" href="adv_conversion.html">Advanced type conversion</a></li>
<li><a class="nav" href="func_wrapping.html">Function wrapping</a></li>
<li><a class="nav" href="class_wrapping.html">Class wrapping</a></li>
<li><a class="nav" href="inherit.html">Inheritance</a></li>
<li><a class="nav" href="struct_wrapping.html">Struct wrapping</a></li>
<li><a class="nav" href="except_wrapping.html">Exception wrapping</a></li>
<li><a class="nav" href="pydobject.html">PydObject</a></li>
<li><a class="nav" href="vsboost.html">vs. Boost.Python</a></li>
<li><a class="nav" href="credits.html">Credits</a></li>
</ul>
</div>

<div id="content">
Expand Down
29 changes: 16 additions & 13 deletions html_doc/credits.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
<body>
<div id="nav">
<p><big>Contents</big></p>
<a class="nav" href="index.html">Main</a><br />
<a class="nav" href="install.html">Installation</a><br />
<a class="nav" href="basics.html">The basics</a><br />
<a class="nav" href="celerid.html">CeleriD</a><br />
<a class="nav" href="conversion.html">Type conversion</a><br />
<a class="nav" href="func_wrapping.html">Function wrapping</a><br />
<a class="nav" href="class_wrapping.html">Class wrapping</a><br />
<a class="nav" href="inherit.html">Inheritance</a><br />
<a class="nav" href="struct_wrapping.html">Struct wrapping</a><br />
<a class="nav" href="except_wrapping.html">Exception wrapping</a><br />
<a class="nav" href="pydobject.html">PydObject</a><br />
<a class="nav" href="vsboost.html">vs. Boost.Python</a><br />
<a class="navcur" href="credits.html">Credits</a>
<ul>
<li><a class="nav" href="index.html">Main</a></li>
<li><a class="nav" href="install.html">Installation</a></li>
<li><a class="nav" href="basics.html">The basics</a></li>
<li><a class="nav" href="celerid.html">CeleriD</a></li>
<li><a class="nav" href="conversion.html">Type conversion</a></li>
<li><a class="nav" href="adv_conversion.html">Advanced type conversion</a></li>
<li><a class="nav" href="func_wrapping.html">Function wrapping</a></li>
<li><a class="nav" href="class_wrapping.html">Class wrapping</a></li>
<li><a class="nav" href="inherit.html">Inheritance</a></li>
<li><a class="nav" href="struct_wrapping.html">Struct wrapping</a></li>
<li><a class="nav" href="except_wrapping.html">Exception wrapping</a></li>
<li><a class="nav" href="pydobject.html">PydObject</a></li>
<li><a class="nav" href="vsboost.html">vs. Boost.Python</a></li>
<li><a class="navcur" href="credits.html">Credits</a></li>
</ul>
</div>

<div id="content">
Expand Down
29 changes: 16 additions & 13 deletions html_doc/except_wrapping.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
<body>
<div id="nav">
<p><big>Contents</big></p>
<a class="nav" href="index.html">Main</a><br />
<a class="nav" href="install.html">Installation</a><br />
<a class="nav" href="basics.html">The basics</a><br />
<a class="nav" href="celerid.html">CeleriD</a><br />
<a class="nav" href="conversion.html">Type conversion</a><br />
<a class="nav" href="func_wrapping.html">Function wrapping</a><br />
<a class="nav" href="class_wrapping.html">Class wrapping</a><br />
<a class="nav" href="inherit.html">Inheritance</a><br />
<a class="nav" href="struct_wrapping.html">Struct wrapping</a><br />
<a class="navcur" href="except_wrapping.html">Exception wrapping</a><br />
<a class="nav" href="pydobject.html">PydObject</a><br />
<a class="nav" href="vsboost.html">vs. Boost.Python</a><br />
<a class="nav" href="credits.html">Credits</a>
<ul>
<li><a class="nav" href="index.html">Main</a></li>
<li><a class="nav" href="install.html">Installation</a></li>
<li><a class="nav" href="basics.html">The basics</a></li>
<li><a class="nav" href="celerid.html">CeleriD</a></li>
<li><a class="nav" href="conversion.html">Type conversion</a></li>
<li><a class="nav" href="adv_conversion.html">Advanced type conversion</a></li>
<li><a class="nav" href="func_wrapping.html">Function wrapping</a></li>
<li><a class="nav" href="class_wrapping.html">Class wrapping</a></li>
<li><a class="nav" href="inherit.html">Inheritance</a></li>
<li><a class="nav" href="struct_wrapping.html">Struct wrapping</a></li>
<li><a class="navcur" href="except_wrapping.html">Exception wrapping</a></li>
<li><a class="nav" href="pydobject.html">PydObject</a></li>
<li><a class="nav" href="vsboost.html">vs. Boost.Python</a></li>
<li><a class="nav" href="credits.html">Credits</a></li>
</ul>
</div>

<div id="content">
Expand Down
Loading

0 comments on commit 8ba97b8

Please sign in to comment.