1 // Boost.Signals library
3 // Copyright Douglas Gregor 2001-2004. 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)
8 // For more information, see http://www.boost.org
10 #ifndef BOOST_SIGNALS_NAMED_SLOT_MAP_HPP
11 #define BOOST_SIGNALS_NAMED_SLOT_MAP_HPP
13 #include <boost/signals/detail/config.hpp>
14 #include <boost/signals/detail/signals_common.hpp>
15 #include <boost/signals/connection.hpp>
16 #include <boost/utility.hpp>
17 #include <boost/shared_ptr.hpp>
18 #include <boost/function/function2.hpp>
19 #include <boost/iterator/iterator_facade.hpp>
24 namespace boost { namespace BOOST_SIGNALS_NAMESPACE {
26 enum connect_position { at_back, at_front };
33 enum storage_kind { sk_empty, sk_front, sk_back, sk_group };
35 stored_group(storage_kind kind = sk_empty) : kind(kind), group() { }
38 stored_group(const T& group) : kind(sk_group), group(new T(group)) { }
40 bool is_front() const { return kind == sk_front; }
41 bool is_back() const { return kind == sk_back; }
42 bool empty() const { return kind == sk_empty; }
44 void* get() const { return group.get(); }
48 shared_ptr<void> group;
51 typedef function2<bool, stored_group, stored_group> compare_type;
53 // This function object bridges from a pair of any objects that hold
54 // values of type Key to the underlying function object that compares
55 // values of type Key.
56 template<typename Compare, typename Key>
57 class group_bridge_compare {
59 typedef bool result_type;
60 typedef const stored_group& first_argument_type;
61 typedef const stored_group& second_argument_type;
63 group_bridge_compare(const Compare& c) : comp(c)
66 bool operator()(const stored_group& k1, const stored_group& k2) const
68 if (k1.is_front()) return !k2.is_front();
69 if (k1.is_back()) return false;
70 if (k2.is_front()) return false;
71 if (k2.is_back()) return true;
73 // Neither is empty, so compare their values to order them
74 return comp(*static_cast<Key*>(k1.get()), *static_cast<Key*>(k2.get()));
81 class BOOST_SIGNALS_DECL named_slot_map_iterator :
82 public iterator_facade<named_slot_map_iterator,
84 forward_traversal_tag>
86 typedef std::list<connection_slot_pair> group_list;
87 typedef group_list::iterator slot_pair_iterator;
88 typedef std::map<stored_group, group_list, compare_type> slot_container_type;
89 typedef slot_container_type::iterator group_iterator;
90 typedef slot_container_type::const_iterator const_group_iterator;
92 typedef iterator_facade<named_slot_map_iterator,
94 forward_traversal_tag> inherited;
96 named_slot_map_iterator() : slot_assigned(false)
98 named_slot_map_iterator(const named_slot_map_iterator& other)
99 : group(other.group), last_group(other.last_group),
100 slot_assigned(other.slot_assigned)
102 if (slot_assigned) slot_ = other.slot_;
104 named_slot_map_iterator& operator=(const named_slot_map_iterator& other)
106 slot_assigned = other.slot_assigned;
108 last_group = other.last_group;
109 if (slot_assigned) slot_ = other.slot_;
112 connection_slot_pair& dereference() const
119 if (slot_ == group->second.end()) {
124 bool equal(const named_slot_map_iterator& other) const {
125 return (group == other.group
126 && (group == last_group
127 || slot_ == other.slot_));
130 #if BOOST_WORKAROUND(_MSC_VER, <= 1500)
132 void advance(difference_type);
136 named_slot_map_iterator(group_iterator group, group_iterator last) :
137 group(group), last_group(last), slot_assigned(false)
138 { init_next_group(); }
139 named_slot_map_iterator(group_iterator group, group_iterator last,
140 slot_pair_iterator slot) :
141 group(group), last_group(last), slot_(slot), slot_assigned(true)
144 void init_next_group()
146 while (group != last_group && group->second.empty()) ++group;
147 if (group != last_group) {
148 slot_ = group->second.begin();
149 slot_assigned = true;
153 group_iterator group;
154 group_iterator last_group;
155 slot_pair_iterator slot_;
158 friend class named_slot_map;
161 class BOOST_SIGNALS_DECL named_slot_map
164 typedef named_slot_map_iterator iterator;
166 named_slot_map(const compare_type& compare);
171 iterator insert(const stored_group& name, const connection& con,
172 const any& slot, connect_position at);
173 void disconnect(const stored_group& name);
174 void erase(iterator pos);
175 void remove_disconnected_slots();
178 typedef std::list<connection_slot_pair> group_list;
179 typedef std::map<stored_group, group_list, compare_type> slot_container_type;
180 typedef slot_container_type::iterator group_iterator;
181 typedef slot_container_type::const_iterator const_group_iterator;
183 bool empty(const_group_iterator group) const
185 return (group->second.empty() && group != groups.begin() && group != back);
187 slot_container_type groups;
193 #endif // BOOST_SIGNALS_NAMED_SLOT_MAP_HPP