Deduplicating template reflection code
Johannes Pfau via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Apr 14 10:57:49 PDT 2017
Am Fri, 14 Apr 2017 13:41:45 +0000
schrieb Moritz Maxeiner <moritz at ucworks.org>:
> On Friday, 14 April 2017 at 11:29:03 UTC, Johannes Pfau wrote:
> >
> > Is there some way to wrap the 'type selection'? In pseudo-code
> > something like this:
> >
> > enum FilteredOverloads(API) = ...
> >
> > foreach(Overload, FilteredOverloads!API)
> > {
> > ....
> > }
>
> Sure, but that's a bit more complex:
>
> ---
> [...] // IgnoreUDA declaration
> [...] // isSpecialFunction declaration
>
> ///
> template FilteredOverloads(API)
> {
> import std.traits : hasUDA, isSomeFunction,
> MemberFunctionsTuple;
> import std.meta : staticMap;
> import std.typetuple : TypeTuple;
>
> enum derivedMembers = __traits(derivedMembers, API);
>
> template MemberOverloads(string member)
> {
> static if (__traits(compiles, __traits(getMember, API,
> member)))
> {
> static if (isSomeFunction!(__traits(getMember, API,
> member))
> && !hasUDA!(__traits(getMember, API,
> member), IgnoreUDA)
> && !isSpecialFunction!member) {
> alias MemberOverloads =
> MemberFunctionsTuple!(API, member);
> } else {
> alias MemberOverloads = TypeTuple!();
> }
> } else {
> alias MemberOverloads = TypeTuple!();
> }
> }
>
> alias FilteredOverloads = staticMap!(MemberOverloads,
> derivedMembers);
> }
>
> //pragma(msg, FilteredOverloads!API);
> foreach(Overload; FilteredOverloads!API) {
> // function dependent code here
> }
> ---
>
> Nested templates and std.meta are your best friends if this is
> the solution you prefer :)
Great, thanks that's exactly the solution I wanted. Figuring this out by
myself is a bit above my template skill level ;-)
-- Johannes
More information about the Digitalmars-d-learn
mailing list