Identifying events

Started by
4 comments, last by Madhed 12 years, 3 months ago
I need a efficient way of identifieng events..

atm I have a event class;



class Event
{
public:
Event(int eventId)
:
m_eventId(eventId)
{
}
private:
int m_eventId;
}


When users write events they do it like this:


enum
{
kCustomEvent = 'cste'
}

class CustomEvent
{
CustomEvent()
:
Event(kCustomEvent)
{
}
}


This is an idea I got from using the C4 Engine.. Its efficient to use integers like this, but Its dangerous as events might be called the same using the four letter char.. I could use strings, but hashing and comparing them wouldn't be as efficient..

Any tips on how I could do this?
Advertisement
Sorry for dobbel posting.. Tried to delete it, but doesn't seem like I can?
I'm guessing your building your own engine/library intended for other people to use? Either way, my most personal opinion is that you can't protect another programmer from every silly mistake he or she might do. If it's documented that you should use enum values and someone attempts to use [font=courier new,courier,monospace]'4'[/font] it should simply fail, preferably with an error message telling that programmer to read the .. manual.

If you actually try to protect someone from every possible error you'll realize that you can never do that good enough. What if one programmer uses the string 'cast' hoping that you'll provide typecasting while another programmer uses the same string 'cast' hoping that his or hers character will throw a fireball?

Summary: Use integers if only one option is available; if there's several options (flags) available you could use bitmasks. Make sure to document it.
Those who can do, do; those who can't do, do teach.
Guess I could have a check to see if its already one event registered with the same id..

But I also use the id's internaly in the engine so the user need to look through the code to know..
Another automated solution would be:

struct Event
{
Event(void)
{
}
virtual ~Event(void)
{
}
virtual const int getID(void) const = 0;
private:
};
template<class E>
struct Event_ : Event
{
Event_(void)
{
}
static bool checkType(Event* event)
{
return (int)&Event_<E>::TypeIDRef == event->getID() ? true : false;
}
static E* convert(Event* event)
{
if ((int)&Event_<E>::TypeIDRef == event->getID())
return (E*)event;
return NULL;
}
virtual const int getID(void) const
{
return (int)&Event_<E>::TypeIDRef;
}
static const int TypeIDRef;
};
template<class E>
const int Event_<E>::TypeIDRef = 0;


then you could use it like:

struct superEvent : Event_<superEvent>
{
};
Event* e = new superEvent();
if (superEvent.checkType(e))
{
//yes this is the event i am looking for
}
//or
superEvent* event = superEvent.convert(e);
if (event)
{
//yes this is the event i am looking for
}
There is a simple template trick for generating type ids. However they are different every time you start your application.


template<typename T> size_t getType() {
static T* ptr=0;
return (size_t)&ptr;
}

class EventA ...
class EventB ...

size_t typeA = getType<EventA>();
size_t typeB = getType<EventB>();

This topic is closed to new replies.

Advertisement