1 // optional_last_value function object (documented as part of Boost.Signals2) 2 3 // Copyright Frank Mori Hess 2007-2008. 4 // Copyright Douglas Gregor 2001-2003. 5 // Distributed under 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 // See http://www.boost.org/libs/signals2 for library home page. 10 11 #ifndef BOOST_SIGNALS2_OPTIONAL_LAST_VALUE_HPP 12 #define BOOST_SIGNALS2_OPTIONAL_LAST_VALUE_HPP 13 14 #include <boost/core/no_exceptions_support.hpp> 15 #include <boost/optional.hpp> 16 #include <boost/signals2/expired_slot.hpp> 17 18 namespace boost { 19 namespace signals2 { 20 21 template<typename T> 22 class optional_last_value 23 { 24 public: 25 typedef optional<T> result_type; 26 27 template<typename InputIterator> operator ()(InputIterator first,InputIterator last) const28 optional<T> operator()(InputIterator first, InputIterator last) const 29 { 30 optional<T> value; 31 while (first != last) 32 { 33 BOOST_TRY 34 { 35 value = *first; 36 } 37 BOOST_CATCH(const expired_slot &) {} 38 BOOST_CATCH_END 39 ++first; 40 } 41 return value; 42 } 43 }; 44 45 template<> 46 class optional_last_value<void> 47 { 48 public: 49 typedef void result_type; 50 template<typename InputIterator> operator ()(InputIterator first,InputIterator last) const51 result_type operator()(InputIterator first, InputIterator last) const 52 { 53 while (first != last) 54 { 55 BOOST_TRY 56 { 57 *first; 58 } 59 BOOST_CATCH(const expired_slot &) {} 60 BOOST_CATCH_END 61 ++first; 62 } 63 return; 64 } 65 }; 66 } // namespace signals2 67 } // namespace boost 68 #endif // BOOST_SIGNALS2_OPTIONAL_LAST_VALUE_HPP 69