1 // Boost.Signals2 library 2 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) 9 10 // For more information, see http://www.boost.org 11 12 #ifndef BOOST_SIGNALS2_SLOT_CALL_ITERATOR_HPP 13 #define BOOST_SIGNALS2_SLOT_CALL_ITERATOR_HPP 14 15 #include <boost/assert.hpp> 16 #include <boost/aligned_storage.hpp> 17 #include <boost/iterator/iterator_facade.hpp> 18 #include <boost/optional.hpp> 19 #include <boost/scoped_ptr.hpp> 20 #include <boost/signals2/connection.hpp> 21 #include <boost/signals2/slot_base.hpp> 22 #include <boost/signals2/detail/auto_buffer.hpp> 23 #include <boost/signals2/detail/unique_lock.hpp> 24 #include <boost/type_traits/add_const.hpp> 25 #include <boost/type_traits/add_reference.hpp> 26 #include <boost/weak_ptr.hpp> 27 28 namespace boost { 29 namespace signals2 { 30 namespace detail { 31 template<typename ResultType, typename Function> 32 class slot_call_iterator_cache 33 { 34 public: slot_call_iterator_cache(const Function & f_arg)35 slot_call_iterator_cache(const Function &f_arg): 36 f(f_arg), 37 connected_slot_count(0), 38 disconnected_slot_count(0), 39 m_active_slot(0) 40 {} 41 ~slot_call_iterator_cache()42 ~slot_call_iterator_cache() 43 { 44 if(m_active_slot) 45 { 46 garbage_collecting_lock<connection_body_base> lock(*m_active_slot); 47 m_active_slot->dec_slot_refcount(lock); 48 } 49 } 50 51 template<typename M> set_active_slot(garbage_collecting_lock<M> & lock,connection_body_base * active_slot)52 void set_active_slot(garbage_collecting_lock<M> &lock, 53 connection_body_base *active_slot) 54 { 55 if(m_active_slot) 56 m_active_slot->dec_slot_refcount(lock); 57 m_active_slot = active_slot; 58 if(m_active_slot) 59 m_active_slot->inc_slot_refcount(lock); 60 } 61 62 optional<ResultType> result; 63 typedef auto_buffer<void_shared_ptr_variant, store_n_objects<10> > tracked_ptrs_type; 64 tracked_ptrs_type tracked_ptrs; 65 Function f; 66 unsigned connected_slot_count; 67 unsigned disconnected_slot_count; 68 connection_body_base *m_active_slot; 69 }; 70 71 // Generates a slot call iterator. Essentially, this is an iterator that: 72 // - skips over disconnected slots in the underlying list 73 // - calls the connected slots when dereferenced 74 // - caches the result of calling the slots 75 template<typename Function, typename Iterator, typename ConnectionBody> 76 class slot_call_iterator_t 77 : public boost::iterator_facade<slot_call_iterator_t<Function, Iterator, ConnectionBody>, 78 typename Function::result_type, 79 boost::single_pass_traversal_tag, 80 typename boost::add_const<typename boost::add_reference<typename Function::result_type>::type>::type > 81 { 82 typedef boost::iterator_facade<slot_call_iterator_t<Function, Iterator, ConnectionBody>, 83 typename Function::result_type, 84 boost::single_pass_traversal_tag, 85 typename boost::add_const<typename boost::add_reference<typename Function::result_type>::type>::type > 86 inherited; 87 88 typedef typename Function::result_type result_type; 89 90 typedef slot_call_iterator_cache<result_type, Function> cache_type; 91 92 friend class boost::iterator_core_access; 93 94 public: slot_call_iterator_t(Iterator iter_in,Iterator end_in,cache_type & c)95 slot_call_iterator_t(Iterator iter_in, Iterator end_in, 96 cache_type &c): 97 iter(iter_in), end(end_in), 98 cache(&c), callable_iter(end_in) 99 { 100 lock_next_callable(); 101 } 102 103 typename inherited::reference dereference() const104 dereference() const 105 { 106 if (!cache->result) { 107 try 108 { 109 cache->result.reset(cache->f(*iter)); 110 } 111 catch(expired_slot &) 112 { 113 (*iter)->disconnect(); 114 throw; 115 } 116 } 117 return cache->result.get(); 118 } 119 increment()120 void increment() 121 { 122 ++iter; 123 lock_next_callable(); 124 cache->result.reset(); 125 } 126 equal(const slot_call_iterator_t & other) const127 bool equal(const slot_call_iterator_t& other) const 128 { 129 return iter == other.iter; 130 } 131 132 private: 133 typedef garbage_collecting_lock<connection_body_base> lock_type; 134 set_callable_iter(lock_type & lock,Iterator newValue) const135 void set_callable_iter(lock_type &lock, Iterator newValue) const 136 { 137 callable_iter = newValue; 138 if(callable_iter == end) 139 cache->set_active_slot(lock, 0); 140 else 141 cache->set_active_slot(lock, (*callable_iter).get()); 142 } 143 lock_next_callable() const144 void lock_next_callable() const 145 { 146 if(iter == callable_iter) 147 { 148 return; 149 } 150 if(iter == end) 151 { 152 if(callable_iter != end) 153 { 154 lock_type lock(**callable_iter); 155 set_callable_iter(lock, end); 156 return; 157 } 158 } 159 // we're only locking the first connection body, 160 // but it doesn't matter they all use the same mutex 161 lock_type lock(**iter); 162 for(;iter != end; ++iter) 163 { 164 cache->tracked_ptrs.clear(); 165 (*iter)->nolock_grab_tracked_objects(lock, std::back_inserter(cache->tracked_ptrs)); 166 if((*iter)->nolock_nograb_connected()) 167 { 168 ++cache->connected_slot_count; 169 }else 170 { 171 ++cache->disconnected_slot_count; 172 } 173 if((*iter)->nolock_nograb_blocked() == false) 174 { 175 set_callable_iter(lock, iter); 176 break; 177 } 178 } 179 if(iter == end) 180 { 181 set_callable_iter(lock, end); 182 } 183 } 184 185 mutable Iterator iter; 186 Iterator end; 187 cache_type *cache; 188 mutable Iterator callable_iter; 189 }; 190 } // end namespace detail 191 } // end namespace BOOST_SIGNALS_NAMESPACE 192 } // end namespace boost 193 194 #endif // BOOST_SIGNALS2_SLOT_CALL_ITERATOR_HPP 195