[Issue 12732] Add an Appender-like template that recursively builds a structure of Appender fields
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Sun May 11 07:52:18 PDT 2014
https://issues.dlang.org/show_bug.cgi?id=12732
--- Comment #4 from Andrej Mitrovic <andrej.mitrovich at gmail.com> ---
There we go:
-----
import std.array;
import std.string;
import std.typetuple;
alias ApplyAppender(T : E[], E) = Appender!T;
alias ApplyAppender(T) = T;
enum Identifier(alias S) = __traits(identifier, S);
string generateAliases(T)()
{
string[] res;
foreach (idx, str; staticMap!(Identifier, T.tupleof))
res ~= format("alias %s = fields[%s];", str, idx);
return res.join("\n");
}
struct AppenderWrapper(T)
{
alias Fields = staticMap!(ApplyAppender, typeof(T.tupleof));
Fields fields;
mixin(generateAliases!T);
@property T data()
{
T res;
foreach (idx, field; fields)
{
static if (is(typeof(res.tupleof[idx]) : E[], E))
res.tupleof[idx] = field.data;
else
res.tupleof[idx] = field;
}
return res;
}
}
alias vec3 = int[3];
struct Model
{
vec3[] indices;
vec3[] vertices;
vec3[] normals;
int other;
}
Model loadModel(string path)
{
AppenderWrapper!(typeof(return)) result;
vec3 vec;
result.indices ~= vec;
result.vertices ~= vec;
result.normals ~= vec;
result.other = 1;
return result.data;
}
void main() { }
-----
--
More information about the Digitalmars-d-bugs
mailing list