Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
array of struct to struct of arrays
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
You can’t perform that action at this time.