Jump to content
  • Advertisement
Sign in to follow this  
lpcstr

Creating a EventHandler class

This topic is 2620 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 some basic event handling for my library, and I'd rather not include a dependency on boost. Also, the fact that Boost.Signals is the slowest implementation of signals/slots doesn't particularly make me enthusiastic about using it, especially if I don't need all of it's additional functionality.

I'm mainly looking to use this class for handling various UI events like OnKey, OnMouse, etc. Here is what I have so far.


struct EventArgs
{};

template <typename EventArgType>
class EventHandler
{
public:
typedef std::function<void(EventArgType)> CallbackType;
EventHandler() {}
void Register(CallbackType func)
{
Callbacks.push_back(func);
}
template <typename Object, typename MemPtr>
void Register(Object obj, FuncType ptr)
{
Callbacks.push_back(std::bind(ptr, obj, std::placeholders::_1));
}
void operator()(EventArgType args)
{
FOREACH(Callback, Callbacks)
{
(*Callback)(args);
}
}
private:
std::vector<CallbackType> Callbacks;
};


Example usage:


struct MouseEventArgs : public EventArgs
{
int x, y;
};


EventHandler<MouseEventArgs> OnMouseMoveEvent;

OnMouseMoveEvent.Register(this, &MainWindow::OnMouseMove);

OnMouseMoveEvent(args);


Any tips?

Share this post


Link to post
Share on other sites
Advertisement
Why args is passed by value instead of const reference? (You can still modify a const reference's mutable member).
How do you implement an "all in one" handler function that has EventArgs as parameter but can accept any derived event class?

Sometimes reinventing the wheels may be not bad, you can learn much from it. But if you decide to reinvent, reinvent some featured wheels, so you won't get trouble when your application goes more complicated in the future.
Otherwise, try to find a matured library. Boost::signal(2) is not the only signal/slot library. Libsigc++ is another one, and there are more others.

Share this post


Link to post
Share on other sites

Why args is passed by value instead of const reference?


It was late and I wrote this up quickly from memory of something I had written once before. I left out the pass by reference by mistake.


How do you implement an "all in one" handler function that has EventArgs as parameter but can accept any derived event class?


I'm not sure I understand what you are asking.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!