set_exception_pass.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 promise<R>
  15. // void set_exception(exception_ptr p);
  16. #define BOOST_THREAD_VERSION 3
  17. #include <boost/thread/future.hpp>
  18. #include <boost/detail/lightweight_test.hpp>
  19. #include <boost/static_assert.hpp>
  20. namespace boost
  21. {
  22. template <typename T>
  23. struct wrap
  24. {
  25. wrap(T const& v) :
  26. value(v)
  27. {
  28. }
  29. T value;
  30. };
  31. template <typename T>
  32. exception_ptr make_exception_ptr(T v)
  33. {
  34. return copy_exception(wrap<T> (v));
  35. }
  36. }
  37. int main()
  38. {
  39. {
  40. typedef int T;
  41. boost::promise<T> p;
  42. boost::future<T> f = p.get_future();
  43. p.set_exception(boost::make_exception_ptr(3));
  44. try
  45. {
  46. f.get();
  47. BOOST_TEST(false);
  48. }
  49. catch (boost::wrap<int> i)
  50. {
  51. BOOST_TEST(i.value == 3);
  52. }
  53. try
  54. {
  55. p.set_exception(boost::make_exception_ptr(3));
  56. BOOST_TEST(false);
  57. }
  58. catch (const boost::future_error& e)
  59. {
  60. BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied));
  61. }
  62. catch (...)
  63. {
  64. BOOST_TEST(false);
  65. }
  66. }
  67. {
  68. typedef int T;
  69. boost::promise<T> p;
  70. boost::future<T> f = p.get_future();
  71. p.set_exception_deferred(boost::make_exception_ptr(3));
  72. BOOST_TEST(!f.is_ready());
  73. p.notify_deferred();
  74. try
  75. {
  76. f.get();
  77. BOOST_TEST(false);
  78. }
  79. catch (boost::wrap<int> i)
  80. {
  81. BOOST_TEST(i.value == 3);
  82. }
  83. try
  84. {
  85. p.set_exception(boost::make_exception_ptr(3));
  86. BOOST_TEST(false);
  87. }
  88. catch (const boost::future_error& e)
  89. {
  90. BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied));
  91. }
  92. catch (...)
  93. {
  94. BOOST_TEST(false);
  95. }
  96. }
  97. return boost::report_errors();
  98. }