Jump to content
  • Advertisement
Sign in to follow this  
Nanook

Identifying events

This topic is 2530 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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>();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!