fix doc example typo
[boost.git] / boost / signals2 / last_value.hpp
blobc5eb2f6b3f2b5b44d3515cbda5a50383f6104555
1 // last_value function object (documented as part of Boost.Signals)
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)
9 // For more information, see http://www.boost.org
11 #ifndef BOOST_SIGNALS2_LAST_VALUE_HPP
12 #define BOOST_SIGNALS2_LAST_VALUE_HPP
14 #include <boost/optional.hpp>
15 #include <boost/signals2/detail/result_type_wrapper.hpp>
16 #include <boost/throw_exception.hpp>
17 #include <stdexcept>
19 namespace boost {
20 namespace signals2 {
21 class expired_slot;
23 // no_slots_error is thrown when we are unable to generate a return value
24 // due to no slots being connected to the signal.
25 class no_slots_error: public std::exception
27 public:
28 virtual const char* what() const throw() {return "boost::signals2::no_slots_error";}
31 template<typename T>
32 class last_value {
33 public:
34 typedef T result_type;
36 template<typename InputIterator>
37 T operator()(InputIterator first, InputIterator last) const
39 T * resolver = 0;
40 if(first == last)
42 boost::throw_exception(no_slots_error());
44 optional<T> value;
45 while (first != last)
47 try
49 value = *first;
51 catch(const expired_slot &) {}
52 ++first;
54 if(value) return value.get();
55 boost::throw_exception(no_slots_error());
59 template<>
60 class last_value<void> {
61 public:
62 typedef detail::result_type_wrapper<void>::type result_type;
63 template<typename InputIterator>
64 result_type operator()(InputIterator first, InputIterator last) const
66 while (first != last)
68 try
70 *first;
72 catch(const expired_slot &) {}
73 ++first;
75 return result_type();
78 } // namespace signals2
79 } // namespace boost
80 #endif // BOOST_SIGNALS2_LAST_VALUE_HPP