]> git.lyx.org Git - features.git/blob - 3rdparty/boost/boost/signals2/last_value.hpp
Replace Boost.Signals with Boost.Signals2
[features.git] / 3rdparty / boost / boost / signals2 / last_value.hpp
1 // last_value function object (documented as part of Boost.Signals)
2
3 // Copyright Frank Mori Hess 2007.
4 // Copyright Douglas Gregor 2001-2003. Use, modification and
5 // distribution is subject to the Boost Software License, Version
6 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8
9 // For more information, see http://www.boost.org
10
11 #ifndef BOOST_SIGNALS2_LAST_VALUE_HPP
12 #define BOOST_SIGNALS2_LAST_VALUE_HPP
13
14 #include <boost/optional.hpp>
15 #include <boost/signals2/expired_slot.hpp>
16 #include <stdexcept>
17
18 namespace boost {
19   namespace signals2 {
20
21     // no_slots_error is thrown when we are unable to generate a return value
22     // due to no slots being connected to the signal.
23     class no_slots_error: public std::exception
24     {
25     public:
26       virtual const char* what() const throw() {return "boost::signals2::no_slots_error";}
27     };
28
29     template<typename T>
30     class last_value {
31     public:
32       typedef T result_type;
33
34       template<typename InputIterator>
35       T operator()(InputIterator first, InputIterator last) const
36       {
37         if(first == last)
38         {
39           throw no_slots_error();
40         }
41         optional<T> value;
42         while (first != last)
43         {
44           try
45           {
46             value = *first;
47           }
48           catch(const expired_slot &) {}
49           ++first;
50         }
51         if(value) return value.get();
52         throw no_slots_error();
53       }
54     };
55
56     template<>
57     class last_value<void> {
58     public:
59       typedef void result_type;
60       template<typename InputIterator>
61         result_type operator()(InputIterator first, InputIterator last) const
62       {
63         while (first != last)
64         {
65           try
66           {
67             *first;
68           }
69           catch(const expired_slot &) {}
70           ++first;
71         }
72         return;
73       }
74     };
75   } // namespace signals2
76 } // namespace boost
77 #endif // BOOST_SIGNALS2_LAST_VALUE_HPP