Skip to content

Commit 1ea77a8

Browse files
committed
using reflection
1 parent b92a949 commit 1ea77a8

23 files changed

Lines changed: 1151 additions & 395 deletions

dev/run-everything

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ then
1818
errors=1
1919
fi
2020

21-
for dir in tests examples
21+
echo test openmethods:tests...
22+
23+
if ! dub test --compiler $DC -q openmethods:tests $*;
24+
then
25+
echo "ERROR"
26+
errors=1
27+
fi
28+
29+
for dir in examples
2230
do
2331
echo "$dir..."
2432

dub.sdl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ buildType "x" {
1212
buildOptions "debugMode" "debugInfo" "unittests"
1313
}
1414
dependency "bolts" version="~>1.7.0"
15+
configuration "unittest" {
16+
dflags "-mixin=mixins.txt"
17+
dependency "fluent-asserts" version="~>0.13.3"
18+
}
1519
targetType "library"
1620
subPackage "./examples/acceptnovisitors"
1721
subPackage "./examples/adventure"
@@ -24,3 +28,4 @@ subPackage "./tests/errors"
2428
subPackage "./tests/mptrhash"
2529
subPackage "./tests/misc"
2630
subPackage "./benchmarks/runtimemetrics"
31+
subPackage "./tests"

examples/dl/run

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ DFLAGS="-g -I../../source -I$BOLT_SOURCE $DFLAGS"
1414
SOFLAGS="-L-fPIC -shared"
1515

1616
$DC $DFLAGS $SOFLAGS -i -c ../../source/openmethods.d
17-
$DC $DFLAGS $SOFLAGS openmethods.o -oflibopenmethods.so
17+
$DC $DFLAGS $SOFLAGS -i -c ../../source/bolts/reflection/*.d
18+
$DC $DFLAGS $SOFLAGS openmethods.o meta*.o -oflibopenmethods.so
1819

1920
$DC $DFLAGS $SOFLAGS -c animals.d
2021
$DC $DFLAGS $SOFLAGS animals.o -oflibanimals.so

examples/rolex/source/app.d

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,42 +44,42 @@ class Plane : Expense
4444
{
4545
}
4646

47-
bool approve(virtual!Role, virtual!Expense) @safe @nogc nothrow;
47+
bool approve(virtual!Role, virtual!Expense) @trusted @nogc nothrow;
4848

4949
@method
50-
bool _approve(Role r, Expense e) @safe @nogc nothrow
50+
bool _approve(Role r, Expense e) @trusted @nogc nothrow
5151
{
5252
return false;
5353
}
5454

5555
@method
56-
bool _approve(Employee r, Public e) @safe @nogc nothrow
56+
bool _approve(Employee r, Public e) @trusted @nogc nothrow
5757
{
5858
return true;
5959
}
6060

6161
@method
62-
bool _approve(Executive r, Taxi e) @safe @nogc nothrow
62+
bool _approve(Executive r, Taxi e) @trusted @nogc nothrow
6363
{
6464
return true;
6565
}
6666

6767
@method
68-
bool _approve(Owner r, Expense e) @safe @nogc nothrow
68+
bool _approve(Owner r, Expense e) @trusted @nogc nothrow
6969
{
7070
return true;
7171
}
7272

73-
string pay(virtual!Employee) @safe;
73+
string pay(virtual!Employee) @trusted;
7474

7575
@method
76-
string _pay(Employee m) @safe
76+
string _pay(Employee m) @trusted
7777
{
7878
return "salary";
7979
}
8080

8181
@method
82-
string _pay(Executive e) @safe
82+
string _pay(Executive e) @trusted
8383
{
8484
return next!pay(e) ~ " + bonus";
8585
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
module bolts.reflection.metaaggregate;
2+
3+
import bolts.reflection.metafunction;
4+
5+
static mixin template ModuleAggregateCommonCode()
6+
{
7+
import std.meta : staticMap, AliasSeq;
8+
import bolts.reflection.metafunction;
9+
10+
alias functions = staticMap!(collectMembers, __traits(allMembers, origin));
11+
12+
private alias collectMembers(string name) = collectOverloads!(
13+
name, 0, __traits(getOverloads, origin, name));
14+
15+
private alias collectOverloads(string name, int index, Fun...) =
16+
AliasSeq!(
17+
Function!(Fun[0], source, name, index),
18+
collectOverloads!(name, index + 1, Fun[1..$]));
19+
20+
private alias collectOverloads(string name, int index) = AliasSeq!();
21+
}
22+
23+
template Aggregate(alias Aggregate_, string Source = __traits(identifier, Aggregate_))
24+
{
25+
alias origin = Aggregate_;
26+
alias source = Source;
27+
mixin ModuleAggregateCommonCode;
28+
}
29+
30+
unittest
31+
{
32+
import std.format;
33+
import bolts.reflection.metafunction;
34+
import std.traits; // needed by expansion or Function.returnType
35+
36+
interface TheFullMonty
37+
{
38+
pure int foo() immutable;
39+
@nogc @trusted nothrow ref int foo(out real, return ref int, lazy int) const;
40+
@safe shared scope void foo(scope Object);
41+
}
42+
43+
template Mocker(alias F)
44+
{
45+
static if (is(F.returnType.origin == void)) {
46+
enum Mocker = F.setBody!("").mixture;
47+
} else static if (F.isRef) {
48+
enum Mocker = F.setBody!(q{
49+
static %s rv;
50+
return rv;
51+
}.format(F.returnType.mixture)).mixture;
52+
} else {
53+
enum Mocker = F.setBody!(q{
54+
return %s.init;
55+
}.format(F.returnType.mixture)).mixture;
56+
}
57+
}
58+
59+
class Mock(Interface) : Interface
60+
{
61+
mixin(staticMap!(Mocker, Aggregate!Interface.functions));
62+
}
63+
64+
TheFullMonty mock = new Mock!TheFullMonty;
65+
real x;
66+
int i, l;
67+
mock.foo(x, i, l++) = 1;
68+
assert(mock.foo(x, i, l++) == 1);
69+
assert(l == 0);
70+
}

0 commit comments

Comments
 (0)