named_slot_map.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_NAMED_SLOT_MAP_HPP
  8. #define BOOST_SIGNALS_NAMED_SLOT_MAP_HPP
  9. #include <boost/signals/detail/config.hpp>
  10. #include <boost/signals/detail/signals_common.hpp>
  11. #include <boost/signals/connection.hpp>
  12. #include <boost/shared_ptr.hpp>
  13. #include <boost/function/function2.hpp>
  14. #include <boost/iterator/iterator_facade.hpp>
  15. #include <map>
  16. #include <memory>
  17. #include <utility>
  18. namespace boost { namespace BOOST_SIGNALS_NAMESPACE {
  19. enum connect_position { at_back, at_front };
  20. namespace detail {
  21. class stored_group
  22. {
  23. public:
  24. enum storage_kind { sk_empty, sk_front, sk_back, sk_group };
  25. stored_group(storage_kind p_kind = sk_empty) : kind(p_kind), group() { }
  26. template<typename T>
  27. stored_group(const T& p_group) : kind(sk_group), group(new T(p_group)) { }
  28. bool is_front() const { return kind == sk_front; }
  29. bool is_back() const { return kind == sk_back; }
  30. bool empty() const { return kind == sk_empty; }
  31. void* get() const { return group.get(); }
  32. private:
  33. storage_kind kind;
  34. shared_ptr<void> group;
  35. };
  36. typedef function2<bool, stored_group, stored_group> compare_type;
  37. // This function object bridges from a pair of any objects that hold
  38. // values of type Key to the underlying function object that compares
  39. // values of type Key.
  40. template<typename Compare, typename Key>
  41. class group_bridge_compare {
  42. public:
  43. typedef bool result_type;
  44. typedef const stored_group& first_argument_type;
  45. typedef const stored_group& second_argument_type;
  46. group_bridge_compare(const Compare& c) : comp(c)
  47. { }
  48. bool operator()(const stored_group& k1, const stored_group& k2) const
  49. {
  50. if (k1.is_front()) return !k2.is_front();
  51. if (k1.is_back()) return false;
  52. if (k2.is_front()) return false;
  53. if (k2.is_back()) return true;
  54. // Neither is empty, so compare their values to order them
  55. return comp(*static_cast<Key*>(k1.get()), *static_cast<Key*>(k2.get()));
  56. }
  57. private:
  58. Compare comp;
  59. };
  60. class BOOST_SIGNALS_DECL named_slot_map_iterator :
  61. public iterator_facade<named_slot_map_iterator,
  62. connection_slot_pair,
  63. forward_traversal_tag>
  64. {
  65. typedef std::list<connection_slot_pair> group_list;
  66. typedef group_list::iterator slot_pair_iterator;
  67. typedef std::map<stored_group, group_list, compare_type> slot_container_type;
  68. typedef slot_container_type::iterator group_iterator;
  69. typedef slot_container_type::const_iterator const_group_iterator;
  70. typedef iterator_facade<named_slot_map_iterator,
  71. connection_slot_pair,
  72. forward_traversal_tag> inherited;
  73. public:
  74. named_slot_map_iterator() : slot_assigned(false)
  75. { }
  76. named_slot_map_iterator(const named_slot_map_iterator& other)
  77. : group(other.group), last_group(other.last_group),
  78. slot_assigned(other.slot_assigned)
  79. {
  80. if (slot_assigned) slot_ = other.slot_;
  81. }
  82. named_slot_map_iterator& operator=(const named_slot_map_iterator& other)
  83. {
  84. slot_assigned = other.slot_assigned;
  85. group = other.group;
  86. last_group = other.last_group;
  87. if (slot_assigned) slot_ = other.slot_;
  88. return *this;
  89. }
  90. connection_slot_pair& dereference() const
  91. {
  92. return *slot_;
  93. }
  94. void increment()
  95. {
  96. ++slot_;
  97. if (slot_ == group->second.end()) {
  98. ++group;
  99. init_next_group();
  100. }
  101. }
  102. bool equal(const named_slot_map_iterator& other) const {
  103. return (group == other.group
  104. && (group == last_group
  105. || slot_ == other.slot_));
  106. }
  107. #if BOOST_WORKAROUND(_MSC_VER, <= 1900)
  108. void decrement();
  109. void advance(difference_type);
  110. #endif
  111. private:
  112. named_slot_map_iterator(group_iterator giter, group_iterator last) :
  113. group(giter), last_group(last), slot_assigned(false)
  114. { init_next_group(); }
  115. named_slot_map_iterator(group_iterator giter, group_iterator last,
  116. slot_pair_iterator slot) :
  117. group(giter), last_group(last), slot_(slot), slot_assigned(true)
  118. { }
  119. void init_next_group()
  120. {
  121. while (group != last_group && group->second.empty()) ++group;
  122. if (group != last_group) {
  123. slot_ = group->second.begin();
  124. slot_assigned = true;
  125. }
  126. }
  127. group_iterator group;
  128. group_iterator last_group;
  129. slot_pair_iterator slot_;
  130. bool slot_assigned;
  131. friend class named_slot_map;
  132. };
  133. class BOOST_SIGNALS_DECL named_slot_map
  134. {
  135. public:
  136. typedef named_slot_map_iterator iterator;
  137. named_slot_map(const compare_type& compare);
  138. void clear();
  139. iterator begin();
  140. iterator end();
  141. iterator insert(const stored_group& name, const connection& con,
  142. const any& slot, connect_position at);
  143. void disconnect(const stored_group& name);
  144. void erase(iterator pos);
  145. void remove_disconnected_slots();
  146. private:
  147. typedef std::list<connection_slot_pair> group_list;
  148. typedef std::map<stored_group, group_list, compare_type> slot_container_type;
  149. typedef slot_container_type::iterator group_iterator;
  150. typedef slot_container_type::const_iterator const_group_iterator;
  151. bool empty(const_group_iterator group) const
  152. {
  153. return (group->second.empty() && group != groups.begin() && group != back);
  154. }
  155. slot_container_type groups;
  156. group_iterator back;
  157. };
  158. } } }
  159. #endif // BOOST_SIGNALS_NAMED_SLOT_MAP_HPP