Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module test;
- import std.stdio;
- import std.conv;
- import std.traits;
- interface Ia {
- void a( string s );
- void a( string s, int a );
- int a( int b, int c );
- }
- interface Ib {
- string b( string s );
- void b( string s, int a );
- int b( int b, int c );
- }
- final class Proxy( T ) : InterfacesTuple!T {
- private template ProxyPImpl( I, string M, O, string Str, Parameters ... ) {
- static if( Parameters.length > 0 ) {
- alias P = Parameters[ 0 ];
- enum str = fullyQualifiedName!P ~ " " ~ Str;
- static if( Parameters.length > 1 )
- enum ProxyPImpl = str ~ ", " ~ ProxyPImpl!( I, M, O, Str ~ "a", Parameters[ 1 .. $ ] );
- else
- enum ProxyPImpl = str;
- } else enum ProxyPImpl = "";
- }
- private template ProxyOPImpl( I, string M, O, string Str, Parameters ... ) {
- static if( Parameters.length > 0 ) {
- alias P = Parameters[ 0 ];
- enum str = Str;
- static if( Parameters.length > 1 )
- enum ProxyOPImpl = str ~ ", " ~ ProxyOPImpl!( I, M, O, Str ~ "a", Parameters[ 1 .. $ ] );
- else
- enum ProxyOPImpl = str;
- } else enum ProxyOPImpl = "";
- }
- private template ProxyOImpl( I, string M, Overloads ... ) {
- static if( Overloads.length > 0 ) {
- alias O = Overloads[ 0 ];
- enum str = /* return type */ fullyQualifiedName!( ReturnType!O ) ~ " " ~ /* function name */ M
- ~ "(" ~ /* argument list */ ProxyPImpl!( I, M, O, "a", ParameterTypeTuple!O ) ~ ")"
- ~ " {\n\treturn obj." ~ M ~ "(" ~ ProxyOPImpl!( I, M, O, "a", ParameterTypeTuple!O ) ~ ");\n}\n";
- static if( Overloads.length > 1 )
- enum ProxyOImpl = str ~ " " ~ ProxyOImpl!( I, M, Overloads[ 1 .. $ ] );
- else
- enum ProxyOImpl = str;
- } else enum ProxyOImpl = " ";
- }
- private template ProxyMImpl( I, Members ... ) {
- static if( Members.length > 0 ) {
- enum M = Members[ 0 ];
- enum str = ProxyOImpl!( I, M, typeof( __traits( getOverloads, I, M ) ) );
- static if( Members.length > 1 )
- enum ProxyMImpl = str ~ " " ~ ProxyMImpl!( I, Members[ 1 .. $ ] );
- else
- enum ProxyMImpl = str;
- } else enum ProxyMImpl = " ";
- }
- private template ProxyIImpl( I, Interfaces ... ) {
- enum str = ProxyMImpl!( I, __traits( allMembers, I ) );
- static if( Interfaces.length )
- enum ProxyIImpl = str ~ " " ~ ProxyIImpl!Interfaces;
- else
- enum ProxyIImpl = str;
- }
- public:
- T obj;
- mixin( ProxyIImpl!( InterfacesTuple!T ) );
- }
- class C : Ia, Ib {
- void a( string s ) {
- };
- void a( string s, int a ){
- };
- int a( int b, int c ){
- return 5;
- };
- string b( string s ){
- return "as";
- };
- void b( string s, int a ){
- };
- int b( int b, int c ){
- return 9;
- };
- }
- interface Ic : Ia, Ib {
- }
- void main() {
- Proxy!C p = new Proxy!C();
- p.obj = new C();
- Ia a = cast( Ia ) p;
- writeln( a.a( 5, 5 ) ); // returns 5
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement