-
-
Save Laeeth/9174498f9b79dc90ac18 to your computer and use it in GitHub Desktop.
array of struct to struct of arrays
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.stdio; | |
import std.traits; | |
struct Test | |
{ | |
double a; | |
long b; | |
string c; | |
} | |
string repackIntoArray(name)(string newname) | |
{ | |
string ret= | |
" | |
struct "~newname~" | |
{ | |
"; | |
auto members=unpack!name; | |
foreach(member;members) | |
ret~=member~";"~"\n\t\t"; | |
ret~=" | |
} | |
"; | |
return ret; | |
} | |
auto unpack(U)() | |
{ | |
string[] ret; | |
auto types=unpackTypeTuple!(FieldTypeTuple!U); | |
auto names=unpackNameTuple!(FieldNameTuple!U); | |
//static assert(types.length=names.length); | |
foreach(i,type;types) | |
{ | |
ret~=type~"[] "~names[i]; | |
} | |
return ret; | |
} | |
void main(string[] args) | |
{ | |
Test[] t; | |
mixin(repackIntoArray!Test("TestArray")); | |
TestArray t2; | |
t2.a=[1,2,3]; | |
writefln("%s",t2); | |
} | |
template unpackTypeTuple(args...) | |
{ | |
string[] unpackTypeTuple() | |
{ | |
string[] ret; | |
foreach(a;args) | |
ret~=a.stringof; | |
return ret; | |
} | |
} | |
template unpackNameTuple(args...) | |
{ | |
string[] unpackNameTuple() | |
{ | |
string[] ret; | |
foreach(a;args) | |
ret~=a; | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment