Skip to content

Instantly share code, notes, and snippets.

@Laeeth
Created July 3, 2015 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Laeeth/9174498f9b79dc90ac18 to your computer and use it in GitHub Desktop.
Save Laeeth/9174498f9b79dc90ac18 to your computer and use it in GitHub Desktop.
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