1 | // Boost.Signals library |
---|
2 | |
---|
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) |
---|
7 | |
---|
8 | // For more information, see http://www.boost.org |
---|
9 | |
---|
10 | #ifndef BOOST_SIGNALS_NAMED_SLOT_MAP_HPP |
---|
11 | #define BOOST_SIGNALS_NAMED_SLOT_MAP_HPP |
---|
12 | |
---|
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> |
---|
20 | #include <map> |
---|
21 | #include <memory> |
---|
22 | #include <utility> |
---|
23 | |
---|
24 | namespace boost { namespace BOOST_SIGNALS_NAMESPACE { |
---|
25 | |
---|
26 | enum connect_position { at_back, at_front }; |
---|
27 | |
---|
28 | namespace detail { |
---|
29 | |
---|
30 | class stored_group |
---|
31 | { |
---|
32 | public: |
---|
33 | enum storage_kind { sk_empty, sk_front, sk_back, sk_group }; |
---|
34 | |
---|
35 | stored_group(storage_kind kind = sk_empty) : kind(kind), group() { } |
---|
36 | |
---|
37 | template<typename T> |
---|
38 | stored_group(const T& group) : kind(sk_group), group(new T(group)) { } |
---|
39 | |
---|
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; } |
---|
43 | |
---|
44 | void* get() const { return group.get(); } |
---|
45 | |
---|
46 | private: |
---|
47 | storage_kind kind; |
---|
48 | shared_ptr<void> group; |
---|
49 | }; |
---|
50 | |
---|
51 | typedef function2<bool, stored_group, stored_group> compare_type; |
---|
52 | |
---|
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 { |
---|
58 | public: |
---|
59 | typedef bool result_type; |
---|
60 | typedef const stored_group& first_argument_type; |
---|
61 | typedef const stored_group& second_argument_type; |
---|
62 | |
---|
63 | group_bridge_compare(const Compare& c) : comp(c) |
---|
64 | { } |
---|
65 | |
---|
66 | bool operator()(const stored_group& k1, const stored_group& k2) const |
---|
67 | { |
---|
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; |
---|
72 | |
---|
73 | // Neither is empty, so compare their values to order them |
---|
74 | return comp(*static_cast<Key*>(k1.get()), *static_cast<Key*>(k2.get())); |
---|
75 | } |
---|
76 | |
---|
77 | private: |
---|
78 | Compare comp; |
---|
79 | }; |
---|
80 | |
---|
81 | class BOOST_SIGNALS_DECL named_slot_map_iterator : |
---|
82 | public iterator_facade<named_slot_map_iterator, |
---|
83 | connection_slot_pair, |
---|
84 | forward_traversal_tag> |
---|
85 | { |
---|
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; |
---|
91 | |
---|
92 | typedef iterator_facade<named_slot_map_iterator, |
---|
93 | connection_slot_pair, |
---|
94 | forward_traversal_tag> inherited; |
---|
95 | public: |
---|
96 | named_slot_map_iterator() : slot_assigned(false) |
---|
97 | { } |
---|
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) |
---|
101 | { |
---|
102 | if (slot_assigned) slot_ = other.slot_; |
---|
103 | } |
---|
104 | named_slot_map_iterator& operator=(const named_slot_map_iterator& other) |
---|
105 | { |
---|
106 | slot_assigned = other.slot_assigned; |
---|
107 | group = other.group; |
---|
108 | last_group = other.last_group; |
---|
109 | if (slot_assigned) slot_ = other.slot_; |
---|
110 | return *this; |
---|
111 | } |
---|
112 | connection_slot_pair& dereference() const |
---|
113 | { |
---|
114 | return *slot_; |
---|
115 | } |
---|
116 | void increment() |
---|
117 | { |
---|
118 | ++slot_; |
---|
119 | if (slot_ == group->second.end()) { |
---|
120 | ++group; |
---|
121 | init_next_group(); |
---|
122 | } |
---|
123 | } |
---|
124 | bool equal(const named_slot_map_iterator& other) const { |
---|
125 | return (group == other.group |
---|
126 | && (group == last_group |
---|
127 | || slot_ == other.slot_)); |
---|
128 | } |
---|
129 | |
---|
130 | #if BOOST_WORKAROUND(_MSC_VER, <= 1400) |
---|
131 | void decrement(); |
---|
132 | void advance(difference_type); |
---|
133 | #endif |
---|
134 | |
---|
135 | private: |
---|
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) |
---|
142 | { } |
---|
143 | |
---|
144 | void init_next_group() |
---|
145 | { |
---|
146 | while (group != last_group && group->second.empty()) ++group; |
---|
147 | if (group != last_group) { |
---|
148 | slot_ = group->second.begin(); |
---|
149 | slot_assigned = true; |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | group_iterator group; |
---|
154 | group_iterator last_group; |
---|
155 | slot_pair_iterator slot_; |
---|
156 | bool slot_assigned; |
---|
157 | |
---|
158 | friend class named_slot_map; |
---|
159 | }; |
---|
160 | |
---|
161 | class BOOST_SIGNALS_DECL named_slot_map |
---|
162 | { |
---|
163 | public: |
---|
164 | typedef named_slot_map_iterator iterator; |
---|
165 | |
---|
166 | named_slot_map(const compare_type& compare); |
---|
167 | |
---|
168 | void clear(); |
---|
169 | iterator begin(); |
---|
170 | iterator end(); |
---|
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(); |
---|
176 | |
---|
177 | private: |
---|
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; |
---|
182 | |
---|
183 | bool empty(const_group_iterator group) const |
---|
184 | { |
---|
185 | return (group->second.empty() && group != groups.begin() && group != back); |
---|
186 | } |
---|
187 | slot_container_type groups; |
---|
188 | group_iterator back; |
---|
189 | }; |
---|
190 | |
---|
191 | } } } |
---|
192 | |
---|
193 | #endif // BOOST_SIGNALS_NAMED_SLOT_MAP_HPP |
---|