Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Template and containers question


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
2 replies to this topic

#1 KaiserJohan   Members   -  Reputation: 1396

Like
0Likes
Like

Posted 23 October 2012 - 08:18 AM

Hello,

I have an input manager in which you can register callbacks to different inputs, like mouse, keyboard, etc.
At first I had a Listener-interface for each and every callback, but I didn't like that solution very much, so I'm trying to find another solution with boost::function and templates. Here's the idea:

struct KeyEvent;
struct MouseEvent;

class InputManager
{
public:
	typedef boost::function<void (KeyEvent ev)>			 KeyCallback;
	typedef boost::function<void (MouseButtonEvent ev)>	 MouseButtonCallback;
	typedef boost::function<void (MouseMotionEvent ev)>	 MouseMotionCallback;

	InputManager();

	template<typename T>
	void RegisterCallback(const T& ev);
	template<typename T>
	void UnregisterCallback(const T& ev);
	template<typename T>
	void ClearAllCallbacks();

private:
	 Vector<KeyCallback> mKeyCallbacks;
	 Vector<MouseButtonCallback> mMouseButtonCallbacks;
	 Vector<MouseButtonCallback> mMouseMotionCallbacks;
};

Right now I'm doing dynamic_cast to know in what container to store the callback, but I feel there must be a better way to do it, it results in alot of casts to find the right one, and also alot of try/catch. What else could I do to map it correctly? I feel I am missusing the templates... sorry if its a vague question :|

Edited by KaiserJohan, 23 October 2012 - 08:29 AM.


Sponsor:

#2 Faelenor   Members   -  Reputation: 396

Like
4Likes
Like

Posted 23 October 2012 - 11:35 AM

Well, you can use template specialization, but at that point, I would just not use template at all and add a function for every type of callback, such as:

[source lang="cpp"]void RegisterCallback(const KeyCallback& ev);void RegisterCallback(const MouseButtonCallback& ev);...[/source]

#3 Matt-D   Crossbones+   -  Reputation: 1546

Like
0Likes
Like

Posted 23 October 2012 - 06:58 PM

Perhaps you can also consider using signals and slots, e.g., Boost.Signals: http://www.boost.org/libs/signals/, or libsigc++, or CppCallback, or, well, there is plenty to choose from ;]
See also "Alternative implementations" here: http://en.wikipedia....gnals_and_slots

Note:

The callback list (signal) performance of CppCallback, for a single slot (note it's 5 slots in a signal in the benchmark) is 50% slower than one single slot call without callback list. That means if there are 10K slot invoking, only 0.2ms is spent on the invoking itself. That also means we can heavily use callbacks (signals) in a 60 FPS game without worrying the impact of the callbacks.

http://www.kbasm.com...-benchmark.html

Edited by Matt-D, 23 October 2012 - 07:05 PM.





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