The OpenD Programming Language

1 /++
2 
3 +/
4 module arsd.uda;
5 
6 /++
7 
8 +/
9 Blueprint extractUdas(Blueprint, Udas...)(Blueprint defaults) {
10 	foreach(alias uda; Udas) {
11 		static if(is(typeof(uda) == Blueprint)) {
12 			defaults = uda;
13 		} else {
14 			foreach(ref member; defaults.tupleof)
15 				static if(is(typeof(member) == typeof(uda)))
16 					member = uda;
17 		}
18 	}
19 
20 	return defaults;
21 }
22 
23 unittest {
24 	import core.attribute;
25 	static struct Name {
26 		@implicit this(string name) { this.name = name; }
27 		string name;
28 	}
29 
30 	static struct Priority {
31 		@implicit this(int priority) { this.priority = priority; }
32 		int priority;
33 	}
34 
35 	static struct Blueprint {
36 		Name name;
37 		Priority priority;
38 	}
39 
40 	static class A {
41 		@Name("a") int a;
42 		@Priority(44) int b;
43 		int c;
44 		@Priority(33) @Name("d") int d;
45 		// @(wtf => wtf) int e; // won't compile when trying to get the blueprint...
46 
47 		@Blueprint(name: "foo", priority: 44) int g;
48 	}
49 
50 	auto bp2 = Blueprint(name: "foo", priority: 44);
51 
52 	foreach(memberName; __traits(derivedMembers, A)) {
53 		alias member = __traits(getMember, A, memberName);
54 		auto bp = extractUdas!(Blueprint, __traits(getAttributes, member))(Blueprint.init);
55 		import std.stdio; writeln(memberName, " ", bp);
56 	}
57 }