reset_pass.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Copyright (C) 2011 Vicente J. Botet Escriba
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. // <boost/thread/future.hpp>
  14. // class packaged_task<R>
  15. // void operator()();
  16. #define BOOST_THREAD_VERSION 4
  17. #if BOOST_THREAD_VERSION == 4
  18. #define BOOST_THREAD_DETAIL_SIGNATURE double()
  19. #else
  20. #define BOOST_THREAD_DETAIL_SIGNATURE double
  21. #endif
  22. #include <boost/thread/future.hpp>
  23. #include <boost/detail/lightweight_test.hpp>
  24. class A
  25. {
  26. long data_;
  27. public:
  28. explicit A(long i) :
  29. data_(i)
  30. {
  31. }
  32. long operator()() const
  33. {
  34. return data_;
  35. }
  36. long operator()(long i, long j) const
  37. {
  38. if (j == 'z') throw A(6);
  39. return data_ + i + j;
  40. }
  41. };
  42. int main()
  43. {
  44. {
  45. boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p(A(5));
  46. boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
  47. //p(3, 'a');
  48. p();
  49. BOOST_TEST(f.get() == 5.0);
  50. p.reset();
  51. //p(4, 'a');
  52. p();
  53. f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
  54. BOOST_TEST(f.get() == 5.0);
  55. }
  56. {
  57. boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p;
  58. try
  59. {
  60. p.reset();
  61. BOOST_TEST(false);
  62. }
  63. catch (const boost::future_error& e)
  64. {
  65. BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::no_state));
  66. }
  67. }
  68. return boost::report_errors();
  69. }