Events in D

kinke via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 31 12:36:00 PDT 2017


On Wednesday, 30 August 2017 at 15:35:57 UTC, bitwise wrote:
> -What if I want an event to lock a shared mutex of the 
> enclosing object, without storing a pointer to that mutex 
> inside the event itself (and every single other event in the 
> object)?
>
> -What if I want an event to call a method of the enclosing 
> object when a handler is added (without keeping a pointer to it 
> inside the actual event)?

So in essence, you'd like something like this to work, right?

struct Event(alias __parent, Handler) {
     enum parentHasLock = __traits(compiles, __parent.lock());
     ...
     void opCall()(Parameters!Handler args)
     {
         static if (parentHasLock)
             __parent.lock();
         ...
     }
}

struct Host1 {
     Event!Handler onChanged;
     Event!Handler onClosed;
}

and have the compiler internally instantiate something like

Event!(/* parent type */ Host1, /* .offsetof in parent in order 
to deduce the __parent address from Event's &this */ 0, Handler)
Event!(Host1, N, Handler)


More information about the Digitalmars-d mailing list