Chris@16: // last_value function object (documented as part of Boost.Signals) Chris@16: Chris@16: // Copyright Frank Mori Hess 2007. Chris@16: // Copyright Douglas Gregor 2001-2003. Use, modification and Chris@16: // distribution is subject to the Boost Software License, Version Chris@16: // 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // For more information, see http://www.boost.org Chris@16: Chris@16: #ifndef BOOST_SIGNALS2_LAST_VALUE_HPP Chris@16: #define BOOST_SIGNALS2_LAST_VALUE_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace signals2 { Chris@16: Chris@16: // no_slots_error is thrown when we are unable to generate a return value Chris@16: // due to no slots being connected to the signal. Chris@16: class no_slots_error: public std::exception Chris@16: { Chris@16: public: Chris@16: virtual const char* what() const throw() {return "boost::signals2::no_slots_error";} Chris@16: }; Chris@16: Chris@16: template Chris@16: class last_value { Chris@16: public: Chris@16: typedef T result_type; Chris@16: Chris@16: template Chris@16: T operator()(InputIterator first, InputIterator last) const Chris@16: { Chris@16: if(first == last) Chris@16: { Chris@16: boost::throw_exception(no_slots_error()); Chris@16: } Chris@16: optional value; Chris@16: while (first != last) Chris@16: { Chris@16: try Chris@16: { Chris@16: value = *first; Chris@16: } Chris@16: catch(const expired_slot &) {} Chris@16: ++first; Chris@16: } Chris@16: if(value) return value.get(); Chris@16: boost::throw_exception(no_slots_error()); Chris@16: } Chris@16: }; Chris@16: Chris@16: template<> Chris@16: class last_value { Chris@16: public: Chris@16: typedef void result_type; Chris@16: template Chris@16: result_type operator()(InputIterator first, InputIterator last) const Chris@16: { Chris@16: while (first != last) Chris@16: { Chris@16: try Chris@16: { Chris@16: *first; Chris@16: } Chris@16: catch(const expired_slot &) {} Chris@16: ++first; Chris@16: } Chris@16: return; Chris@16: } Chris@16: }; Chris@16: } // namespace signals2 Chris@16: } // namespace boost Chris@16: #endif // BOOST_SIGNALS2_LAST_VALUE_HPP