signal_template.hpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /*
  2. Template for Signa1, Signal2, ... classes that support signals
  3. with 1, 2, ... parameters
  4. Begin: 2007-01-23
  5. */
  6. // Copyright Frank Mori Hess 2007-2008
  7. //
  8. // Use, modification and
  9. // distribution is subject to the Boost Software License, Version
  10. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. // This file is included iteratively, and should not be protected from multiple inclusion
  13. #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  14. #define BOOST_SIGNALS2_NUM_ARGS BOOST_PP_ITERATION()
  15. #else
  16. #define BOOST_SIGNALS2_NUM_ARGS 1
  17. #endif
  18. // R, T1, T2, ..., TN, Combiner, Group, GroupCompare, SlotFunction, ExtendedSlotFunction, Mutex
  19. #define BOOST_SIGNALS2_SIGNAL_TEMPLATE_INSTANTIATION \
  20. BOOST_SIGNALS2_SIGNATURE_TEMPLATE_INSTANTIATION(BOOST_SIGNALS2_NUM_ARGS), \
  21. Combiner, Group, GroupCompare, SlotFunction, ExtendedSlotFunction, Mutex
  22. namespace boost
  23. {
  24. namespace signals2
  25. {
  26. namespace detail
  27. {
  28. // helper for bound_extended_slot_function that handles specialization for void return
  29. template<typename R>
  30. class BOOST_SIGNALS2_BOUND_EXTENDED_SLOT_FUNCTION_INVOKER_N(BOOST_SIGNALS2_NUM_ARGS)
  31. {
  32. public:
  33. typedef R result_type;
  34. template<typename ExtendedSlotFunction BOOST_SIGNALS2_PP_COMMA_IF(BOOST_SIGNALS2_NUM_ARGS)
  35. BOOST_SIGNALS2_ARGS_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS)>
  36. result_type operator()(ExtendedSlotFunction &func, const connection &conn
  37. BOOST_SIGNALS2_PP_COMMA_IF(BOOST_SIGNALS2_NUM_ARGS)
  38. BOOST_SIGNALS2_FULL_FORWARD_ARGS(BOOST_SIGNALS2_NUM_ARGS)) const
  39. {
  40. return func(conn BOOST_SIGNALS2_PP_COMMA_IF(BOOST_SIGNALS2_NUM_ARGS)
  41. BOOST_SIGNALS2_FORWARDED_ARGS(BOOST_SIGNALS2_NUM_ARGS));
  42. }
  43. };
  44. #ifdef BOOST_NO_VOID_RETURNS
  45. template<>
  46. class BOOST_SIGNALS2_BOUND_EXTENDED_SLOT_FUNCTION_INVOKER_N(BOOST_SIGNALS2_NUM_ARGS)<void>
  47. {
  48. public:
  49. typedef result_type_wrapper<void>::type result_type;
  50. template<typename ExtendedSlotFunction BOOST_SIGNALS2_PP_COMMA_IF(BOOST_SIGNALS2_NUM_ARGS)
  51. BOOST_SIGNALS2_ARGS_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS)>
  52. result_type operator()(ExtendedSlotFunction &func, const connection &conn
  53. BOOST_SIGNALS2_PP_COMMA_IF(BOOST_SIGNALS2_NUM_ARGS)
  54. BOOST_SIGNALS2_FULL_FORWARD_ARGS(BOOST_SIGNALS2_NUM_ARGS)) const
  55. {
  56. func(conn BOOST_SIGNALS2_PP_COMMA_IF(BOOST_SIGNALS2_NUM_ARGS)
  57. BOOST_SIGNALS2_FORWARDED_ARGS(BOOST_SIGNALS2_NUM_ARGS));
  58. return result_type();
  59. }
  60. };
  61. #endif
  62. // wrapper around an signalN::extended_slot_function which binds the
  63. // connection argument so it looks like a normal
  64. // signalN::slot_function
  65. template<typename ExtendedSlotFunction>
  66. class BOOST_SIGNALS2_BOUND_EXTENDED_SLOT_FUNCTION_N(BOOST_SIGNALS2_NUM_ARGS)
  67. {
  68. public:
  69. typedef typename result_type_wrapper<typename ExtendedSlotFunction::result_type>::type result_type;
  70. BOOST_SIGNALS2_BOUND_EXTENDED_SLOT_FUNCTION_N(BOOST_SIGNALS2_NUM_ARGS)(const ExtendedSlotFunction &fun):
  71. _fun(fun), _connection(new connection)
  72. {}
  73. void set_connection(const connection &conn)
  74. {
  75. *_connection = conn;
  76. }
  77. #if BOOST_SIGNALS2_NUM_ARGS > 0
  78. template<BOOST_SIGNALS2_ARGS_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS)>
  79. #endif // BOOST_SIGNALS2_NUM_ARGS > 0
  80. result_type operator()(BOOST_SIGNALS2_FULL_FORWARD_ARGS(BOOST_SIGNALS2_NUM_ARGS))
  81. {
  82. return BOOST_SIGNALS2_BOUND_EXTENDED_SLOT_FUNCTION_INVOKER_N(BOOST_SIGNALS2_NUM_ARGS)
  83. <typename ExtendedSlotFunction::result_type>()
  84. (_fun, *_connection BOOST_SIGNALS2_PP_COMMA_IF(BOOST_SIGNALS2_NUM_ARGS)
  85. BOOST_SIGNALS2_FORWARDED_ARGS(BOOST_SIGNALS2_NUM_ARGS));
  86. }
  87. // const overload
  88. #if BOOST_SIGNALS2_NUM_ARGS > 0
  89. template<BOOST_SIGNALS2_ARGS_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS)>
  90. #endif // BOOST_SIGNALS2_NUM_ARGS > 0
  91. result_type operator()(BOOST_SIGNALS2_FULL_FORWARD_ARGS(BOOST_SIGNALS2_NUM_ARGS)) const
  92. {
  93. return BOOST_SIGNALS2_BOUND_EXTENDED_SLOT_FUNCTION_INVOKER_N(BOOST_SIGNALS2_NUM_ARGS)
  94. <typename ExtendedSlotFunction::result_type>()
  95. (_fun, *_connection BOOST_SIGNALS2_PP_COMMA_IF(BOOST_SIGNALS2_NUM_ARGS)
  96. BOOST_SIGNALS2_FORWARDED_ARGS(BOOST_SIGNALS2_NUM_ARGS));
  97. }
  98. template<typename T>
  99. bool operator==(const T &other) const
  100. {
  101. return _fun == other;
  102. }
  103. private:
  104. BOOST_SIGNALS2_BOUND_EXTENDED_SLOT_FUNCTION_N(BOOST_SIGNALS2_NUM_ARGS)()
  105. {}
  106. ExtendedSlotFunction _fun;
  107. boost::shared_ptr<connection> _connection;
  108. };
  109. template<BOOST_SIGNALS2_SIGNAL_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS)>
  110. class BOOST_SIGNALS2_SIGNAL_IMPL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS);
  111. template<BOOST_SIGNALS2_SIGNAL_TEMPLATE_SPECIALIZATION_DECL(BOOST_SIGNALS2_NUM_ARGS)>
  112. class BOOST_SIGNALS2_SIGNAL_IMPL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS) BOOST_SIGNALS2_SIGNAL_TEMPLATE_SPECIALIZATION
  113. {
  114. public:
  115. typedef SlotFunction slot_function_type;
  116. // typedef slotN<Signature, SlotFunction> slot_type;
  117. typedef BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
  118. <BOOST_SIGNALS2_SIGNATURE_TEMPLATE_INSTANTIATION(BOOST_SIGNALS2_NUM_ARGS),
  119. slot_function_type> slot_type;
  120. typedef ExtendedSlotFunction extended_slot_function_type;
  121. // typedef slotN+1<R, const connection &, T1, T2, ..., TN, extended_slot_function_type> extended_slot_type;
  122. typedef BOOST_SIGNALS2_EXTENDED_SLOT_TYPE(BOOST_SIGNALS2_NUM_ARGS) extended_slot_type;
  123. typedef typename nonvoid<typename slot_function_type::result_type>::type nonvoid_slot_result_type;
  124. private:
  125. #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  126. class slot_invoker;
  127. #else // BOOST_NO_CXX11_VARIADIC_TEMPLATES
  128. typedef variadic_slot_invoker<nonvoid_slot_result_type, Args...> slot_invoker;
  129. #endif // BOOST_NO_CXX11_VARIADIC_TEMPLATES
  130. typedef slot_call_iterator_cache<nonvoid_slot_result_type, slot_invoker> slot_call_iterator_cache_type;
  131. typedef typename group_key<Group>::type group_key_type;
  132. typedef shared_ptr<connection_body<group_key_type, slot_type, Mutex> > connection_body_type;
  133. typedef grouped_list<Group, GroupCompare, connection_body_type> connection_list_type;
  134. typedef BOOST_SIGNALS2_BOUND_EXTENDED_SLOT_FUNCTION_N(BOOST_SIGNALS2_NUM_ARGS)<extended_slot_function_type>
  135. bound_extended_slot_function_type;
  136. public:
  137. typedef Combiner combiner_type;
  138. typedef typename result_type_wrapper<typename combiner_type::result_type>::type result_type;
  139. typedef Group group_type;
  140. typedef GroupCompare group_compare_type;
  141. typedef typename detail::slot_call_iterator_t<slot_invoker,
  142. typename connection_list_type::iterator, connection_body<group_key_type, slot_type, Mutex> > slot_call_iterator;
  143. BOOST_SIGNALS2_SIGNAL_IMPL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)(const combiner_type &combiner_arg,
  144. const group_compare_type &group_compare):
  145. _shared_state(new invocation_state(connection_list_type(group_compare), combiner_arg)),
  146. _garbage_collector_it(_shared_state->connection_bodies().end()),
  147. _mutex(new mutex_type())
  148. {}
  149. // connect slot
  150. connection connect(const slot_type &slot, connect_position position = at_back)
  151. {
  152. garbage_collecting_lock<mutex_type> lock(*_mutex);
  153. return nolock_connect(lock, slot, position);
  154. }
  155. connection connect(const group_type &group,
  156. const slot_type &slot, connect_position position = at_back)
  157. {
  158. garbage_collecting_lock<mutex_type> lock(*_mutex);
  159. return nolock_connect(lock, group, slot, position);
  160. }
  161. // connect extended slot
  162. connection connect_extended(const extended_slot_type &ext_slot, connect_position position = at_back)
  163. {
  164. garbage_collecting_lock<mutex_type> lock(*_mutex);
  165. bound_extended_slot_function_type bound_slot(ext_slot.slot_function());
  166. slot_type slot = replace_slot_function<slot_type>(ext_slot, bound_slot);
  167. connection conn = nolock_connect(lock, slot, position);
  168. bound_slot.set_connection(conn);
  169. return conn;
  170. }
  171. connection connect_extended(const group_type &group,
  172. const extended_slot_type &ext_slot, connect_position position = at_back)
  173. {
  174. garbage_collecting_lock<Mutex> lock(*_mutex);
  175. bound_extended_slot_function_type bound_slot(ext_slot.slot_function());
  176. slot_type slot = replace_slot_function<slot_type>(ext_slot, bound_slot);
  177. connection conn = nolock_connect(lock, group, slot, position);
  178. bound_slot.set_connection(conn);
  179. return conn;
  180. }
  181. // disconnect slot(s)
  182. void disconnect_all_slots()
  183. {
  184. shared_ptr<invocation_state> local_state =
  185. get_readable_state();
  186. typename connection_list_type::iterator it;
  187. for(it = local_state->connection_bodies().begin();
  188. it != local_state->connection_bodies().end(); ++it)
  189. {
  190. (*it)->disconnect();
  191. }
  192. }
  193. void disconnect(const group_type &group)
  194. {
  195. shared_ptr<invocation_state> local_state =
  196. get_readable_state();
  197. group_key_type group_key(grouped_slots, group);
  198. typename connection_list_type::iterator it;
  199. typename connection_list_type::iterator end_it =
  200. local_state->connection_bodies().upper_bound(group_key);
  201. for(it = local_state->connection_bodies().lower_bound(group_key);
  202. it != end_it; ++it)
  203. {
  204. (*it)->disconnect();
  205. }
  206. }
  207. template <typename T>
  208. void disconnect(const T &slot)
  209. {
  210. typedef mpl::bool_<(is_convertible<T, group_type>::value)> is_group;
  211. do_disconnect(slot, is_group());
  212. }
  213. // emit signal
  214. result_type operator ()(BOOST_SIGNALS2_SIGNATURE_FULL_ARGS(BOOST_SIGNALS2_NUM_ARGS))
  215. {
  216. shared_ptr<invocation_state> local_state;
  217. typename connection_list_type::iterator it;
  218. {
  219. garbage_collecting_lock<mutex_type> list_lock(*_mutex);
  220. // only clean up if it is safe to do so
  221. if(_shared_state.unique())
  222. nolock_cleanup_connections(list_lock, false, 1);
  223. /* Make a local copy of _shared_state while holding mutex, so we are
  224. thread safe against the combiner or connection list getting modified
  225. during invocation. */
  226. local_state = _shared_state;
  227. }
  228. slot_invoker invoker = slot_invoker(BOOST_SIGNALS2_SIGNATURE_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
  229. slot_call_iterator_cache_type cache(invoker);
  230. invocation_janitor janitor(cache, *this, &local_state->connection_bodies());
  231. return detail::combiner_invoker<typename combiner_type::result_type>()
  232. (
  233. local_state->combiner(),
  234. slot_call_iterator(local_state->connection_bodies().begin(), local_state->connection_bodies().end(), cache),
  235. slot_call_iterator(local_state->connection_bodies().end(), local_state->connection_bodies().end(), cache)
  236. );
  237. }
  238. result_type operator ()(BOOST_SIGNALS2_SIGNATURE_FULL_ARGS(BOOST_SIGNALS2_NUM_ARGS)) const
  239. {
  240. shared_ptr<invocation_state> local_state;
  241. typename connection_list_type::iterator it;
  242. {
  243. garbage_collecting_lock<mutex_type> list_lock(*_mutex);
  244. // only clean up if it is safe to do so
  245. if(_shared_state.unique())
  246. nolock_cleanup_connections(list_lock, false, 1);
  247. /* Make a local copy of _shared_state while holding mutex, so we are
  248. thread safe against the combiner or connection list getting modified
  249. during invocation. */
  250. local_state = _shared_state;
  251. }
  252. slot_invoker invoker = slot_invoker(BOOST_SIGNALS2_SIGNATURE_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
  253. slot_call_iterator_cache_type cache(invoker);
  254. invocation_janitor janitor(cache, *this, &local_state->connection_bodies());
  255. return detail::combiner_invoker<typename combiner_type::result_type>()
  256. (
  257. local_state->combiner(),
  258. slot_call_iterator(local_state->connection_bodies().begin(), local_state->connection_bodies().end(), cache),
  259. slot_call_iterator(local_state->connection_bodies().end(), local_state->connection_bodies().end(), cache)
  260. );
  261. }
  262. std::size_t num_slots() const
  263. {
  264. shared_ptr<invocation_state> local_state =
  265. get_readable_state();
  266. typename connection_list_type::iterator it;
  267. std::size_t count = 0;
  268. for(it = local_state->connection_bodies().begin();
  269. it != local_state->connection_bodies().end(); ++it)
  270. {
  271. if((*it)->connected()) ++count;
  272. }
  273. return count;
  274. }
  275. bool empty() const
  276. {
  277. shared_ptr<invocation_state> local_state =
  278. get_readable_state();
  279. typename connection_list_type::iterator it;
  280. for(it = local_state->connection_bodies().begin();
  281. it != local_state->connection_bodies().end(); ++it)
  282. {
  283. if((*it)->connected()) return false;
  284. }
  285. return true;
  286. }
  287. combiner_type combiner() const
  288. {
  289. unique_lock<mutex_type> lock(*_mutex);
  290. return _shared_state->combiner();
  291. }
  292. void set_combiner(const combiner_type &combiner_arg)
  293. {
  294. unique_lock<mutex_type> lock(*_mutex);
  295. if(_shared_state.unique())
  296. _shared_state->combiner() = combiner_arg;
  297. else
  298. _shared_state.reset(new invocation_state(*_shared_state, combiner_arg));
  299. }
  300. private:
  301. typedef Mutex mutex_type;
  302. // slot_invoker is passed to slot_call_iterator_t to run slots
  303. #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  304. class slot_invoker
  305. {
  306. public:
  307. typedef nonvoid_slot_result_type result_type;
  308. // typename add_reference<Tn>::type
  309. #define BOOST_SIGNALS2_ADD_REF_TYPE(z, n, data) \
  310. typename add_reference<BOOST_PP_CAT(T, BOOST_PP_INC(n))>::type
  311. // typename add_reference<Tn>::type argn
  312. #define BOOST_SIGNALS2_ADD_REF_ARG(z, n, data) \
  313. BOOST_SIGNALS2_ADD_REF_TYPE(~, n, ~) \
  314. BOOST_SIGNALS2_SIGNATURE_ARG_NAME(~, n, ~)
  315. // typename add_reference<T1>::type arg1, typename add_reference<T2>::type arg2, ..., typename add_reference<Tn>::type argn
  316. #define BOOST_SIGNALS2_ADD_REF_ARGS(arity) \
  317. BOOST_PP_ENUM(arity, BOOST_SIGNALS2_ADD_REF_ARG, ~)
  318. slot_invoker(BOOST_SIGNALS2_ADD_REF_ARGS(BOOST_SIGNALS2_NUM_ARGS)) BOOST_PP_EXPR_IF(BOOST_SIGNALS2_NUM_ARGS, :)
  319. #undef BOOST_SIGNALS2_ADD_REF_ARGS
  320. // m_argn
  321. #define BOOST_SIGNALS2_M_ARG_NAME(z, n, data) BOOST_PP_CAT(m_arg, BOOST_PP_INC(n))
  322. // m_argn ( argn )
  323. #define BOOST_SIGNALS2_MISC_STATEMENT(z, n, data) \
  324. BOOST_SIGNALS2_M_ARG_NAME(~, n, ~) ( BOOST_SIGNALS2_SIGNATURE_ARG_NAME(~, n, ~) )
  325. // m_arg1(arg1), m_arg2(arg2), ..., m_argn(argn)
  326. BOOST_PP_ENUM(BOOST_SIGNALS2_NUM_ARGS, BOOST_SIGNALS2_MISC_STATEMENT, ~)
  327. #undef BOOST_SIGNALS2_MISC_STATEMENT
  328. {}
  329. result_type operator ()(const connection_body_type &connectionBody) const
  330. {
  331. return m_invoke<typename slot_type::result_type>(connectionBody);
  332. }
  333. private:
  334. // declare assignment operator private since this class might have reference or const members
  335. slot_invoker & operator=(const slot_invoker &);
  336. #define BOOST_SIGNALS2_ADD_REF_M_ARG_STATEMENT(z, n, data) \
  337. BOOST_SIGNALS2_ADD_REF_TYPE(~, n, ~) BOOST_SIGNALS2_M_ARG_NAME(~, n, ~) ;
  338. BOOST_PP_REPEAT(BOOST_SIGNALS2_NUM_ARGS, BOOST_SIGNALS2_ADD_REF_M_ARG_STATEMENT, ~)
  339. #undef BOOST_SIGNALS2_ADD_REF_M_ARG_STATEMENT
  340. #undef BOOST_SIGNALS2_ADD_REF_ARG
  341. #undef BOOST_SIGNALS2_ADD_REF_TYPE
  342. // m_arg1, m_arg2, ..., m_argn
  343. #define BOOST_SIGNALS2_M_ARG_NAMES(arity) BOOST_PP_ENUM(arity, BOOST_SIGNALS2_M_ARG_NAME, ~)
  344. template<typename SlotResultType>
  345. result_type m_invoke(const connection_body_type &connectionBody,
  346. typename boost::enable_if<boost::is_void<SlotResultType> >::type * = 0) const
  347. {
  348. connectionBody->slot().slot_function()(BOOST_SIGNALS2_M_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
  349. return void_type();
  350. }
  351. template<typename SlotResultType>
  352. result_type m_invoke(const connection_body_type &connectionBody,
  353. typename boost::disable_if<boost::is_void<SlotResultType> >::type * = 0) const
  354. {
  355. return connectionBody->slot().slot_function()(BOOST_SIGNALS2_M_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
  356. }
  357. };
  358. #undef BOOST_SIGNALS2_M_ARG_NAMES
  359. #undef BOOST_SIGNALS2_M_ARG_NAME
  360. #endif // BOOST_NO_CXX11_VARIADIC_TEMPLATES
  361. // a struct used to optimize (minimize) the number of shared_ptrs that need to be created
  362. // inside operator()
  363. class invocation_state
  364. {
  365. public:
  366. invocation_state(const connection_list_type &connections_in,
  367. const combiner_type &combiner_in): _connection_bodies(new connection_list_type(connections_in)),
  368. _combiner(new combiner_type(combiner_in))
  369. {}
  370. invocation_state(const invocation_state &other, const connection_list_type &connections_in):
  371. _connection_bodies(new connection_list_type(connections_in)),
  372. _combiner(other._combiner)
  373. {}
  374. invocation_state(const invocation_state &other, const combiner_type &combiner_in):
  375. _connection_bodies(other._connection_bodies),
  376. _combiner(new combiner_type(combiner_in))
  377. {}
  378. connection_list_type & connection_bodies() { return *_connection_bodies; }
  379. const connection_list_type & connection_bodies() const { return *_connection_bodies; }
  380. combiner_type & combiner() { return *_combiner; }
  381. const combiner_type & combiner() const { return *_combiner; }
  382. private:
  383. invocation_state(const invocation_state &);
  384. shared_ptr<connection_list_type> _connection_bodies;
  385. shared_ptr<combiner_type> _combiner;
  386. };
  387. // Destructor of invocation_janitor does some cleanup when a signal invocation completes.
  388. // Code can't be put directly in signal's operator() due to complications from void return types.
  389. class invocation_janitor: noncopyable
  390. {
  391. public:
  392. typedef BOOST_SIGNALS2_SIGNAL_IMPL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS) signal_type;
  393. invocation_janitor
  394. (
  395. const slot_call_iterator_cache_type &cache,
  396. const signal_type &sig,
  397. const connection_list_type *connection_bodies
  398. ):_cache(cache), _sig(sig), _connection_bodies(connection_bodies)
  399. {}
  400. ~invocation_janitor()
  401. {
  402. // force a full cleanup of disconnected slots if there are too many
  403. if(_cache.disconnected_slot_count > _cache.connected_slot_count)
  404. {
  405. _sig.force_cleanup_connections(_connection_bodies);
  406. }
  407. }
  408. private:
  409. const slot_call_iterator_cache_type &_cache;
  410. const signal_type &_sig;
  411. const connection_list_type *_connection_bodies;
  412. };
  413. // clean up disconnected connections
  414. void nolock_cleanup_connections_from(garbage_collecting_lock<mutex_type> &lock,
  415. bool grab_tracked,
  416. const typename connection_list_type::iterator &begin, unsigned count = 0) const
  417. {
  418. BOOST_ASSERT(_shared_state.unique());
  419. typename connection_list_type::iterator it;
  420. unsigned i;
  421. for(it = begin, i = 0;
  422. it != _shared_state->connection_bodies().end() && (count == 0 || i < count);
  423. ++i)
  424. {
  425. bool connected;
  426. if(grab_tracked)
  427. (*it)->disconnect_expired_slot(lock);
  428. connected = (*it)->nolock_nograb_connected();
  429. if(connected == false)
  430. {
  431. it = _shared_state->connection_bodies().erase((*it)->group_key(), it);
  432. }else
  433. {
  434. ++it;
  435. }
  436. }
  437. _garbage_collector_it = it;
  438. }
  439. // clean up a few connections in constant time
  440. void nolock_cleanup_connections(garbage_collecting_lock<mutex_type> &lock,
  441. bool grab_tracked, unsigned count) const
  442. {
  443. BOOST_ASSERT(_shared_state.unique());
  444. typename connection_list_type::iterator begin;
  445. if(_garbage_collector_it == _shared_state->connection_bodies().end())
  446. {
  447. begin = _shared_state->connection_bodies().begin();
  448. }else
  449. {
  450. begin = _garbage_collector_it;
  451. }
  452. nolock_cleanup_connections_from(lock, grab_tracked, begin, count);
  453. }
  454. /* Make a new copy of the slot list if it is currently being read somewhere else
  455. */
  456. void nolock_force_unique_connection_list(garbage_collecting_lock<mutex_type> &lock)
  457. {
  458. if(_shared_state.unique() == false)
  459. {
  460. _shared_state.reset(new invocation_state(*_shared_state, _shared_state->connection_bodies()));
  461. nolock_cleanup_connections_from(lock, true, _shared_state->connection_bodies().begin());
  462. }else
  463. {
  464. /* We need to try and check more than just 1 connection here to avoid corner
  465. cases where certain repeated connect/disconnect patterns cause the slot
  466. list to grow without limit. */
  467. nolock_cleanup_connections(lock, true, 2);
  468. }
  469. }
  470. // force a full cleanup of the connection list
  471. void force_cleanup_connections(const connection_list_type *connection_bodies) const
  472. {
  473. garbage_collecting_lock<mutex_type> list_lock(*_mutex);
  474. // if the connection list passed in as a parameter is no longer in use,
  475. // we don't need to do any cleanup.
  476. if(&_shared_state->connection_bodies() != connection_bodies)
  477. {
  478. return;
  479. }
  480. if(_shared_state.unique() == false)
  481. {
  482. _shared_state.reset(new invocation_state(*_shared_state, _shared_state->connection_bodies()));
  483. }
  484. nolock_cleanup_connections_from(list_lock, false, _shared_state->connection_bodies().begin());
  485. }
  486. shared_ptr<invocation_state> get_readable_state() const
  487. {
  488. unique_lock<mutex_type> list_lock(*_mutex);
  489. return _shared_state;
  490. }
  491. connection_body_type create_new_connection(garbage_collecting_lock<mutex_type> &lock,
  492. const slot_type &slot)
  493. {
  494. nolock_force_unique_connection_list(lock);
  495. return connection_body_type(new connection_body<group_key_type, slot_type, Mutex>(slot, _mutex));
  496. }
  497. void do_disconnect(const group_type &group, mpl::bool_<true> /* is_group */)
  498. {
  499. disconnect(group);
  500. }
  501. template<typename T>
  502. void do_disconnect(const T &slot, mpl::bool_<false> /* is_group */)
  503. {
  504. shared_ptr<invocation_state> local_state =
  505. get_readable_state();
  506. typename connection_list_type::iterator it;
  507. for(it = local_state->connection_bodies().begin();
  508. it != local_state->connection_bodies().end(); ++it)
  509. {
  510. garbage_collecting_lock<connection_body_base> lock(**it);
  511. if((*it)->nolock_nograb_connected() == false) continue;
  512. if((*it)->slot().slot_function() == slot)
  513. {
  514. (*it)->nolock_disconnect(lock);
  515. }else
  516. {
  517. // check for wrapped extended slot
  518. bound_extended_slot_function_type *fp;
  519. fp = (*it)->slot().slot_function().template target<bound_extended_slot_function_type>();
  520. if(fp && *fp == slot)
  521. {
  522. (*it)->nolock_disconnect(lock);
  523. }
  524. }
  525. }
  526. }
  527. // connect slot
  528. connection nolock_connect(garbage_collecting_lock<mutex_type> &lock,
  529. const slot_type &slot, connect_position position)
  530. {
  531. connection_body_type newConnectionBody =
  532. create_new_connection(lock, slot);
  533. group_key_type group_key;
  534. if(position == at_back)
  535. {
  536. group_key.first = back_ungrouped_slots;
  537. _shared_state->connection_bodies().push_back(group_key, newConnectionBody);
  538. }else
  539. {
  540. group_key.first = front_ungrouped_slots;
  541. _shared_state->connection_bodies().push_front(group_key, newConnectionBody);
  542. }
  543. newConnectionBody->set_group_key(group_key);
  544. return connection(newConnectionBody);
  545. }
  546. connection nolock_connect(garbage_collecting_lock<mutex_type> &lock,
  547. const group_type &group,
  548. const slot_type &slot, connect_position position)
  549. {
  550. connection_body_type newConnectionBody =
  551. create_new_connection(lock, slot);
  552. // update map to first connection body in group if needed
  553. group_key_type group_key(grouped_slots, group);
  554. newConnectionBody->set_group_key(group_key);
  555. if(position == at_back)
  556. {
  557. _shared_state->connection_bodies().push_back(group_key, newConnectionBody);
  558. }else // at_front
  559. {
  560. _shared_state->connection_bodies().push_front(group_key, newConnectionBody);
  561. }
  562. return connection(newConnectionBody);
  563. }
  564. // _shared_state is mutable so we can do force_cleanup_connections during a const invocation
  565. mutable shared_ptr<invocation_state> _shared_state;
  566. mutable typename connection_list_type::iterator _garbage_collector_it;
  567. // connection list mutex must never be locked when attempting a blocking lock on a slot,
  568. // or you could deadlock.
  569. const boost::shared_ptr<mutex_type> _mutex;
  570. };
  571. template<BOOST_SIGNALS2_SIGNAL_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS)>
  572. class BOOST_SIGNALS2_WEAK_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS);
  573. }
  574. template<BOOST_SIGNALS2_SIGNAL_TEMPLATE_DEFAULTED_DECL(BOOST_SIGNALS2_NUM_ARGS)>
  575. class BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS);
  576. template<BOOST_SIGNALS2_SIGNAL_TEMPLATE_SPECIALIZATION_DECL(BOOST_SIGNALS2_NUM_ARGS)>
  577. class BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
  578. BOOST_SIGNALS2_SIGNAL_TEMPLATE_SPECIALIZATION: public signal_base,
  579. public detail::BOOST_SIGNALS2_STD_FUNCTIONAL_BASE
  580. {
  581. typedef detail::BOOST_SIGNALS2_SIGNAL_IMPL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
  582. <BOOST_SIGNALS2_SIGNAL_TEMPLATE_INSTANTIATION> impl_class;
  583. public:
  584. typedef detail::BOOST_SIGNALS2_WEAK_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
  585. <BOOST_SIGNALS2_SIGNAL_TEMPLATE_INSTANTIATION> weak_signal_type;
  586. friend class detail::BOOST_SIGNALS2_WEAK_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
  587. <BOOST_SIGNALS2_SIGNAL_TEMPLATE_INSTANTIATION>;
  588. typedef SlotFunction slot_function_type;
  589. // typedef slotN<Signature, SlotFunction> slot_type;
  590. typedef typename impl_class::slot_type slot_type;
  591. typedef typename impl_class::extended_slot_function_type extended_slot_function_type;
  592. typedef typename impl_class::extended_slot_type extended_slot_type;
  593. typedef typename slot_function_type::result_type slot_result_type;
  594. typedef Combiner combiner_type;
  595. typedef typename impl_class::result_type result_type;
  596. typedef Group group_type;
  597. typedef GroupCompare group_compare_type;
  598. typedef typename impl_class::slot_call_iterator
  599. slot_call_iterator;
  600. typedef typename mpl::identity<BOOST_SIGNALS2_SIGNATURE_FUNCTION_TYPE(BOOST_SIGNALS2_NUM_ARGS)>::type signature_type;
  601. #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  602. // typedef Tn argn_type;
  603. #define BOOST_SIGNALS2_MISC_STATEMENT(z, n, data) \
  604. typedef BOOST_PP_CAT(T, BOOST_PP_INC(n)) BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)), _type);
  605. BOOST_PP_REPEAT(BOOST_SIGNALS2_NUM_ARGS, BOOST_SIGNALS2_MISC_STATEMENT, ~)
  606. #undef BOOST_SIGNALS2_MISC_STATEMENT
  607. #if BOOST_SIGNALS2_NUM_ARGS == 1
  608. typedef arg1_type argument_type;
  609. #elif BOOST_SIGNALS2_NUM_ARGS == 2
  610. typedef arg1_type first_argument_type;
  611. typedef arg2_type second_argument_type;
  612. #endif
  613. template<unsigned n> class arg : public
  614. detail::BOOST_SIGNALS2_PREPROCESSED_ARG_N_TYPE_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
  615. <n BOOST_SIGNALS2_PP_COMMA_IF(BOOST_SIGNALS2_NUM_ARGS)
  616. BOOST_SIGNALS2_ARGS_TEMPLATE_INSTANTIATION(BOOST_SIGNALS2_NUM_ARGS)>
  617. {};
  618. BOOST_STATIC_CONSTANT(int, arity = BOOST_SIGNALS2_NUM_ARGS);
  619. #else // BOOST_NO_CXX11_VARIADIC_TEMPLATES
  620. template<unsigned n> class arg
  621. {
  622. public:
  623. typedef typename detail::variadic_arg_type<n, Args...>::type type;
  624. };
  625. BOOST_STATIC_CONSTANT(int, arity = sizeof...(Args));
  626. #endif // BOOST_NO_CXX11_VARIADIC_TEMPLATES
  627. BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)(const combiner_type &combiner_arg = combiner_type(),
  628. const group_compare_type &group_compare = group_compare_type()):
  629. _pimpl(new impl_class(combiner_arg, group_compare))
  630. {}
  631. virtual ~BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)()
  632. {
  633. }
  634. //move support
  635. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  636. BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)(
  637. BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS) && other) BOOST_NOEXCEPT
  638. {
  639. using std::swap;
  640. swap(_pimpl, other._pimpl);
  641. }
  642. BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS) &
  643. operator=(BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS) && rhs) BOOST_NOEXCEPT
  644. {
  645. if(this == &rhs)
  646. {
  647. return *this;
  648. }
  649. _pimpl.reset();
  650. using std::swap;
  651. swap(_pimpl, rhs._pimpl);
  652. return *this;
  653. }
  654. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  655. connection connect(const slot_type &slot, connect_position position = at_back)
  656. {
  657. return (*_pimpl).connect(slot, position);
  658. }
  659. connection connect(const group_type &group,
  660. const slot_type &slot, connect_position position = at_back)
  661. {
  662. return (*_pimpl).connect(group, slot, position);
  663. }
  664. connection connect_extended(const extended_slot_type &slot, connect_position position = at_back)
  665. {
  666. return (*_pimpl).connect_extended(slot, position);
  667. }
  668. connection connect_extended(const group_type &group,
  669. const extended_slot_type &slot, connect_position position = at_back)
  670. {
  671. return (*_pimpl).connect_extended(group, slot, position);
  672. }
  673. void disconnect_all_slots()
  674. {
  675. (*_pimpl).disconnect_all_slots();
  676. }
  677. void disconnect(const group_type &group)
  678. {
  679. (*_pimpl).disconnect(group);
  680. }
  681. template <typename T>
  682. void disconnect(const T &slot)
  683. {
  684. (*_pimpl).disconnect(slot);
  685. }
  686. result_type operator ()(BOOST_SIGNALS2_SIGNATURE_FULL_ARGS(BOOST_SIGNALS2_NUM_ARGS))
  687. {
  688. return (*_pimpl)(BOOST_SIGNALS2_SIGNATURE_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
  689. }
  690. result_type operator ()(BOOST_SIGNALS2_SIGNATURE_FULL_ARGS(BOOST_SIGNALS2_NUM_ARGS)) const
  691. {
  692. return (*_pimpl)(BOOST_SIGNALS2_SIGNATURE_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
  693. }
  694. std::size_t num_slots() const
  695. {
  696. return (*_pimpl).num_slots();
  697. }
  698. bool empty() const
  699. {
  700. return (*_pimpl).empty();
  701. }
  702. combiner_type combiner() const
  703. {
  704. return (*_pimpl).combiner();
  705. }
  706. void set_combiner(const combiner_type &combiner_arg)
  707. {
  708. return (*_pimpl).set_combiner(combiner_arg);
  709. }
  710. void swap(BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS) & other) BOOST_NOEXCEPT
  711. {
  712. using std::swap;
  713. swap(_pimpl, other._pimpl);
  714. }
  715. protected:
  716. virtual shared_ptr<void> lock_pimpl() const
  717. {
  718. return _pimpl;
  719. }
  720. private:
  721. shared_ptr<impl_class>
  722. _pimpl;
  723. };
  724. #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  725. // free swap function for signalN classes, findable by ADL
  726. template<BOOST_SIGNALS2_SIGNAL_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS)>
  727. void swap(
  728. BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS) <BOOST_SIGNALS2_SIGNAL_TEMPLATE_INSTANTIATION> &sig1,
  729. BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS) <BOOST_SIGNALS2_SIGNAL_TEMPLATE_INSTANTIATION> &sig2 ) BOOST_NOEXCEPT
  730. {
  731. sig1.swap(sig2);
  732. }
  733. #endif
  734. namespace detail
  735. {
  736. // wrapper class for storing other signals as slots with automatic lifetime tracking
  737. template<BOOST_SIGNALS2_SIGNAL_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS)>
  738. class BOOST_SIGNALS2_WEAK_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS);
  739. template<BOOST_SIGNALS2_SIGNAL_TEMPLATE_SPECIALIZATION_DECL(BOOST_SIGNALS2_NUM_ARGS)>
  740. class BOOST_SIGNALS2_WEAK_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
  741. BOOST_SIGNALS2_SIGNAL_TEMPLATE_SPECIALIZATION
  742. {
  743. public:
  744. typedef typename BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
  745. <BOOST_SIGNALS2_SIGNAL_TEMPLATE_INSTANTIATION>::result_type
  746. result_type;
  747. BOOST_SIGNALS2_WEAK_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
  748. (const BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
  749. <BOOST_SIGNALS2_SIGNAL_TEMPLATE_INSTANTIATION>
  750. &signal):
  751. _weak_pimpl(signal._pimpl)
  752. {}
  753. result_type operator ()(BOOST_SIGNALS2_SIGNATURE_FULL_ARGS(BOOST_SIGNALS2_NUM_ARGS))
  754. {
  755. shared_ptr<detail::BOOST_SIGNALS2_SIGNAL_IMPL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
  756. <BOOST_SIGNALS2_SIGNAL_TEMPLATE_INSTANTIATION> >
  757. shared_pimpl(_weak_pimpl.lock());
  758. return (*shared_pimpl)(BOOST_SIGNALS2_SIGNATURE_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
  759. }
  760. result_type operator ()(BOOST_SIGNALS2_SIGNATURE_FULL_ARGS(BOOST_SIGNALS2_NUM_ARGS)) const
  761. {
  762. shared_ptr<detail::BOOST_SIGNALS2_SIGNAL_IMPL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
  763. <BOOST_SIGNALS2_SIGNAL_TEMPLATE_INSTANTIATION> >
  764. shared_pimpl(_weak_pimpl.lock());
  765. return (*shared_pimpl)(BOOST_SIGNALS2_SIGNATURE_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
  766. }
  767. private:
  768. boost::weak_ptr<detail::BOOST_SIGNALS2_SIGNAL_IMPL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
  769. <BOOST_SIGNALS2_SIGNAL_TEMPLATE_INSTANTIATION> > _weak_pimpl;
  770. };
  771. #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  772. template<int arity, typename Signature>
  773. class extended_signature: public variadic_extended_signature<Signature>
  774. {};
  775. #else // BOOST_NO_CXX11_VARIADIC_TEMPLATES
  776. template<int arity, typename Signature>
  777. class extended_signature;
  778. // partial template specialization
  779. template<typename Signature>
  780. class extended_signature<BOOST_SIGNALS2_NUM_ARGS, Signature>
  781. {
  782. public:
  783. // typename function_traits<Signature>::result_type (
  784. // const boost::signals2::connection &,
  785. // typename function_traits<Signature>::arg1_type,
  786. // typename function_traits<Signature>::arg2_type,
  787. // ...,
  788. // typename function_traits<Signature>::argn_type)
  789. #define BOOST_SIGNALS2_EXT_SIGNATURE(arity, Signature) \
  790. typename function_traits<Signature>::result_type ( \
  791. const boost::signals2::connection & BOOST_SIGNALS2_PP_COMMA_IF(BOOST_SIGNALS2_NUM_ARGS) \
  792. BOOST_PP_ENUM(arity, BOOST_SIGNALS2_SIGNATURE_TO_ARGN_TYPE, Signature) )
  793. typedef function<BOOST_SIGNALS2_EXT_SIGNATURE(BOOST_SIGNALS2_NUM_ARGS, Signature)> function_type;
  794. #undef BOOST_SIGNALS2_EXT_SIGNATURE
  795. };
  796. template<unsigned arity, typename Signature, typename Combiner,
  797. typename Group, typename GroupCompare, typename SlotFunction,
  798. typename ExtendedSlotFunction, typename Mutex>
  799. class signalN;
  800. // partial template specialization
  801. template<typename Signature, typename Combiner, typename Group,
  802. typename GroupCompare, typename SlotFunction,
  803. typename ExtendedSlotFunction, typename Mutex>
  804. class signalN<BOOST_SIGNALS2_NUM_ARGS, Signature, Combiner, Group,
  805. GroupCompare, SlotFunction, ExtendedSlotFunction, Mutex>
  806. {
  807. public:
  808. typedef BOOST_SIGNALS2_SIGNAL_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)<
  809. BOOST_SIGNALS2_PORTABLE_SIGNATURE(BOOST_SIGNALS2_NUM_ARGS, Signature),
  810. Combiner, Group,
  811. GroupCompare, SlotFunction, ExtendedSlotFunction, Mutex> type;
  812. };
  813. #endif // BOOST_NO_CXX11_VARIADIC_TEMPLATES
  814. } // namespace detail
  815. } // namespace signals2
  816. } // namespace boost
  817. #undef BOOST_SIGNALS2_NUM_ARGS
  818. #undef BOOST_SIGNALS2_SIGNAL_TEMPLATE_INSTANTIATION