Last active
August 29, 2015 14:22
-
-
Save DanSnow/1eff8e102e450090d482 to your computer and use it in GitHub Desktop.
Like boost signal implement with c++11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SIGNAL_HPP_INCLUDE | |
#define SIGNAL_HPP_INCLUDE | |
#include <functional> | |
template<typename Signature> | |
class Signal { | |
public: | |
typedef typename std::function<Signature> Function; | |
typedef typename Function::result_type Result; | |
Signal(void); | |
Signal(const Signal &); | |
Signal &operator=(const Signal &); | |
void connect(Function); | |
template<typename... Args> | |
Result operator()(Args&&... args); | |
private: | |
Function f; | |
}; | |
#include "signal_priv.hpp" | |
#endif /* end of include guard: SIGNAL_HPP_INCLUDE */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SIGNAL_PRIV_HPP_INCLUDE | |
#define SIGNAL_PRIV_HPP_INCLUDE | |
template<typename Signature> | |
Signal<Signature>::Signal() { | |
} | |
template<typename Signature> | |
Signal<Signature>::Signal(const Signal &oth) { | |
f = oth.f; | |
} | |
template<typename Signature> | |
Signal<Signature> &Signal<Signature>::operator=(const Signal &oth) { | |
f = oth.f; | |
return *this; | |
} | |
template<typename Signature> | |
void Signal<Signature>::connect(Function func) { | |
this->f = func; | |
} | |
template<typename Signature> | |
template<typename... Args> | |
typename Signal<Signature>::Result | |
Signal<Signature>::operator()(Args&&... args) { | |
return f(std::forward<Args>(args)...); | |
} | |
#endif /* end of include guard: SIGNAL_PRIV_HPP_INCLUDE */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I add a specialization for void(void) because if args is void it will cause error.
But it's a bad solution.