Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-03 08:30:18

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       {
0082         typedef boost::iterator_facade<slot_call_iterator_t<Function, Iterator, ConnectionBody>,
0083           typename Function::result_type,
0084           boost::single_pass_traversal_tag>
0085         inherited;
0086 
0087         typedef typename Function::result_type result_type;
0088 
0089         typedef slot_call_iterator_cache<result_type, Function> cache_type;
0090 
0091         friend class boost::iterator_core_access;
0092 
0093       public:
0094         slot_call_iterator_t(Iterator iter_in, Iterator end_in,
0095           cache_type &c):
0096           iter(iter_in), end(end_in),
0097           cache(&c), callable_iter(end_in)
0098         {
0099           lock_next_callable();
0100         }
0101 
0102         typename inherited::reference
0103         dereference() const
0104         {
0105           if (!cache->result) {
0106             BOOST_TRY
0107             {
0108               cache->result = cache->f(*iter);
0109             }
0110             BOOST_CATCH(expired_slot &)
0111             {
0112               (*iter)->disconnect();
0113               BOOST_RETHROW
0114             }
0115             BOOST_CATCH_END
0116           }
0117           return cache->result.get();
0118         }
0119 
0120         void increment()
0121         {
0122           ++iter;
0123           lock_next_callable();
0124           cache->result.reset();
0125         }
0126 
0127         bool equal(const slot_call_iterator_t& other) const
0128         {
0129           return iter == other.iter;
0130         }
0131 
0132       private:
0133         typedef garbage_collecting_lock<connection_body_base> lock_type;
0134 
0135         void set_callable_iter(lock_type &lock, Iterator newValue) const
0136         {
0137           callable_iter = newValue;
0138           if(callable_iter == end)
0139             cache->set_active_slot(lock, 0);
0140           else
0141             cache->set_active_slot(lock, (*callable_iter).get());
0142         }
0143 
0144         void lock_next_callable() const
0145         {
0146           if(iter == callable_iter)
0147           {
0148             return;
0149           }
0150   
0151           for(;iter != end; ++iter)
0152           {
0153             cache->tracked_ptrs.clear();
0154             lock_type lock(**iter);
0155             (*iter)->nolock_grab_tracked_objects(lock, std::back_inserter(cache->tracked_ptrs));
0156             if((*iter)->nolock_nograb_connected())
0157             {
0158               ++cache->connected_slot_count;
0159             }else
0160             {
0161               ++cache->disconnected_slot_count;
0162             }
0163             if((*iter)->nolock_nograb_blocked() == false)
0164             {
0165               set_callable_iter(lock, iter);
0166               break;
0167             }
0168           }
0169           
0170           if(iter == end)
0171           {
0172             if(callable_iter != end)
0173             {
0174               lock_type lock(**callable_iter);
0175               set_callable_iter(lock, end);
0176             }
0177           }
0178         }
0179 
0180         mutable Iterator iter;
0181         Iterator end;
0182         cache_type *cache;
0183         mutable Iterator callable_iter;
0184       };
0185     } // end namespace detail
0186   } // end namespace BOOST_SIGNALS_NAMESPACE
0187 } // end namespace boost
0188 
0189 #endif // BOOST_SIGNALS2_SLOT_CALL_ITERATOR_HPP