1 // Boost.Signals library
2 
3 // Copyright Douglas Gregor 2001-2004. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 
8 // For more information, see http://www.boost.org
9 
10 #ifndef BOOST_SIGNALS_SLOT_CALL_ITERATOR
11 #define BOOST_SIGNALS_SLOT_CALL_ITERATOR
12 
13 #include <memory>
14 #include <boost/iterator/iterator_facade.hpp>
15 #include <boost/smart_ptr.hpp>
16 #include <boost/signals/detail/config.hpp>
17 #include <boost/signals/connection.hpp>
18 #include <boost/optional.hpp>
19 
20 #ifdef BOOST_HAS_ABI_HEADERS
21 #  include BOOST_ABI_PREFIX
22 #endif
23 
24 namespace boost {
25   namespace BOOST_SIGNALS_NAMESPACE {
26     namespace detail {
27 
28       // Generates a slot call iterator. Essentially, this is an iterator that:
29       //   - skips over disconnected slots in the underlying list
30       //   - calls the connected slots when dereferenced
31       //   - caches the result of calling the slots
32       template<typename Function, typename Iterator>
33       class slot_call_iterator
34         : public iterator_facade<slot_call_iterator<Function, Iterator>,
35                                  typename Function::result_type,
36                                  single_pass_traversal_tag,
37                                  typename Function::result_type const&>
38       {
39         typedef iterator_facade<slot_call_iterator<Function, Iterator>,
40                                 typename Function::result_type,
41                                 single_pass_traversal_tag,
42                                 typename Function::result_type const&>
43           inherited;
44 
45         typedef typename Function::result_type result_type;
46 
47         friend class iterator_core_access;
48 
49       public:
slot_call_iterator(Iterator iter_in,Iterator end_in,Function f,optional<result_type> & c)50         slot_call_iterator(Iterator iter_in, Iterator end_in, Function f,
51                            optional<result_type> &c)
52           : iter(iter_in), end(end_in), f(f), cache(&c)
53         {
54           iter = std::find_if(iter, end, is_callable());
55         }
56 
57         typename inherited::reference
dereference() const58         dereference() const
59         {
60           if (!cache->is_initialized()) {
61             cache->reset(f(*iter));
62           }
63 
64           return cache->get();
65         }
66 
increment()67         void increment()
68         {
69           iter = std::find_if(++iter, end, is_callable());
70           cache->reset();
71         }
72 
equal(const slot_call_iterator & other) const73         bool equal(const slot_call_iterator& other) const
74         {
75           iter = std::find_if(iter, end, is_callable());
76           other.iter = std::find_if(other.iter, other.end,
77                                     is_callable());
78           return iter == other.iter;
79         }
80 
81       private:
82         mutable Iterator iter;
83         Iterator end;
84         Function f;
85         optional<result_type>* cache;
86       };
87     } // end namespace detail
88   } // end namespace BOOST_SIGNALS_NAMESPACE
89 } // end namespace boost
90 
91 #ifdef BOOST_HAS_ABI_HEADERS
92 #  include BOOST_ABI_SUFFIX
93 #endif
94 
95 #endif // BOOST_SIGNALS_SLOT_CALL_ITERATOR
96