uncaught_exceptions_np.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright Andrey Semashev 2018.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * https://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file uncaught_exceptions_np.cpp
  9. * \author Andrey Semashev
  10. * \date 2018-11-10
  11. *
  12. * \brief This file contains tests for the uncaught_exceptions function.
  13. *
  14. * This file contains checks that are compiler specific and not quite portable or require C++17.
  15. */
  16. #include <boost/core/uncaught_exceptions.hpp>
  17. #if !defined(BOOST_CORE_UNCAUGHT_EXCEPTIONS_EMULATED)
  18. #include <boost/core/lightweight_test.hpp>
  19. struct my_exception1 {};
  20. struct my_exception2 {};
  21. class exception_watcher2
  22. {
  23. unsigned int& m_count;
  24. public:
  25. explicit exception_watcher2(unsigned int& count) : m_count(count) {}
  26. ~exception_watcher2() { m_count = boost::core::uncaught_exceptions(); }
  27. };
  28. class exception_watcher1
  29. {
  30. unsigned int& m_count1;
  31. unsigned int& m_count2;
  32. public:
  33. exception_watcher1(unsigned int& count1, unsigned int& count2) : m_count1(count1), m_count2(count2) {}
  34. ~exception_watcher1()
  35. {
  36. m_count1 = boost::core::uncaught_exceptions();
  37. try
  38. {
  39. exception_watcher2 watcher2(m_count2);
  40. throw my_exception2();
  41. }
  42. catch (...)
  43. {
  44. }
  45. }
  46. };
  47. // Tests for uncaught_exceptions when used in nested destructors while an exception propagates
  48. void test_in_nested_destructors()
  49. {
  50. const unsigned int root_count = boost::core::uncaught_exceptions();
  51. unsigned int level1_count = root_count, level2_count = root_count;
  52. try
  53. {
  54. exception_watcher1 watcher1(level1_count, level2_count);
  55. throw my_exception1();
  56. }
  57. catch (...)
  58. {
  59. }
  60. BOOST_TEST_NE(root_count, level1_count);
  61. BOOST_TEST_NE(root_count, level2_count);
  62. BOOST_TEST_NE(level1_count, level2_count);
  63. }
  64. int main()
  65. {
  66. test_in_nested_destructors();
  67. return boost::report_errors();
  68. }
  69. #else
  70. int main()
  71. {
  72. return 0;
  73. }
  74. #endif