You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nano_signal_slot.hpp 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*------------------------------------------------------------------------------
  2. ----------------------// Version: 1.06 // License: MIT //-----------------------
  3. --------------------------------------------------------------------------------
  4. Copyright (c) 2012-2013 ApEk, Nano-signal-slot Contributors
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. this software and associated documentation files (the "Software"), to deal in
  7. the Software without restriction, including without limitation the rights to
  8. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  9. of the Software, and to permit persons to whom the Software is furnished to do
  10. so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. IN THE SOFTWARE.
  20. --------------------------------------------------------------------------------
  21. ----------------------// Version: 1.06 // License: MIT //-----------------------
  22. ------------------------------------------------------------------------------*/
  23. #ifndef NANO_SIGNAL_SLOT_HPP
  24. #define NANO_SIGNAL_SLOT_HPP
  25. #include <unordered_map>
  26. #include <vector>
  27. namespace Nano
  28. {
  29. typedef std::pair<std::uintptr_t, std::uintptr_t> delegate_key_t;
  30. template <typename Re_t> class function;
  31. template <typename Re_t, typename... Args> class function<Re_t(Args...)>
  32. {
  33. void *m_this_ptr;
  34. Re_t(*m_stub_ptr)(void*, Args...);
  35. template <typename I, typename F>
  36. function (I&& this_ptr, F&& stub_ptr) :
  37. m_this_ptr { std::forward<I>(this_ptr) },
  38. m_stub_ptr { std::forward<F>(stub_ptr) } { }
  39. public:
  40. template <Re_t (*func_ptr)(Args...)>
  41. static inline function bind()
  42. {
  43. return { nullptr, [] (void *, Args... args) {
  44. return (*func_ptr)(args...); } };
  45. }
  46. template <typename T, Re_t (T::*meth_ptr)(Args...)>
  47. static inline function bind(T* pointer)
  48. {
  49. return { pointer, [] (void *this_ptr, Args... args) {
  50. return (static_cast<T*>(this_ptr)->*meth_ptr)(args...); } };
  51. }
  52. template <typename T, Re_t (T::*meth_ptr)(Args...) const>
  53. static inline function bind(T* pointer)
  54. {
  55. return { pointer, [] (void *this_ptr, Args... args) {
  56. return (static_cast<const T*>(this_ptr)->*meth_ptr)(args...); } };
  57. }
  58. Re_t operator()(Args&&... args) const
  59. {
  60. return (*m_stub_ptr)(m_this_ptr, args...);
  61. }
  62. bool operator== (delegate_key_t const& other) const
  63. {
  64. return this->operator delegate_key_t() == other;
  65. }
  66. bool operator!= (delegate_key_t const& other) const
  67. {
  68. return this->operator delegate_key_t() != other;
  69. }
  70. operator delegate_key_t ( ) const
  71. {
  72. return { reinterpret_cast<std::uintptr_t>(m_this_ptr),
  73. reinterpret_cast<std::uintptr_t>(m_stub_ptr) };
  74. }
  75. };
  76. } // namespace Nano ------------------------------------------------------------
  77. namespace std // std::hash specialization
  78. {
  79. template <> struct hash<Nano::delegate_key_t>
  80. {
  81. inline std::size_t operator() (Nano::delegate_key_t const& key) const
  82. {
  83. return key.first + 0x9E3779B9 + (key.second << 6) + (key.second >> 2);
  84. }
  85. };
  86. } // namespace std -------------------------------------------------------------
  87. namespace Nano
  88. {
  89. struct tracked
  90. {
  91. template <typename T> friend class signal;
  92. private:
  93. std::unordered_map<delegate_key_t, tracked*> tracked_connections;
  94. virtual void remove_tracked(delegate_key_t const& key)
  95. {
  96. tracked_connections.erase(key);
  97. }
  98. protected:
  99. ~tracked ()
  100. {
  101. for (auto const& connection : tracked_connections)
  102. {
  103. connection.second->remove_tracked(connection.first);
  104. }
  105. }
  106. };
  107. //------------------------------------------------------------------------------
  108. template <typename Re_t> class signal;
  109. template <typename Re_t, typename... Args>
  110. class signal<Re_t(Args...)> : public Nano::tracked
  111. {
  112. typedef Nano::function<Re_t(Args...)> function;
  113. std::unordered_map<delegate_key_t, function> m_slots;
  114. template <typename T>
  115. void sfinae_con(delegate_key_t const& key, typename T::tracked* instance)
  116. {
  117. instance->tracked_connections.emplace(key, this);
  118. tracked_connections.emplace(key, instance);
  119. }
  120. template <typename T>
  121. void sfinae_dis(delegate_key_t const& key, typename T::tracked* instance)
  122. {
  123. instance->tracked_connections.erase(key);
  124. tracked_connections.erase(key);
  125. }
  126. template <typename T> void sfinae_con(...) { }
  127. template <typename T> void sfinae_dis(...) { }
  128. virtual void remove_tracked(delegate_key_t const& key)
  129. {
  130. tracked_connections.erase(key);
  131. m_slots.erase(key);
  132. }
  133. public:
  134. template <Re_t (*func_ptr)(Args...)>
  135. void connect()
  136. {
  137. auto delegate = function::template bind<func_ptr>();
  138. m_slots.emplace(delegate, delegate);
  139. }
  140. template <typename T, Re_t (T::*meth_ptr)(Args...)>
  141. void connect(T* instance)
  142. {
  143. auto delegate = function::template bind<T, meth_ptr>(instance);
  144. m_slots.emplace(delegate, delegate);
  145. sfinae_con<T>(delegate, instance);
  146. }
  147. template <typename T, Re_t (T::*meth_ptr)(Args...) const>
  148. void connect(T* instance)
  149. {
  150. auto delegate = function::template bind<T, meth_ptr>(instance);
  151. m_slots.emplace(delegate, delegate);
  152. sfinae_con<T>(delegate, instance);
  153. }
  154. //------------------------------------------------------------------------------
  155. template <Re_t (*func_ptr)(Args... )>
  156. void disconnect()
  157. {
  158. auto delegate = function::template bind<func_ptr>();
  159. m_slots.erase(delegate);
  160. }
  161. template <typename T, Re_t (T::*meth_ptr)(Args...)>
  162. void disconnect(T* instance)
  163. {
  164. auto delegate = function::template bind<T, meth_ptr>(instance);
  165. sfinae_dis<T>(delegate, instance);
  166. m_slots.erase(delegate);
  167. }
  168. template <typename T, Re_t (T::*meth_ptr)(Args...) const>
  169. void disconnect(T* instance)
  170. {
  171. auto delegate = function::template bind<T, meth_ptr>(instance);
  172. sfinae_dis<T>(delegate, instance);
  173. m_slots.erase(delegate);
  174. }
  175. //------------------------------------------------------------------------------
  176. void operator()(Args... args) // TODO deprecated!
  177. {
  178. for (auto const& slot : m_slots)
  179. {
  180. slot.second(std::forward<Args>(args)...);
  181. }
  182. }
  183. void emit(Args... args) const
  184. {
  185. for (auto const& slot : m_slots)
  186. {
  187. slot.second(std::forward<Args>(args)...);
  188. }
  189. }
  190. template <typename Transform>
  191. Re_t emit(Args... args) const
  192. {
  193. std::vector<Re_t> srv;
  194. srv.reserve(m_slots.size());
  195. for (auto const& slot : m_slots)
  196. {
  197. srv.emplace_back(slot.second(std::forward<Args>(args)...));
  198. }
  199. return Transform()(srv.cbegin(), srv.cend());
  200. }
  201. };
  202. } // namespace Nano ------------------------------------------------------------
  203. #endif // NANO_SIGNAL_SLOT_HPP