Skip to content

@bioinfornatics /gist:1974033
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
import std.traits;
import std.stdio;
import std.string;
import std.range;
import std.ascii; // lowercase
static string getFunctionParameterString(T, S)( T ptt, S pstc ) @safe pure nothrow{
enum result = "";
static if( ptt.length > 0 && pstc.length > 0 && ptt.length == pstc.length){
result = ptt[0].stringof ~" "~ pstc.stringof ~" ";
static if( ptt.length > 1 )
result ~= ", "~ getFunctionParameterString!(typeof(ptt), typeof(pstc))( ptt[1 .. $], pstc[1 .. $] );
}
return result;
}
static string getParameterString(T, S)( T ptt, S pstc ) @safe pure nothrow{
enum result = "";
static if( ptt.length > 0 && pstc.length > 0 && ptt.length == pstc.length ){
result = pstc.stringof ~" ";
static if( ptt.length > 1 )
result ~= ", "~ getParameterString!(typeof(ptt), typeof(pstc))( ptt[1 .. $], pstc[1 .. $] );
}
return result;
}
mixin template delegates( alias instance, methods... ){
static if( methods.length > 0 ){
static if( hasMember!(typeof(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;
// static assert( ptt.length <= 26u, "Unsuported method "~methods[0]~" who has more than 26 parameters" );
mixin(
type.stringof ~" "~ typeof(instance).stringof ~" ( "
~ getFunctionParameterString!(typeof(ptt), typeof(pstc))(ptt, pstc ) ~" ){"
~ typeof(instance).stringof~"."~methods[0]~ getParameterString!(typeof(ptt), typeof(pstc))(ptt, pstc ) ~";"
~ "}"
);
}
else
static assert(false, "Method: "~methods[0]~" is not a member of "~typeof(instance).stringof );
static if( methods.length > 1 )
mixin delgates!( instance, methods[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] );
}
@9rnsr

A new utility std.typecons.Proxy, introduced in dmd 2.059, would be helpful for yours.
D-Programming-Language/phobos@608218d#L0R2771

@bioinfornatics

Proxy seem to be really interesting but i can not choose which function to enable for selected member, no ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.