1 // Boost.Signals2 library
3 // Copyright Douglas Gregor 2001-2004.
4 // Copyright Frank Mori Hess 2007-2008.
5 // Use, modification and
6 // distribution is subject to the Boost Software License, Version
7 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // For more information, see http://www.boost.org
12 #ifndef BOOST_SIGNALS2_SLOT_CALL_ITERATOR_HPP
13 #define BOOST_SIGNALS2_SLOT_CALL_ITERATOR_HPP
15 #include <boost/assert.hpp>
16 #include <boost/aligned_storage.hpp>
17 #include <boost/core/no_exceptions_support.hpp>
18 #include <boost/iterator/iterator_facade.hpp>
19 #include <boost/optional.hpp>
20 #include <boost/scoped_ptr.hpp>
21 #include <boost/signals2/connection.hpp>
22 #include <boost/signals2/slot_base.hpp>
23 #include <boost/signals2/detail/auto_buffer.hpp>
24 #include <boost/signals2/detail/unique_lock.hpp>
25 #include <boost/type_traits/add_const.hpp>
26 #include <boost/type_traits/add_reference.hpp>
27 #include <boost/weak_ptr.hpp>
32 template<typename ResultType, typename Function>
33 class slot_call_iterator_cache
36 slot_call_iterator_cache(const Function &f_arg):
38 connected_slot_count(0),
39 disconnected_slot_count(0),
43 ~slot_call_iterator_cache()
47 garbage_collecting_lock<connection_body_base> lock(*m_active_slot);
48 m_active_slot->dec_slot_refcount(lock);
53 void set_active_slot(garbage_collecting_lock<M> &lock,
54 connection_body_base *active_slot)
57 m_active_slot->dec_slot_refcount(lock);
58 m_active_slot = active_slot;
60 m_active_slot->inc_slot_refcount(lock);
63 optional<ResultType> result;
64 typedef auto_buffer<void_shared_ptr_variant, store_n_objects<10> > tracked_ptrs_type;
65 tracked_ptrs_type tracked_ptrs;
67 unsigned connected_slot_count;
68 unsigned disconnected_slot_count;
69 connection_body_base *m_active_slot;
72 // Generates a slot call iterator. Essentially, this is an iterator that:
73 // - skips over disconnected slots in the underlying list
74 // - calls the connected slots when dereferenced
75 // - caches the result of calling the slots
76 template<typename Function, typename Iterator, typename ConnectionBody>
77 class slot_call_iterator_t
78 : public boost::iterator_facade<slot_call_iterator_t<Function, Iterator, ConnectionBody>,
79 typename Function::result_type,
80 boost::single_pass_traversal_tag,
81 typename boost::add_const<typename boost::add_reference<typename Function::result_type>::type>::type >
83 typedef boost::iterator_facade<slot_call_iterator_t<Function, Iterator, ConnectionBody>,
84 typename Function::result_type,
85 boost::single_pass_traversal_tag,
86 typename boost::add_const<typename boost::add_reference<typename Function::result_type>::type>::type >
89 typedef typename Function::result_type result_type;
91 typedef slot_call_iterator_cache<result_type, Function> cache_type;
93 friend class boost::iterator_core_access;
96 slot_call_iterator_t(Iterator iter_in, Iterator end_in,
98 iter(iter_in), end(end_in),
99 cache(&c), callable_iter(end_in)
101 lock_next_callable();
104 typename inherited::reference
107 if (!cache->result) {
110 cache->result.reset(cache->f(*iter));
112 BOOST_CATCH(expired_slot &)
114 (*iter)->disconnect();
119 return cache->result.get();
125 lock_next_callable();
126 cache->result.reset();
129 bool equal(const slot_call_iterator_t& other) const
131 return iter == other.iter;
135 typedef garbage_collecting_lock<connection_body_base> lock_type;
137 void set_callable_iter(lock_type &lock, Iterator newValue) const
139 callable_iter = newValue;
140 if(callable_iter == end)
141 cache->set_active_slot(lock, 0);
143 cache->set_active_slot(lock, (*callable_iter).get());
146 void lock_next_callable() const
148 if(iter == callable_iter)
154 if(callable_iter != end)
156 lock_type lock(**callable_iter);
157 set_callable_iter(lock, end);
161 // we're only locking the first connection body,
162 // but it doesn't matter they all use the same mutex
163 lock_type lock(**iter);
164 for(;iter != end; ++iter)
166 cache->tracked_ptrs.clear();
167 (*iter)->nolock_grab_tracked_objects(lock, std::back_inserter(cache->tracked_ptrs));
168 if((*iter)->nolock_nograb_connected())
170 ++cache->connected_slot_count;
173 ++cache->disconnected_slot_count;
175 if((*iter)->nolock_nograb_blocked() == false)
177 set_callable_iter(lock, iter);
183 set_callable_iter(lock, end);
187 mutable Iterator iter;
190 mutable Iterator callable_iter;
192 } // end namespace detail
193 } // end namespace BOOST_SIGNALS_NAMESPACE
194 } // end namespace boost
196 #endif // BOOST_SIGNALS2_SLOT_CALL_ITERATOR_HPP