Get superclasses at compile time

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 4 20:58:17 PST 2016


On 05/01/16 5:50 PM, Straivers wrote:
> On Tuesday, 5 January 2016 at 04:41:45 UTC, Rikki Cattermole wrote:
>> On 05/01/16 5:37 PM, Straivers wrote:
>>> Hello,
>>>
>>> I'm working on an event system, and I want to be able to check if an
>>> event is a subclass of another event. How might I go about this? In
>>> essence, I'm looking to compress this:
>>>
>>> static if (E == UserInputEvent || E == MouseEvent || E ==
>>> MouseButtonEvent || E == MouseReleasedEvent)
>>> {
>>>      //MouseEvent => MouseButtonEvent => MouseReleasedEvent
>>>      mreListeners[mrePtr] = listener;
>>>      mrePtr++;
>>> }
>>>
>>> into something like this:
>>>
>>> static if (isSuperclassOf!(MouseReleasedEvent, E))
>>> {
>>>      mreListeners[mrePtr] = listener;
>>>      mrePtr++;
>>> }
>>>
>>> Thank you for your time.
>>>
>>> -S
>>
>> is(E : UserInputEvent)
>
> Okay, maybe I didn't write enough. I have multiple subtypes like:
>
> static if (E == UserInputEvent || E == MouseEvent || E ==
> MouseMovementEvent)
>          { //MouseMovementEvent is a subclass of MouseEvent
>              mmeListeners[mmePtr] = listener;
>              mmePtr++;
>          }
>          static if (E == UserInputEvent || E == MouseEvent || E ==
> MouseButtonEvent || E == MouseReleasedEvent)
>          { //MouseEvent => MouseButtonEvent => MouseReleasedEvent
>              mreListeners[mrePtr] = listener;
>              mrePtr++;
>          }
>          static if (E == UserInputEvent || E == MouseEvent || E ==
> MouseButtonEvent || E == MousePressedEvent)
>          { //MouseEvent => MouseButtonEvent => MousePressedEvent
>              mpeListeners[mpePtr] = listener;
>              mpePtr++;
>          }
>
> and I want to differentiate between them so that a UserInputEvent will
> cause all three blocks to be compiled, but a MouseMovementEvent will
> only cause the first block to be compiled. Sorry about that.

static if (is(E : UserInputEvent) || is(E : MouseEvent) || is(E : 
MouseButtonEvent) || is(E : MousePressedEvent)) {
	//....
}


More information about the Digitalmars-d-learn mailing list