optional_last_value.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/move/utility_core.hpp>
  12. #include <boost/optional.hpp>
  13. #include <boost/signals2/expired_slot.hpp>
  14. namespace boost {
  15. namespace signals2 {
  16. template<typename T>
  17. class optional_last_value
  18. {
  19. public:
  20. typedef optional<T> result_type;
  21. template<typename InputIterator>
  22. optional<T> operator()(InputIterator first, InputIterator last) const
  23. {
  24. optional<T> value;
  25. while (first != last)
  26. {
  27. BOOST_TRY
  28. {
  29. value = boost::move_if_not_lvalue_reference<T>(*first);
  30. }
  31. BOOST_CATCH(const expired_slot &) {}
  32. BOOST_CATCH_END
  33. ++first;
  34. }
  35. return value;
  36. }
  37. };
  38. template<>
  39. class optional_last_value<void>
  40. {
  41. public:
  42. typedef void result_type;
  43. template<typename InputIterator>
  44. result_type operator()(InputIterator first, InputIterator last) const
  45. {
  46. while (first != last)
  47. {
  48. BOOST_TRY
  49. {
  50. *first;
  51. }
  52. BOOST_CATCH(const expired_slot &) {}
  53. BOOST_CATCH_END
  54. ++first;
  55. }
  56. return;
  57. }
  58. };
  59. } // namespace signals2
  60. } // namespace boost
  61. #endif // BOOST_SIGNALS2_OPTIONAL_LAST_VALUE_HPP