Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:41

0001 // Boost.Signals2 library
0002 
0003 // Copyright Douglas Gregor 2001-2004.
0004 // Copyright Frank Mori Hess 2007-2008.
0005 // Use, modification and
0006 // distribution is subject to the Boost Software License, Version
0007 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0008 // http://www.boost.org/LICENSE_1_0.txt)
0009 
0010 // For more information, see http://www.boost.org
0011 
0012 #ifndef BOOST_SIGNALS2_SLOT_CALL_ITERATOR_HPP
0013 #define BOOST_SIGNALS2_SLOT_CALL_ITERATOR_HPP
0014 
0015 #include <boost/assert.hpp>
0016 #include <boost/core/no_exceptions_support.hpp>
0017 #include <boost/iterator/iterator_facade.hpp>
0018 #include <boost/optional.hpp>
0019 #include <boost/scoped_ptr.hpp>
0020 #include <boost/signals2/connection.hpp>
0021 #include <boost/signals2/slot_base.hpp>
0022 #include <boost/signals2/detail/auto_buffer.hpp>
0023 #include <boost/signals2/detail/unique_lock.hpp>
0024 #include <boost/type_traits/add_const.hpp>
0025 #include <boost/type_traits/add_reference.hpp>
0026 #include <boost/type_traits/aligned_storage.hpp>
0027 #include <boost/weak_ptr.hpp>
0028 
0029 namespace boost {
0030   namespace signals2 {
0031     namespace detail {
0032       template<typename ResultType, typename Function>
0033         class slot_call_iterator_cache
0034       {
0035       public:
0036         slot_call_iterator_cache(const Function &f_arg):
0037           f(f_arg),
0038           connected_slot_count(0),
0039           disconnected_slot_count(0),
0040           m_active_slot(0)
0041         {}
0042 
0043         ~slot_call_iterator_cache()
0044         {
0045           if(m_active_slot)
0046           {
0047             garbage_collecting_lock<connection_body_base> lock(*m_active_slot);
0048             m_active_slot->dec_slot_refcount(lock);
0049           }
0050         }
0051 
0052         template<typename M>
0053         void set_active_slot(garbage_collecting_lock<M> &lock,
0054           connection_body_base *active_slot)
0055         {
0056           if(m_active_slot)
0057             m_active_slot->dec_slot_refcount(lock);
0058           m_active_slot = active_slot;
0059           if(m_active_slot)
0060             m_active_slot->inc_slot_refcount(lock);
0061         }
0062 
0063         optional<ResultType> result;
0064         typedef auto_buffer<void_shared_ptr_variant, store_n_objects<10> > tracked_ptrs_type;
0065         tracked_ptrs_type tracked_ptrs;
0066         Function f;
0067         unsigned connected_slot_count;
0068         unsigned disconnected_slot_count;
0069         connection_body_base *m_active_slot;
0070       };
0071 
0072       // Generates a slot call iterator. Essentially, this is an iterator that:
0073       //   - skips over disconnected slots in the underlying list
0074       //   - calls the connected slots when dereferenced
0075       //   - caches the result of calling the slots
0076       template<typename Function, typename Iterator, typename ConnectionBody>
0077       class slot_call_iterator_t
0078         : public boost::iterator_facade<slot_call_iterator_t<Function, Iterator, ConnectionBody>,
0079         typename Function::result_type,
0080         boost::single_pass_traversal_tag,
0081         typename boost::add_reference<typename boost::add_const<typename Function::result_type>::type>::type >
0082       {
0083         typedef boost::iterator_facade<slot_call_iterator_t<Function, Iterator, ConnectionBody>,
0084           typename Function::result_type,
0085           boost::single_pass_traversal_tag,
0086           typename boost::add_reference<typename boost::add_const<typename Function::result_type>::type>::type >
0087         inherited;
0088 
0089         typedef typename Function::result_type result_type;
0090 
0091         typedef slot_call_iterator_cache<result_type, Function> cache_type;
0092 
0093         friend class boost::iterator_core_access;
0094 
0095       public:
0096         slot_call_iterator_t(Iterator iter_in, Iterator end_in,
0097           cache_type &c):
0098           iter(iter_in), end(end_in),
0099           cache(&c), callable_iter(end_in)
0100         {
0101           lock_next_callable();
0102         }
0103 
0104         typename inherited::reference
0105         dereference() const
0106         {
0107           if (!cache->result) {
0108             BOOST_TRY
0109             {
0110               cache->result.reset(cache->f(*iter));
0111             }
0112             BOOST_CATCH(expired_slot &)
0113             {
0114               (*iter)->disconnect();
0115               BOOST_RETHROW
0116             }
0117             BOOST_CATCH_END
0118           }
0119           return cache->result.get();
0120         }
0121 
0122         void increment()
0123         {
0124           ++iter;
0125           lock_next_callable();
0126           cache->result.reset();
0127         }
0128 
0129         bool equal(const slot_call_iterator_t& other) const
0130         {
0131           return iter == other.iter;
0132         }
0133 
0134       private:
0135         typedef garbage_collecting_lock<connection_body_base> lock_type;
0136 
0137         void set_callable_iter(lock_type &lock, Iterator newValue) const
0138         {
0139           callable_iter = newValue;
0140           if(callable_iter == end)
0141             cache->set_active_slot(lock, 0);
0142           else
0143             cache->set_active_slot(lock, (*callable_iter).get());
0144         }
0145 
0146         void lock_next_callable() const
0147         {
0148           if(iter == callable_iter)
0149           {
0150             return;
0151           }
0152   
0153           for(;iter != end; ++iter)
0154           {
0155             cache->tracked_ptrs.clear();
0156             lock_type lock(**iter);
0157             (*iter)->nolock_grab_tracked_objects(lock, std::back_inserter(cache->tracked_ptrs));
0158             if((*iter)->nolock_nograb_connected())
0159             {
0160               ++cache->connected_slot_count;
0161             }else
0162             {
0163               ++cache->disconnected_slot_count;
0164             }
0165             if((*iter)->nolock_nograb_blocked() == false)
0166             {
0167               set_callable_iter(lock, iter);
0168               break;
0169             }
0170           }
0171           
0172           if(iter == end)
0173           {
0174             if(callable_iter != end)
0175             {
0176               lock_type lock(**callable_iter);
0177               set_callable_iter(lock, end);
0178             }
0179           }
0180         }
0181 
0182         mutable Iterator iter;
0183         Iterator end;
0184         cache_type *cache;
0185         mutable Iterator callable_iter;
0186       };
0187     } // end namespace detail
0188   } // end namespace BOOST_SIGNALS_NAMESPACE
0189 } // end namespace boost
0190 
0191 #endif // BOOST_SIGNALS2_SLOT_CALL_ITERATOR_HPP