Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


Identifying events


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Nanook   Members   -  Reputation: 505

Like
0Likes
Like

Posted 07 January 2012 - 05:54 PM

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?

Sponsor:

#2 Nanook   Members   -  Reputation: 505

Like
0Likes
Like

Posted 07 January 2012 - 06:15 PM

Sorry for dobbel posting.. Tried to delete it, but doesn't seem like I can?

#3 Deprecated   Members   -  Reputation: 107

Like
0Likes
Like

Posted 07 January 2012 - 10:10 PM

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 '4' 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.

#4 Nanook   Members   -  Reputation: 505

Like
0Likes
Like

Posted 08 January 2012 - 12:26 PM

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..

#5 Sudi   Members   -  Reputation: 706

Like
0Likes
Like

Posted 08 January 2012 - 05:12 PM

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
}


#6 Madhed   Crossbones+   -  Reputation: 3073

Like
0Likes
Like

Posted 08 January 2012 - 07:01 PM

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>();





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS