Extend an enumeration

E

eXt

For a realtime-application (half an application at least, kind of a
framework) I am working with I need a event managing system and
naturally it must be fast. I have defined a set of available event
types in an enumeration like this:

enum Events {
EVENT_FOO,
EVENT_BAR,
EVENT_BAZ,
EVENT_CUSTOM1,
EVENT_CUSTOM2,
EVENT_CUSTOM3,

EVENT_MAX
};

I create an array of STL vector with EVENT_MAX elements. Any object in
the application can subscribe to an event by pushing itself onto the
events vector.

As seen in the enumeration above I have 3 custom events which will be
used to extend the application later (without changing the original
enumeration).

By defining a new enumeration I can rename the original names:

enum CustomEvents {
EVENT_CUSTOM_FOO = EVENT_CUSTOM1,
EVENT_CUSTOM_BAR,
EVENT_CUSTOM_BAZ
};

However, the prototype for subscribing and publishing events only
accepts the enumeration Events, for type-safe purposes. I could let it
take any integer but that would render the enumeration quite useless.

I don't know if I'm doing this all wrong but what I would like to do is
kind of extend the original enumeration with new names. Any
suggestions?
 
M

mlimber

eXt said:
For a realtime-application (half an application at least, kind of a
framework) I am working with I need a event managing system and
naturally it must be fast. I have defined a set of available event
types in an enumeration like this:

enum Events {
EVENT_FOO,
EVENT_BAR,
EVENT_BAZ,
EVENT_CUSTOM1,
EVENT_CUSTOM2,
EVENT_CUSTOM3,

EVENT_MAX
};

I create an array of STL vector with EVENT_MAX elements. Any object in
the application can subscribe to an event by pushing itself onto the
events vector.

As seen in the enumeration above I have 3 custom events which will be
used to extend the application later (without changing the original
enumeration).

By defining a new enumeration I can rename the original names:

enum CustomEvents {
EVENT_CUSTOM_FOO = EVENT_CUSTOM1,
EVENT_CUSTOM_BAR,
EVENT_CUSTOM_BAZ
};

However, the prototype for subscribing and publishing events only
accepts the enumeration Events, for type-safe purposes. I could let it
take any integer but that would render the enumeration quite useless.

I don't know if I'm doing this all wrong but what I would like to do is
kind of extend the original enumeration with new names. Any
suggestions?

Add a function overload that accepts your custom event type and
converts it to the generic event type. Should be nothing but a switch
statement (or perhaps a std::map lookup depending on how many there
are).

Cheers! --M
 
E

eXt

mlimber said:
Add a function overload that accepts your custom event type and
converts it to the generic event type. Should be nothing but a switch
statement (or perhaps a std::map lookup depending on how many there
are).

I guess that would work, just too bad the one using custom events has
to do that part. I guess that's the price to pay.
 
B

Bart

eXt said:
For a realtime-application (half an application at least, kind of a
framework) I am working with I need a event managing system and
naturally it must be fast. I have defined a set of available event
types in an enumeration like this:

enum Events {
EVENT_FOO,
EVENT_BAR,
EVENT_BAZ,
EVENT_CUSTOM1,
EVENT_CUSTOM2,
EVENT_CUSTOM3,

EVENT_MAX
};

I create an array of STL vector with EVENT_MAX elements. Any object in
the application can subscribe to an event by pushing itself onto the
events vector.

As seen in the enumeration above I have 3 custom events which will be
used to extend the application later (without changing the original
enumeration).

By defining a new enumeration I can rename the original names:

enum CustomEvents {
EVENT_CUSTOM_FOO = EVENT_CUSTOM1,
EVENT_CUSTOM_BAR,
EVENT_CUSTOM_BAZ

However, the prototype for subscribing and publishing events only
accepts the enumeration Events, for type-safe purposes. I could let it
take any integer but that would render the enumeration quite useless.

I don't know if I'm doing this all wrong but what I would like to do is
kind of extend the original enumeration with new names. Any
suggestions?

Use this instead:

static const Events EVENT_CUSTOM_FOO = EVENT_CUSTOM1;
static const Events EVENT_CUSTOM_BAR = EVENT_CUSTOM2;
static const Events EVENT_CUSTOM_BAZ = EVENT_CUSTOM3;


Regards,
Bart.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top