D Paste by bigasoft@gmail.com
Description: Class member functions generator with respect of some interface
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | module test; /* * The idea is to automatically generate implementation of class member functions * required by some interface (not necessarily inherited). * * In this example the following idea is demonstrated: * * Assume we have some interface MyIface. * Class MyClass implements interface MyIface, but all what it does is * forwarding all calls to a child class ChildClass (which also implements interface MyIface). * * There is a restriction for interface functions: * storage classes aren't supported with this approach, i.e. one * can't use 'ref', 'out', 'lazy', etc. function parameters. * * Build using dmd2. */ import std.stdio; import std.traits: ParameterTypeTuple; import std.typetuple: TypeTuple; // Auxiliary template template arrayToTuple(alias arr) { static if (arr.length) alias TypeTuple!(arr[0], arrayToTuple!(arr[1 .. $])) arrayToTuple; else alias TypeTuple!() arrayToTuple; } // This function generates member functions for each function required // by interface 'Iface'. // Function body is passed as template, where '##' is replaced with function name. // (Unfortunately, function body can't be constructed via a delegate function, // because delegates aren't allowed at compile time.) static string genIfaceFunctions(Iface)(string fbody_tpl) { // Can't use std.string.replace at compile time for some reason. static string replaceall(string s, string from, string to) { int j; string res; for (int i = 0; i <= s.length - from.length; i++) { if ( s[i..i+from.length] == from ) { // 'from' found res ~= s[j..i] ~ to; // add part of 's' before 'from', and add 'to'. i += from.length; // skip 'from' j = i; } } return res ~ s[j..$]; } string res; alias arrayToTuple!(__traits(allMembers, Iface)) Members; foreach (j, fname; Members) { string name = Members[j]; foreach (t; __traits(getVirtualFunctions, Iface, Members[j])) { string fbody = replaceall(fbody_tpl, "##", name); static if (is(typeof(t) R == return)) // deduce function return type res ~= R.stringof // function return type ~ " " ~ name // function name ~ "(TypeTuple!"~ParameterTypeTuple!(typeof(&t)).stringof~"args)" // args "{"~fbody~"}" // function body "\n"; } } return res; }; // This function is just a helper. genIfaceFunctions() could be called // directly from MyClass, but I just wished to avoid unneeded class members, // so placed the template instantiation outside MyClass. static string genMyIfaceFunctions(string fbody) { return genIfaceFunctions!(MyIface)(fbody); } // Our test interface interface MyIface { void a(string aa1); int a(void* aa1, char[] aa2); // overloaded functions string b(); // void parameters void delegate(const char[] ddd) d(void* dd); // let's try something complex // No 'ref', 'out', 'lazy', etc. keywords in function parameters. They won't work. } // Our test class class MyClass: MyIface { MyIface child; // All MyIface calls will be forwarded to 'child'. this(MyIface xchild) { child = xchild; // initiate 'child' by a given object } // Generate all functions required by MyIface: // void a(TypeTuple!(string) args){return child.a(args);} // int a(TypeTuple!(void*, char[]) args){return child.a(args);} // ... // Just forward all MyIface calls to the 'child' after printing a simple message. mixin(genMyIfaceFunctions("writef(\"MyClass.a(), \"); return child.##(args);")); pragma(msg, "Generated code:"); pragma(msg, genMyIfaceFunctions("writef(\"MyClass.a(), \"); return child.##(args);")); } // All MyIface functions are implemented in a child class: class ChildClass: MyIface { void a(TypeTuple!(string) args) { writeln("ChildClass.a()"); } // The rest are all dummy functions int a(TypeTuple!(void*, char[]) args){typeof(return) res; return res;} string b(){typeof(return) res; return res;} void delegate(const const(char[]) ddd) d(TypeTuple!(void*) args){typeof(return) res; return res;} } void main() { writeln("MyClass members: ", __traits(allMembers, MyClass),"\n"); MyClass myclass = new MyClass(new ChildClass()); writef("Calling myclass.a(\"Hello!\"): "); myclass.a("Hello!"); } |