optional_last_value.hpp 1.7 KB

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