[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:45:44 PDT 2014
https://issues.dlang.org/show_bug.cgi?id=12732
--- Comment #2 from Andrej Mitrovic <andrej.mitrovich at gmail.com> ---
Here's a working version, unfortunately I again had to resort to string mixins:
-----
import std.array;
import std.string;
import std.typetuple;
alias vec3 = int[3];
template ApplyAppender(alias S)
{
static if (is(typeof(S) : E[], E))
enum ApplyAppender = format("Appender!(%s) %s;", typeof(S).stringof,
__traits(identifier, S));
else
enum ApplyAppender = format("%s %s;", typeof(S).stringof,
__traits(identifier, S));
}
string generate(T)()
{
string[] res;
foreach (str; staticMap!(ApplyAppender, T.tupleof))
res ~= str;
return res.join("\n");
}
struct AppenderWrapper(T)
{
mixin(generate!T);
@property T data()
{
T res;
foreach (idx, field; this.tupleof)
{
static if (is(typeof(res.tupleof[idx]) : E[], E))
res.tupleof[idx] = field.data;
else
res.tupleof[idx] = field;
}
return res;
}
}
///
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 = 5;
return result.data;
}
void main() { }
-----
--
More information about the Digitalmars-d-bugs
mailing list