set_value_const_pass.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 promise::set_value(const R& r);
  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. #ifdef BOOST_MSVC
  21. # pragma warning(disable: 4702) // unreachable code
  22. #endif
  23. struct A
  24. {
  25. A()
  26. {
  27. }
  28. A(const A&)
  29. {
  30. throw 10;
  31. }
  32. };
  33. int main()
  34. {
  35. {
  36. typedef int T;
  37. T i = 3;
  38. boost::promise<T> p;
  39. boost::future<T> f = p.get_future();
  40. p.set_value(i);
  41. ++i;
  42. BOOST_TEST(f.get() == 3);
  43. --i;
  44. try
  45. {
  46. p.set_value(i);
  47. BOOST_TEST(false);
  48. }
  49. catch (const boost::future_error& e)
  50. {
  51. BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied));
  52. }
  53. catch (...)
  54. {
  55. BOOST_TEST(false);
  56. }
  57. }
  58. {
  59. typedef int T;
  60. T i = 3;
  61. boost::promise<T> p;
  62. boost::future<T> f = p.get_future();
  63. p.set_value_deferred(i);
  64. p.notify_deferred();
  65. ++i;
  66. BOOST_TEST(f.get() == 3);
  67. --i;
  68. try
  69. {
  70. p.set_value(i);
  71. BOOST_TEST(false);
  72. }
  73. catch (const boost::future_error& e)
  74. {
  75. BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied));
  76. }
  77. catch (...)
  78. {
  79. BOOST_TEST(false);
  80. }
  81. }
  82. {
  83. typedef A T;
  84. T i;
  85. boost::promise<T> p;
  86. boost::future<T> f = p.get_future();
  87. try
  88. {
  89. p.set_value(i);
  90. BOOST_TEST(false);
  91. }
  92. catch (int j)
  93. {
  94. BOOST_TEST(j == 10);
  95. }
  96. catch (...)
  97. {
  98. BOOST_TEST(false);
  99. }
  100. }
  101. return boost::report_errors();
  102. }