www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get superclasses at compile time

reply Straivers <straivers98 gmail.com> writes:
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
Jan 04
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
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)
Jan 04
parent reply Straivers <straivers98 gmail.com> writes:
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.
Jan 04
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
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)) { //.... }
Jan 04
parent Straivers <straivers98 gmail.com> writes:
Huh, alright. Thanks.
Jan 06