Created
March 4, 2012 18:21
-
-
Save bioinfornatics/1974263 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.string; | |
import std.stdio; | |
import std.range; | |
import std.lowercase; | |
mixin template delegates( alias instance, methods... ){ | |
static if( methods.length > 0 ){ | |
static if( hasmembers(instance, methods[0]) ){ | |
alias ReturnType!(methods[0]) type; // function/method type returned for first method | |
alias ParameterTypeTuple!(methods[0]) ptt; // All parameter for first method | |
alias ParameterStorageClass STC; // shorten the enum name | |
alias ParameterStorageClassTuple!(methods[0]) pstc; // All parameter storage class for first method | |
enum currentIndexForParameterName = 0; | |
assert( ptt.length <= 26u, "Unsuported method "~methods[0]~" who has more than 26 parameters" ); | |
enum code = type.stringof ~" "~ typeof(instance).stringof ~" ( "; | |
code ~= getFunctionParameterString!(typeof(ptt), typeof(pstc))(ptt, pstc ) ~" ){"; | |
code ~= typeof(instance).stringof~"."~methods[0]~ mixin getParameterString!typeof(ptt), typeof(pstc))(ptt, pstc ) ~";"; | |
code ~= "}"; | |
mixin(code); | |
code = ""; | |
} | |
else | |
assert(false, "Method: "~methods[0]~" is not a member of "~typeof(instance).stringof ) | |
static if( methods.length > 1 ) | |
mixin delgates!( instance, methods[1 .. $] ); | |
} | |
} | |
template getFunctionParameterString(T, S)( T ptt, S pstc ){ | |
enum getFunctionParameterString = "", | |
static if( ptt.length > 0 && pstc.length > 0 && ptt.length == pstc.length){ | |
getFunctionParameterString = ptt[0].stringof ~" "~ pstc.stringof ~" "; | |
static if( ptt.length > 1 ) | |
getFunctionParameterString ~= ~", "~ getFunctionParameterString!(typeof(ptt), typeof(pstc))( ptt[1 .. $], pst[1 .. $] ); | |
} | |
} | |
template getParameterString(T, S)( T ptt, S pstc ){ | |
enum getParameterString = "", | |
static if( ptt.length > 0 && pstc.length > 0 && ptt.length == pstc.length ){ | |
getParameterString = pstc.stringof ~" "; | |
static if( ptt.length > 1 ) | |
getParameterString ~= ~", "~ getParameterString!(typeof(ptt), typeof(pstc))( ptt[1 .. $], pst[1 .. $] ); | |
} | |
} | |
class C{ | |
size_t[] array; | |
mixin delegates!(array, "opIndexAssign", "opIndex", "length"); | |
this( size_t[] a ){ | |
array = a; | |
} | |
} | |
void main( ){ | |
C c = new C( [0u, 1u, 2u, 3u] ); | |
writeln( c[2] ); | |
c[2] = 4u; | |
writeln( c[2] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment