slot_groups.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Boost.Signals2 library
  2. // Copyright Frank Mori Hess 2007-2008.
  3. // Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // For more information, see http://www.boost.org
  8. #ifndef BOOST_SIGNALS2_SLOT_GROUPS_HPP
  9. #define BOOST_SIGNALS2_SLOT_GROUPS_HPP
  10. #include <boost/signals2/connection.hpp>
  11. #include <boost/optional.hpp>
  12. #include <list>
  13. #include <map>
  14. #include <utility>
  15. namespace boost {
  16. namespace signals2 {
  17. namespace detail {
  18. enum slot_meta_group {front_ungrouped_slots, grouped_slots, back_ungrouped_slots};
  19. template<typename Group>
  20. struct group_key
  21. {
  22. typedef std::pair<enum slot_meta_group, boost::optional<Group> > type;
  23. };
  24. template<typename Group, typename GroupCompare>
  25. class group_key_less
  26. {
  27. public:
  28. group_key_less()
  29. {}
  30. group_key_less(const GroupCompare &group_compare): _group_compare(group_compare)
  31. {}
  32. bool operator ()(const typename group_key<Group>::type &key1, const typename group_key<Group>::type &key2) const
  33. {
  34. if(key1.first != key2.first) return key1.first < key2.first;
  35. if(key1.first != grouped_slots) return false;
  36. return _group_compare(key1.second.get(), key2.second.get());
  37. }
  38. private:
  39. GroupCompare _group_compare;
  40. };
  41. template<typename Group, typename GroupCompare, typename ValueType>
  42. class grouped_list
  43. {
  44. public:
  45. typedef group_key_less<Group, GroupCompare> group_key_compare_type;
  46. private:
  47. typedef std::list<ValueType> list_type;
  48. typedef std::map
  49. <
  50. typename group_key<Group>::type,
  51. typename list_type::iterator,
  52. group_key_compare_type
  53. > map_type;
  54. typedef typename map_type::iterator map_iterator;
  55. typedef typename map_type::const_iterator const_map_iterator;
  56. public:
  57. typedef typename list_type::iterator iterator;
  58. typedef typename list_type::const_iterator const_iterator;
  59. typedef typename group_key<Group>::type group_key_type;
  60. grouped_list(const group_key_compare_type &group_key_compare):
  61. _group_key_compare(group_key_compare)
  62. {}
  63. grouped_list(const grouped_list &other): _list(other._list),
  64. _group_map(other._group_map), _group_key_compare(other._group_key_compare)
  65. {
  66. // fix up _group_map
  67. typename map_type::const_iterator other_map_it;
  68. typename list_type::iterator this_list_it = _list.begin();
  69. typename map_type::iterator this_map_it = _group_map.begin();
  70. for(other_map_it = other._group_map.begin();
  71. other_map_it != other._group_map.end();
  72. ++other_map_it, ++this_map_it)
  73. {
  74. BOOST_ASSERT(this_map_it != _group_map.end());
  75. this_map_it->second = this_list_it;
  76. typename list_type::const_iterator other_list_it = other.get_list_iterator(other_map_it);
  77. typename map_type::const_iterator other_next_map_it = other_map_it;
  78. ++other_next_map_it;
  79. typename list_type::const_iterator other_next_list_it = other.get_list_iterator(other_next_map_it);
  80. while(other_list_it != other_next_list_it)
  81. {
  82. ++other_list_it;
  83. ++this_list_it;
  84. }
  85. }
  86. }
  87. iterator begin()
  88. {
  89. return _list.begin();
  90. }
  91. iterator end()
  92. {
  93. return _list.end();
  94. }
  95. iterator lower_bound(const group_key_type &key)
  96. {
  97. map_iterator map_it = _group_map.lower_bound(key);
  98. return get_list_iterator(map_it);
  99. }
  100. iterator upper_bound(const group_key_type &key)
  101. {
  102. map_iterator map_it = _group_map.upper_bound(key);
  103. return get_list_iterator(map_it);
  104. }
  105. void push_front(const group_key_type &key, const ValueType &value)
  106. {
  107. map_iterator map_it;
  108. if(key.first == front_ungrouped_slots)
  109. {// optimization
  110. map_it = _group_map.begin();
  111. }else
  112. {
  113. map_it = _group_map.lower_bound(key);
  114. }
  115. m_insert(map_it, key, value);
  116. }
  117. void push_back(const group_key_type &key, const ValueType &value)
  118. {
  119. map_iterator map_it;
  120. if(key.first == back_ungrouped_slots)
  121. {// optimization
  122. map_it = _group_map.end();
  123. }else
  124. {
  125. map_it = _group_map.upper_bound(key);
  126. }
  127. m_insert(map_it, key, value);
  128. }
  129. void erase(const group_key_type &key)
  130. {
  131. map_iterator map_it = _group_map.lower_bound(key);
  132. iterator begin_list_it = get_list_iterator(map_it);
  133. iterator end_list_it = upper_bound(key);
  134. if(begin_list_it != end_list_it)
  135. {
  136. _list.erase(begin_list_it, end_list_it);
  137. _group_map.erase(map_it);
  138. }
  139. }
  140. iterator erase(const group_key_type &key, const iterator &it)
  141. {
  142. BOOST_ASSERT(it != _list.end());
  143. map_iterator map_it = _group_map.lower_bound(key);
  144. BOOST_ASSERT(map_it != _group_map.end());
  145. BOOST_ASSERT(weakly_equivalent(map_it->first, key));
  146. if(map_it->second == it)
  147. {
  148. iterator next = it;
  149. ++next;
  150. // if next is in same group
  151. if(next != upper_bound(key))
  152. {
  153. _group_map[key] = next;
  154. }else
  155. {
  156. _group_map.erase(map_it);
  157. }
  158. }
  159. return _list.erase(it);
  160. }
  161. void clear()
  162. {
  163. _list.clear();
  164. _group_map.clear();
  165. }
  166. private:
  167. /* Suppress default assignment operator, since it has the wrong semantics. */
  168. grouped_list& operator=(const grouped_list &other);
  169. bool weakly_equivalent(const group_key_type &arg1, const group_key_type &arg2)
  170. {
  171. if(_group_key_compare(arg1, arg2)) return false;
  172. if(_group_key_compare(arg2, arg1)) return false;
  173. return true;
  174. }
  175. void m_insert(const map_iterator &map_it, const group_key_type &key, const ValueType &value)
  176. {
  177. iterator list_it = get_list_iterator(map_it);
  178. iterator new_it = _list.insert(list_it, value);
  179. if(map_it != _group_map.end() && weakly_equivalent(key, map_it->first))
  180. {
  181. _group_map.erase(map_it);
  182. }
  183. map_iterator lower_bound_it = _group_map.lower_bound(key);
  184. if(lower_bound_it == _group_map.end() ||
  185. weakly_equivalent(lower_bound_it->first, key) == false)
  186. {
  187. /* doing the following instead of just
  188. _group_map[key] = new_it;
  189. to avoid bogus error when enabling checked iterators with g++ */
  190. _group_map.insert(typename map_type::value_type(key, new_it));
  191. }
  192. }
  193. iterator get_list_iterator(const const_map_iterator &map_it)
  194. {
  195. iterator list_it;
  196. if(map_it == _group_map.end())
  197. {
  198. list_it = _list.end();
  199. }else
  200. {
  201. list_it = map_it->second;
  202. }
  203. return list_it;
  204. }
  205. const_iterator get_list_iterator(const const_map_iterator &map_it) const
  206. {
  207. const_iterator list_it;
  208. if(map_it == _group_map.end())
  209. {
  210. list_it = _list.end();
  211. }else
  212. {
  213. list_it = map_it->second;
  214. }
  215. return list_it;
  216. }
  217. list_type _list;
  218. // holds iterators to first list item in each group
  219. map_type _group_map;
  220. group_key_compare_type _group_key_compare;
  221. };
  222. } // end namespace detail
  223. enum connect_position { at_back, at_front };
  224. } // end namespace signals2
  225. } // end namespace boost
  226. #endif // BOOST_SIGNALS2_SLOT_GROUPS_HPP