1 // Boost.Signals2 library
3 // Copyright Frank Mori Hess 2007-2008.
4 // Use, modification and
5 // distribution is subject to the Boost Software License, Version
6 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // For more information, see http://www.boost.org
11 #ifndef BOOST_SIGNALS2_SLOT_GROUPS_HPP
12 #define BOOST_SIGNALS2_SLOT_GROUPS_HPP
14 #include <boost/signals2/connection.hpp>
15 #include <boost/optional.hpp>
23 enum slot_meta_group {front_ungrouped_slots, grouped_slots, back_ungrouped_slots};
24 template<typename Group>
27 typedef std::pair<enum slot_meta_group, boost::optional<Group> > type;
29 template<typename Group, typename GroupCompare>
35 group_key_less(const GroupCompare &group_compare): _group_compare(group_compare)
37 bool operator ()(const typename group_key<Group>::type &key1, const typename group_key<Group>::type &key2) const
39 if(key1.first != key2.first) return key1.first < key2.first;
40 if(key1.first != grouped_slots) return false;
41 return _group_compare(key1.second.get(), key2.second.get());
44 GroupCompare _group_compare;
46 template<typename Group, typename GroupCompare, typename ValueType>
50 typedef group_key_less<Group, GroupCompare> group_key_compare_type;
52 typedef std::list<ValueType> list_type;
55 typename group_key<Group>::type,
56 typename list_type::iterator,
57 group_key_compare_type
59 typedef typename map_type::iterator map_iterator;
60 typedef typename map_type::const_iterator const_map_iterator;
62 typedef typename list_type::iterator iterator;
63 typedef typename list_type::const_iterator const_iterator;
64 typedef typename group_key<Group>::type group_key_type;
66 grouped_list(const group_key_compare_type &group_key_compare):
67 _group_key_compare(group_key_compare)
69 grouped_list(const grouped_list &other): _list(other._list),
70 _group_map(other._group_map), _group_key_compare(other._group_key_compare)
73 typename map_type::const_iterator other_map_it;
74 typename list_type::iterator this_list_it = _list.begin();
75 typename map_type::iterator this_map_it = _group_map.begin();
76 for(other_map_it = other._group_map.begin();
77 other_map_it != other._group_map.end();
78 ++other_map_it, ++this_map_it)
80 BOOST_ASSERT(this_map_it != _group_map.end());
81 this_map_it->second = this_list_it;
82 typename list_type::const_iterator other_list_it = other.get_list_iterator(other_map_it);
83 typename map_type::const_iterator other_next_map_it = other_map_it;
85 typename list_type::const_iterator other_next_list_it = other.get_list_iterator(other_next_map_it);
86 while(other_list_it != other_next_list_it)
101 iterator lower_bound(const group_key_type &key)
103 map_iterator map_it = _group_map.lower_bound(key);
104 return get_list_iterator(map_it);
106 iterator upper_bound(const group_key_type &key)
108 map_iterator map_it = _group_map.upper_bound(key);
109 return get_list_iterator(map_it);
111 void push_front(const group_key_type &key, const ValueType &value)
114 if(key.first == front_ungrouped_slots)
116 map_it = _group_map.begin();
119 map_it = _group_map.lower_bound(key);
121 m_insert(map_it, key, value);
123 void push_back(const group_key_type &key, const ValueType &value)
126 if(key.first == back_ungrouped_slots)
128 map_it = _group_map.end();
131 map_it = _group_map.upper_bound(key);
133 m_insert(map_it, key, value);
135 void erase(const group_key_type &key)
137 map_iterator map_it = _group_map.lower_bound(key);
138 iterator begin_list_it = get_list_iterator(map_it);
139 iterator end_list_it = upper_bound(key);
140 if(begin_list_it != end_list_it)
142 _list.erase(begin_list_it, end_list_it);
143 _group_map.erase(map_it);
146 iterator erase(const group_key_type &key, const iterator &it)
148 BOOST_ASSERT(it != _list.end());
149 map_iterator map_it = _group_map.lower_bound(key);
150 BOOST_ASSERT(map_it != _group_map.end());
151 BOOST_ASSERT(weakly_equivalent(map_it->first, key));
152 if(map_it->second == it)
156 // if next is in same group
157 if(next != upper_bound(key))
159 _group_map[key] = next;
162 _group_map.erase(map_it);
165 return _list.erase(it);
173 /* Suppress default assignment operator, since it has the wrong semantics. */
174 grouped_list& operator=(const grouped_list &other);
176 bool weakly_equivalent(const group_key_type &arg1, const group_key_type &arg2)
178 if(_group_key_compare(arg1, arg2)) return false;
179 if(_group_key_compare(arg2, arg1)) return false;
182 void m_insert(const map_iterator &map_it, const group_key_type &key, const ValueType &value)
184 iterator list_it = get_list_iterator(map_it);
185 iterator new_it = _list.insert(list_it, value);
186 if(map_it != _group_map.end() && weakly_equivalent(key, map_it->first))
188 _group_map.erase(map_it);
190 map_iterator lower_bound_it = _group_map.lower_bound(key);
191 if(lower_bound_it == _group_map.end() ||
192 weakly_equivalent(lower_bound_it->first, key) == false)
194 /* doing the following instead of just
195 _group_map[key] = new_it;
196 to avoid bogus error when enabling checked iterators with g++ */
197 _group_map.insert(typename map_type::value_type(key, new_it));
200 iterator get_list_iterator(const const_map_iterator &map_it)
203 if(map_it == _group_map.end())
205 list_it = _list.end();
208 list_it = map_it->second;
212 const_iterator get_list_iterator(const const_map_iterator &map_it) const
214 const_iterator list_it;
215 if(map_it == _group_map.end())
217 list_it = _list.end();
220 list_it = map_it->second;
226 // holds iterators to first list item in each group
228 group_key_compare_type _group_key_compare;
230 } // end namespace detail
231 enum connect_position { at_back, at_front };
232 } // end namespace signals2
233 } // end namespace boost
235 #endif // BOOST_SIGNALS2_SLOT_GROUPS_HPP