| 1 |
module testdll; |
|---|
| 2 |
|
|---|
| 3 |
import python; |
|---|
| 4 |
import pyd.pyd; |
|---|
| 5 |
import std.stdio, std.string; |
|---|
| 6 |
|
|---|
| 7 |
void foo() { |
|---|
| 8 |
writefln("20 Monkey"); |
|---|
| 9 |
} |
|---|
| 10 |
|
|---|
| 11 |
void foo(int i) { |
|---|
| 12 |
writefln("You entered %s", i); |
|---|
| 13 |
} |
|---|
| 14 |
|
|---|
| 15 |
string bar(int i) { |
|---|
| 16 |
if (i > 10) { |
|---|
| 17 |
return "It's greater than 10!"; |
|---|
| 18 |
} else { |
|---|
| 19 |
return "It's less than 10!"; |
|---|
| 20 |
} |
|---|
| 21 |
} |
|---|
| 22 |
|
|---|
| 23 |
void baz(int i=10, string s="moo") { |
|---|
| 24 |
writefln("i = %s\ns = %s", i, s); |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
class Foo { |
|---|
| 28 |
int m_i; |
|---|
| 29 |
this() { } |
|---|
| 30 |
this(int i) { |
|---|
| 31 |
m_i = i; |
|---|
| 32 |
} |
|---|
| 33 |
this(int i, int j) { |
|---|
| 34 |
m_i = i + j; |
|---|
| 35 |
} |
|---|
| 36 |
void foo() { |
|---|
| 37 |
writefln("Foo.foo(): i = %s", m_i); |
|---|
| 38 |
} |
|---|
| 39 |
int length() { return 10; } |
|---|
| 40 |
int opSlice(int i1, int i2) { |
|---|
| 41 |
writefln(i1, " ", i2); |
|---|
| 42 |
return 12; |
|---|
| 43 |
} |
|---|
| 44 |
int opIndex(int x, int y) { |
|---|
| 45 |
writefln(x, " ", y); |
|---|
| 46 |
return x+y; |
|---|
| 47 |
} |
|---|
| 48 |
Foo opAdd(Foo f) { return new Foo(m_i + f.m_i); } |
|---|
| 49 |
int opApply(int delegate(inout int, inout int) dg) { |
|---|
| 50 |
int result = 0; |
|---|
| 51 |
int j; |
|---|
| 52 |
for (int i=0; i<10; ++i) { |
|---|
| 53 |
j = i+1; |
|---|
| 54 |
result = dg(i, j); |
|---|
| 55 |
if (result) break; |
|---|
| 56 |
} |
|---|
| 57 |
return result; |
|---|
| 58 |
} |
|---|
| 59 |
int i() { return m_i; } |
|---|
| 60 |
void i(int j) { m_i = j; } |
|---|
| 61 |
void a() {} |
|---|
| 62 |
void b() {} |
|---|
| 63 |
void c() {} |
|---|
| 64 |
void d() {} |
|---|
| 65 |
void e() {} |
|---|
| 66 |
void f() {} |
|---|
| 67 |
void g() {} |
|---|
| 68 |
void h() {} |
|---|
| 69 |
void j() {} |
|---|
| 70 |
void k() {} |
|---|
| 71 |
void l() {} |
|---|
| 72 |
void m() {} |
|---|
| 73 |
void n() {} |
|---|
| 74 |
void o() {} |
|---|
| 75 |
void p() {} |
|---|
| 76 |
void q() {} |
|---|
| 77 |
void r() {} |
|---|
| 78 |
void s() {} |
|---|
| 79 |
void t() {} |
|---|
| 80 |
void u() {} |
|---|
| 81 |
void v() {} |
|---|
| 82 |
void w() {} |
|---|
| 83 |
void x() {} |
|---|
| 84 |
void y() {} |
|---|
| 85 |
void z() {} |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
void delegate() func_test() { |
|---|
| 89 |
return { writefln("Delegate works!"); }; |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
void dg_test(void delegate() dg) { |
|---|
| 93 |
dg(); |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
class Bar { |
|---|
| 97 |
int[] m_a; |
|---|
| 98 |
this() { } |
|---|
| 99 |
this(int[] i ...) { m_a = i; } |
|---|
| 100 |
int opApply(int delegate(inout int) dg) { |
|---|
| 101 |
int result = 0; |
|---|
| 102 |
for (int i=0; i<m_a.length; ++i) { |
|---|
| 103 |
result = dg(m_a[i]); |
|---|
| 104 |
if (result) break; |
|---|
| 105 |
} |
|---|
| 106 |
return result; |
|---|
| 107 |
} |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
struct S { |
|---|
| 111 |
int i; |
|---|
| 112 |
char[] s; |
|---|
| 113 |
void write_s() { |
|---|
| 114 |
writefln(s); |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
struct A { |
|---|
| 120 |
int i; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
Foo spam(Foo f) { |
|---|
| 124 |
f.foo(); |
|---|
| 125 |
Foo g = new Foo(f.i + 10); |
|---|
| 126 |
return g; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
void throws() { |
|---|
| 130 |
throw new Exception("Yay! An exception!"); |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
A conv1() { |
|---|
| 134 |
A a; |
|---|
| 135 |
a.i = 12; |
|---|
| 136 |
return a; |
|---|
| 137 |
} |
|---|
| 138 |
void conv2(A a) { |
|---|
| 139 |
writefln(a.i); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
mixin _wrap_class!( |
|---|
| 143 |
Foo, |
|---|
| 144 |
"Foo", |
|---|
| 145 |
Init!(void delegate(int), void delegate(int, int)), |
|---|
| 146 |
Property!(Foo.i, "A sample property of Foo."), |
|---|
| 147 |
Def!(Foo.foo, "A sample method of Foo."), |
|---|
| 148 |
Def!(Foo.a), |
|---|
| 149 |
Def!(Foo.b), |
|---|
| 150 |
Def!(Foo.c), |
|---|
| 151 |
Def!(Foo.d), |
|---|
| 152 |
Def!(Foo.e), |
|---|
| 153 |
Def!(Foo.f), |
|---|
| 154 |
Def!(Foo.g), |
|---|
| 155 |
Def!(Foo.h), |
|---|
| 156 |
Def!(Foo.j), |
|---|
| 157 |
Def!(Foo.k), |
|---|
| 158 |
Def!(Foo.l), |
|---|
| 159 |
Def!(Foo.m), |
|---|
| 160 |
Def!(Foo.n)/*, // Maximum length |
|---|
| 161 |
Def!(Foo.o), |
|---|
| 162 |
Def!(Foo.p), |
|---|
| 163 |
Def!(Foo.q), |
|---|
| 164 |
Def!(Foo.r), |
|---|
| 165 |
Def!(Foo.s), |
|---|
| 166 |
Def!(Foo.t), |
|---|
| 167 |
Def!(Foo.u), |
|---|
| 168 |
Def!(Foo.v), |
|---|
| 169 |
Def!(Foo.w), |
|---|
| 170 |
Def!(Foo.x), |
|---|
| 171 |
Def!(Foo.y), |
|---|
| 172 |
Def!(Foo.z)*/ |
|---|
| 173 |
) F; |
|---|
| 174 |
|
|---|
| 175 |
extern(C) void PydMain() { |
|---|
| 176 |
pragma(msg, "testdll.PydMain"); |
|---|
| 177 |
d_to_python(delegate int(A a) { return a.i; }); |
|---|
| 178 |
python_to_d(delegate A(int i) { A a; a.i = i; return a; }); |
|---|
| 179 |
|
|---|
| 180 |
def!(foo); |
|---|
| 181 |
// Python does not support function overloading. This allows us to wrap |
|---|
| 182 |
// an overloading function under a different name. Note that if the |
|---|
| 183 |
// overload accepts a different number of minimum arguments, that number |
|---|
| 184 |
// must be specified. |
|---|
| 185 |
def!(foo, "foo2", void function(int), 1); |
|---|
| 186 |
def!(bar); |
|---|
| 187 |
// Default argument support - Now implicit! |
|---|
| 188 |
def!(baz); |
|---|
| 189 |
def!(spam); |
|---|
| 190 |
def!(func_test); |
|---|
| 191 |
def!(dg_test); |
|---|
| 192 |
def!(throws); |
|---|
| 193 |
def!(conv1); |
|---|
| 194 |
def!(conv2); |
|---|
| 195 |
|
|---|
| 196 |
module_init(); |
|---|
| 197 |
|
|---|
| 198 |
F.wrap_class("A sample class."); |
|---|
| 199 |
|
|---|
| 200 |
wrap_struct!( |
|---|
| 201 |
S, |
|---|
| 202 |
Def!(S.write_s, "A struct member function."), |
|---|
| 203 |
Member!("i", "One sample data member of S."), |
|---|
| 204 |
Member!("s", "Another sample data member of S.") |
|---|
| 205 |
) ("A sample struct."); |
|---|
| 206 |
} |
|---|