Implement support for connecting void-args functions on Events. The use case is to shorten the function signature in cases where the user does not care about the event arguments.
This commit is contained in:
@@ -39,6 +39,14 @@ public:
|
||||
Connection Connect(delegate callback);
|
||||
void Disconnect(Connection &conn);
|
||||
Connection operator+=(delegate callback);
|
||||
|
||||
/// Also support binding callbacks that don't read in arguments, as a shorthand.
|
||||
// TODO: No need to wrap the callback if the template args type is void.
|
||||
Connection Connect(const std::function<void()>& default_callback);
|
||||
/// Also support binding callbacks that don't read in arguments, as a shorthand.
|
||||
// TODO: No need to wrap the callback if the template args type is void.
|
||||
Connection operator += (const std::function<void()>& default_callback);
|
||||
|
||||
protected:
|
||||
std::vector<EventPtr> listeners;
|
||||
};
|
||||
@@ -62,6 +70,19 @@ bool BasicEvent<delegate, Args...>::Connection::Disconnect() {
|
||||
template<typename delegate, typename... Args>
|
||||
BasicEvent<delegate, Args...>::Connection BasicEvent<delegate, Args...>::operator+=(delegate callback) { return Connect(callback); }
|
||||
|
||||
template<typename delegate, typename ... Args>
|
||||
typename BasicEvent<delegate, Args...>::Connection BasicEvent<delegate, Args...>::Connect(
|
||||
const std::function<void()> &default_callback) {
|
||||
delegate wrapper = [default_callback] (Args... args) { default_callback(); };
|
||||
return Connect(wrapper);
|
||||
}
|
||||
|
||||
template<typename delegate, typename ... Args>
|
||||
typename BasicEvent<delegate, Args...>::Connection BasicEvent<delegate, Args...>::operator+=(
|
||||
const std::function<void()> &default_callback) {
|
||||
return Connect(default_callback);
|
||||
}
|
||||
|
||||
template<typename delegate, typename... Args>
|
||||
void BasicEvent<delegate, Args...>::operator()(Args... args) { Invoke(args...); }
|
||||
|
||||
|
Reference in New Issue
Block a user