1 // Boost.Signals library
3 // Copyright (C) 2001-2 Doug Gregor (gregod@cs.rpi.edu)
5 // Permission to copy, use, sell and distribute this software is granted
6 // provided this copyright notice appears in all copies.
7 // Permission to modify the code and to distribute modified code is granted
8 // provided this copyright notice appears in all copies, and a notice
9 // that the code was modified is included with the copyright notice.
11 // This software is provided "as is" without express or implied warranty,
12 // and with no claim as to its suitability for any purpose.
14 // For more information, see http://www.boost.org
16 #ifndef BOOST_SIGNALS_SIGNAL_BASE_HEADER
17 #define BOOST_SIGNALS_SIGNAL_BASE_HEADER
19 #include <boost/signals/detail/signals_common.hpp>
20 #include <boost/signals/connection.hpp>
21 #include <boost/signals/trackable.hpp>
22 #include <boost/smart_ptr.hpp>
23 #include <boost/any.hpp>
24 #include <boost/utility.hpp>
25 #include <boost/function/function2.hpp>
33 // Forward declaration for the mapping from slot names to connections
36 // This function object bridges from a pair of any objects that hold
37 // values of type Key to the underlying function object that compares
38 // values of type Key.
39 template<typename Compare, typename Key>
40 class any_bridge_compare {
42 typedef bool result_type;
43 typedef const any& first_argument_type;
44 typedef const any& second_argument_type;
46 any_bridge_compare(const Compare& c) : comp(c) {}
48 bool operator()(const any& k1, const any& k2) const
50 // if k1 is empty, then it precedes nothing
54 // if k2 is empty, then k1 must precede it
58 // Neither is empty, so compare their values to order them
59 // The strange */& is so that we will get a reference to the
60 // value stored in the any object instead of a copy
61 return comp(*any_cast<Key>(&k1), *any_cast<Key>(&k2));
68 // Must be constructed before calling the slots, because it safely
70 class call_notification {
72 call_notification(const shared_ptr<signal_base_impl>&);
75 shared_ptr<signal_base_impl> impl;
78 // Implementation of base class for all signals. It handles the
79 // management of the underlying slot lists.
80 class signal_base_impl {
82 friend class call_notification;
84 typedef function2<bool, any, any> compare_type;
86 // Make sure that an exception does not cause the "clearing" flag to
88 class temporarily_set_clearing {
90 temporarily_set_clearing(signal_base_impl* b) : base(b)
92 base->flags.clearing = true;
95 ~temporarily_set_clearing()
97 base->flags.clearing = false;
101 signal_base_impl* base;
104 friend class temporarily_set_clearing;
106 signal_base_impl(const compare_type&);
109 // Disconnect all slots connected to this signal
110 void disconnect_all_slots();
112 // Are there any connected slots?
115 // Disconnect all slots in the given group
116 void disconnect(const any&);
118 // We're being notified that a slot has disconnected
119 static void slot_disconnected(void* obj, void* data);
121 connection connect_slot(const any& slot,
123 const std::vector<const trackable*>&);
126 // Remove all of the slots that have been marked "disconnected"
127 void remove_disconnected_slots() const;
130 // Our call depth when invoking slots (> 1 when we have a loop)
131 mutable int call_depth;
134 // True if some slots have disconnected, but we were not able to
135 // remove them from the list of slots because there are valid
136 // iterators into the slot list
137 mutable bool delayed_disconnect:1;
139 // True if we are disconnecting all disconnected slots
144 typedef std::multimap<any, connection_slot_pair, compare_type>
146 typedef slot_container_type::iterator slot_iterator;
147 typedef slot_container_type::value_type stored_slot_type;
148 mutable slot_container_type slots;
151 class signal_base : public noncopyable {
153 typedef signal_base_impl::compare_type compare_type;
155 friend class call_notification;
157 signal_base(const compare_type& comp) : impl()
159 impl.reset(new signal_base_impl(comp));
165 // Disconnect all slots connected to this signal
166 void disconnect_all_slots() { impl->disconnect_all_slots(); }
168 // Are there any connected slots?
169 bool empty() const { return impl->empty(); }
172 connection connect_slot(const any& slot,
174 const std::vector<const trackable*>& bound)
176 return impl->connect_slot(slot, name, bound);
179 typedef signal_base_impl::slot_iterator slot_iterator;
180 typedef signal_base_impl::stored_slot_type stored_slot_type;
182 shared_ptr<signal_base_impl> impl;
184 } // end namespace detail
185 } // end namespace signals
186 } // end namespace boost
188 #endif // BOOST_SIGNALS_SIGNAL_BASE_HEADER