Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef ONIDEV_UI_SIGNAL_H_INCLUDED
- #define ONIDEV_UI_SIGNAL_H_INCLUDED
- #include <functional>
- #include "signal.incl"
- template <typename... Args>
- class Signal
- {
- // http://stackoverflow.com/questions/21192659/variadic-templates-and-stdbind
- template<class T, int... Is>
- void connect_member(void (T::*field)(Args...), T* t, int_sequence<Is...>)
- {
- _slot = std::bind(field, t, placeholder_template<Is>{}...);
- }
- public:
- typedef std::function<void(Args...)> Slot;
- template<class T>
- void connect(void (T::*field)(Args...), T* t)
- {
- connect_member(field, t, make_int_sequence<sizeof...(Args)>{});
- }
- void connect(Slot const& slot) const
- {
- _slot = slot;
- }
- void disconnect() const
- {
- _slot = Slot();
- }
- void emit(Args... p)
- {
- if(_slot)
- _slot(p...);
- }
- Signal& operator=(Signal const& other)
- {
- disconnect();
- }
- private:
- mutable Slot _slot;
- };
- #endif
- --- signal.incl ---
- #include <cstddef>
- #include <type_traits>
- template<int...>
- struct int_sequence
- {
- };
- template<int N, int... Is>
- struct make_int_sequence:
- make_int_sequence<N-1, N-1, Is...>
- {
- };
- template<int... Is>
- struct make_int_sequence<0, Is...>:
- int_sequence<Is...>
- {
- };
- template<int>
- struct placeholder_template
- {
- };
- namespace std
- {
- template<int N>
- struct is_placeholder< placeholder_template<N> >
- : integral_constant<int, N+1>
- {
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment