signal_base.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Boost.Signals library
  2. // Copyright Douglas Gregor 2001-2004. Use, modification and
  3. // distribution is subject to the Boost Software License, Version
  4. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // For more information, see http://www.boost.org
  7. #ifndef BOOST_SIGNALS_SIGNAL_BASE_HEADER
  8. #define BOOST_SIGNALS_SIGNAL_BASE_HEADER
  9. #include <boost/signals/detail/config.hpp>
  10. #include <boost/signals/detail/signals_common.hpp>
  11. #include <boost/signals/detail/named_slot_map.hpp>
  12. #include <boost/signals/connection.hpp>
  13. #include <boost/signals/trackable.hpp>
  14. #include <boost/signals/slot.hpp>
  15. #include <boost/smart_ptr.hpp>
  16. #include <boost/noncopyable.hpp>
  17. #include <boost/function/function2.hpp>
  18. #include <utility>
  19. #include <vector>
  20. #ifdef BOOST_HAS_ABI_HEADERS
  21. # include BOOST_ABI_PREFIX
  22. #endif
  23. namespace boost {
  24. namespace BOOST_SIGNALS_NAMESPACE {
  25. namespace detail {
  26. // Must be constructed before calling the slots, because it safely
  27. // manages call depth
  28. class BOOST_SIGNALS_DECL call_notification {
  29. public:
  30. call_notification(const shared_ptr<signal_base_impl>&);
  31. ~call_notification();
  32. shared_ptr<signal_base_impl> impl;
  33. };
  34. // Implementation of base class for all signals. It handles the
  35. // management of the underlying slot lists.
  36. class BOOST_SIGNALS_DECL signal_base_impl {
  37. public:
  38. friend class call_notification;
  39. typedef function2<bool, stored_group, stored_group> compare_type;
  40. // Make sure that an exception does not cause the "clearing" flag to
  41. // remain set
  42. class temporarily_set_clearing {
  43. public:
  44. temporarily_set_clearing(signal_base_impl* b) : base(b)
  45. {
  46. base->flags.clearing = true;
  47. }
  48. ~temporarily_set_clearing()
  49. {
  50. base->flags.clearing = false;
  51. }
  52. private:
  53. signal_base_impl* base;
  54. };
  55. friend class temporarily_set_clearing;
  56. signal_base_impl(const compare_type&, const any&);
  57. ~signal_base_impl();
  58. // Disconnect all slots connected to this signal
  59. void disconnect_all_slots();
  60. // Are there any connected slots?
  61. bool empty() const;
  62. // The number of connected slots
  63. std::size_t num_slots() const;
  64. // Disconnect all slots in the given group
  65. void disconnect(const stored_group&);
  66. // We're being notified that a slot has disconnected
  67. static void slot_disconnected(void* obj, void* data);
  68. connection connect_slot(const any& slot,
  69. const stored_group& name,
  70. shared_ptr<slot_base::data_t> data,
  71. connect_position at);
  72. private:
  73. // Remove all of the slots that have been marked "disconnected"
  74. void remove_disconnected_slots() const;
  75. public:
  76. // Our call depth when invoking slots (> 1 when we have a loop)
  77. mutable int call_depth;
  78. struct {
  79. // True if some slots have disconnected, but we were not able to
  80. // remove them from the list of slots because there are valid
  81. // iterators into the slot list
  82. mutable bool delayed_disconnect:1;
  83. // True if we are disconnecting all disconnected slots
  84. bool clearing:1;
  85. } flags;
  86. // Slots
  87. mutable named_slot_map slots_;
  88. any combiner_;
  89. // Types
  90. typedef named_slot_map::iterator iterator;
  91. };
  92. class BOOST_SIGNALS_DECL signal_base : public noncopyable {
  93. public:
  94. typedef signal_base_impl::compare_type compare_type;
  95. friend class call_notification;
  96. signal_base(const compare_type& comp, const any& combiner);
  97. ~signal_base();
  98. public:
  99. // Disconnect all slots connected to this signal
  100. void disconnect_all_slots() { impl->disconnect_all_slots(); }
  101. // Are there any connected slots?
  102. bool empty() const { return impl->empty(); }
  103. // How many slots are connected?
  104. std::size_t num_slots() const { return impl->num_slots(); }
  105. protected:
  106. connection connect_slot(const any& slot,
  107. const stored_group& name,
  108. shared_ptr<slot_base::data_t> data,
  109. connect_position at)
  110. {
  111. return impl->connect_slot(slot, name, data, at);
  112. }
  113. typedef named_slot_map::iterator iterator;
  114. shared_ptr<signal_base_impl> impl;
  115. };
  116. } // end namespace detail
  117. } // end namespace BOOST_SIGNALS_NAMESPACE
  118. } // end namespace boost
  119. #ifdef BOOST_HAS_ABI_HEADERS
  120. # include BOOST_ABI_SUFFIX
  121. #endif
  122. #endif // BOOST_SIGNALS_SIGNAL_BASE_HEADER