Last active
October 1, 2016 15:51
-
-
Save AndrejMitrovic/72a08aa2c078767ea4c35eb1b0560c8d 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
/* | |
Copyright: Copyright Andrej Mitrovic 2012- | |
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). | |
*/ | |
module inherit_constructors; | |
import std.string; | |
import std.typecons; | |
import std.traits; | |
import std.exception; | |
private string stcToString(uint stc) // bug: can't use ParameterStorageClass type | |
{ | |
string[] result; | |
if (stc & ParameterStorageClass.scope_) | |
result ~= "scope"; | |
if (stc & ParameterStorageClass.out_) | |
result ~= "out"; | |
if (stc & ParameterStorageClass.ref_) | |
result ~= "ref"; | |
if (stc & ParameterStorageClass.lazy_) | |
result ~= "lazy"; | |
return result.join(" "); | |
} | |
private @property Tuple!(string, string) getParams(alias func)() | |
{ | |
string[] params; | |
string[] args; | |
alias ParameterStorageClassTuple!func STCFunc; | |
foreach (idx, param; ParameterTypeTuple!func) | |
{ | |
string arg = format("_arg%s", idx); | |
params ~= format("%s %s %s", stcToString(STCFunc[idx]), param.stringof, arg); | |
args ~= arg; | |
} | |
return tuple(params.join(", "), args.join(", ")); | |
} | |
private string ForwardCtorsImpl(T)() | |
{ | |
string result; | |
foreach (Ctor; __traits(getOverloads, T, "__ctor")) | |
{ | |
auto res = getParams!Ctor; | |
result ~= format("this(%s) { super(%s); }\n", res[0], res[1]); | |
} | |
return result; | |
} | |
mixin template ForwardCtors() | |
{ | |
mixin(ForwardCtorsImpl!(typeof(this))()); | |
} | |
class Foo | |
{ | |
this(ref int x) { } | |
this(out float x) { } | |
this(lazy string x) { } | |
this(scope int[] x) { } | |
this(float[] z) { } | |
} | |
class Bar : Foo | |
{ | |
mixin ForwardCtors; | |
} | |
void main() | |
{ | |
int a; | |
float b; | |
string c; | |
int[] d; | |
float[] e; | |
auto b1 = new Bar(a); | |
auto b2 = new Bar(b); | |
auto b3 = new Bar(c); | |
auto b4 = new Bar(d); | |
auto b5 = new Bar(e); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment