Store struct tuple of alias and access members through it?
Timoses
timosesu at gmail.com
Thu Apr 19 15:12:09 UTC 2018
On Saturday, 7 April 2018 at 19:21:30 UTC, Simen Kjærås wrote:
> import std.meta;
> import std.traits;
>
> // List all member functions, and wrap them such that
> myFoo.fun(3) can be called as
> AllMemberFunctions!(typeof(myFoo))[idx](myFoo, 3).
> template AllMemberFunctions(T)
> {
> template createDg(alias fn)
> {
> static if (__traits(isStaticFunction, fn))
> alias createDg = fn;
> else
> ReturnType!fn createDg(ref T ctx, Parameters!fn
> args)
> {
> ReturnType!fn delegate(Parameters!fn) fun;
> fun.funcptr = &fn;
> fun.ptr = cast(void*)&ctx;
> return fun(args);
> }
> }
>
> alias GetOverloads(string name) =
> AliasSeq!(__traits(getOverloads, T, name));
>
> alias AllMemberFunctions = staticMap!(createDg,
> staticMap!(GetOverloads, __traits(allMembers, T)));
> }
>
> --
> Simen
Many thanks for this!!! Was really helpful.
I ended up unfolding the struct members into an array of member
strings and mapping those to either the struct tuple members or
the struct function members.
This way I can call all members (normal and bitfield members) in
order.
Result:
https://gist.github.com/Timoses/c78e599e91b8d05be34aefaf75ca3739
This project is really teaching me some template actions : D.
More information about the Digitalmars-d-learn
mailing list