Thread overview | |||||
---|---|---|---|---|---|
|
July 30, 2019 how to get the struct member as delegate type ? | ||||
---|---|---|---|---|
| ||||
I need to check on struct members is match a delegate type. for example; alias Type = scope void delegate(int) @nogc ; struct S { void onMatch1(int) scope @nogc { } void onNotMatch2(int) scope { } void onNotMatch2(int, int) scope @nogc { } Type not_match3; } please notify not_match3 member is should return false, I try this template but not work: template isDelegateMember(S, string name, D) if(is(S == struct) && __traits(hasMember, S, name) && is( D == delegate ) ) { alias Fn = typeof( __traits(getMember, S.init, name) ); static assert( isFunction!Fn ); pragma(msg, D); pragma(msg, typeof(D.init.funcptr)); pragma(msg, Fn ); pragma(msg, __traits(isSame, Fn, typeof(D.init.funcptr)) ); enum isDelegateMember = is( Fn == typeof(D.init.funcptr) ) ; } |
July 30, 2019 Re: how to get the struct member as delegate type ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Newbie2019 | On Tuesday, 30 July 2019 at 10:06:21 UTC, Newbie2019 wrote:
>
> template isDelegateMember(S, string name, D) if(is(S == struct) && __traits(hasMember, S, name) && is( D == delegate ) ) {
> alias Fn = typeof( __traits(getMember, S.init, name) );
> static assert( isFunction!Fn );
> pragma(msg, D);
> pragma(msg, typeof(D.init.funcptr));
> pragma(msg, Fn );
> pragma(msg, __traits(isSame, Fn, typeof(D.init.funcptr)) );
> enum isDelegateMember = is( Fn == typeof(D.init.funcptr) ) ;
> }
foreach(int name_index, name; __traits(allMembers, S) ) static if( isDelegateMember!(S, name, Type) ) {
enum Rules = getUDAs!( __traits(getMember, S, name) , Rule);
// handle the member is match this rules.
}
|
July 30, 2019 Re: how to get the struct member as delegate type ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Newbie2019 | On Tuesday, 30 July 2019 at 10:08:55 UTC, Newbie2019 wrote:
> foreach(int name_index, name; __traits(allMembers, S) ) static if( isDelegateMember!(S, name, Type) ) {
> enum Rules = getUDAs!( __traits(getMember, S, name) , Rule);
> // handle the member is match this rules.
> }
And one more question, how to get the Function address or delegate address of a struct member
on compile time ?
struct S {
void onMatch(){}
}
__gshared auto onMatch = (&(S.init)).funcptr ; // this not work
|
Copyright © 1999-2021 by the D Language Foundation