Created
March 4, 2012 01:21
-
-
Save bioinfornatics/1969776 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; | |
mixin template arrayDelegator( alias instance, methods... ){ | |
static if( methods.length > 0 ){ | |
static if( methods[0] == "opIndexAssign" ){ | |
mixin(" | |
void opIndexAssign( " ~ ElementEncodingType!(typeof(instance)).stringof ~ " value, size_t index ){ | |
array[index] = value; | |
} | |
"); | |
} | |
else static if( methods[0] == "opIndex" ){ | |
mixin( | |
ElementEncodingType!(typeof(instance)).stringof ~ " opIndex( size_t index ){ | |
return instance[index]; | |
} | |
"); | |
} | |
else static if( methods[0] == "length" ){ | |
mixin(" | |
@property size_t length(){ | |
return instance.length; | |
} | |
"); | |
} | |
else | |
static assert( false ); | |
static if( methods.length > 1 ) | |
mixin arrayDelegator!( instance, methods[1 .. $ ] ); | |
} | |
} | |
class Container{ | |
size_t[] array; | |
mixin arrayDelegator!(array, "opIndexAssign", "opIndex", "length"); | |
this( size_t[] a ){ | |
array = a; | |
} | |
} | |
void main( string[] args ){ | |
Container c = new Container( [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