On Sat, 03 Sep 2005 23:57:14 +0200, Karel Miklav
<ka***@lovetemple.adbloccker.net> wrote:
I have a kind of ... game. This game gets messages about UI events
from a platform dependant layer. Messages are various but simple, like:
class some_display_event
{
public:
virtual ~some_display_event() { };
};
class reconfigure_event : public some_display_event
{
public:
int width;
int height;
reconfigure_event(int _width, int _height) :
width(_width), height (_height) { };
private:
reconfigure_event();
};
class quit_event : public some_display_event { };
...
Ah, the classical polymorphic event queue problem! The use of RTTI in this
case is not only messy (as you have discovered), it is also _very_ expensive
in the run-time domain, as each event will take on average N/2 dynamic_cast's
to determine what event it actually is (where N is the total number of event
classes). If you receive several thousand messages per second (typical for a
GUI), the lookup overhead will be significant.
I suspect what you want is an event _interface_:
class some_display_event
{
public:
virtual void do() = 0; // pure virtual
};
and each event will implement do() in its own intelligent way:
class reconfigure_event: public some_display_event
{
public:
int width;
int height;
reconfigure_event(int width, int height);
virtual void do();
};
void reconfigure_event::do()
{
renconfigure(width, height);
}
and your event loop will look like:
while(loop_endlessly)
{
/* ... */
some_display_event* event = display.check_events();
event->do();
/* ... */
}
Now the overhead for processing each event is constant, i.e. one virtual
function address lookup.
-dr