fork download
  1. // ____________________________________[ uncaught_exception_count.hpp ]____________________________________
  2. // Copyright Evgeny Panasyuk 2012.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://w...content-available-to-author-only...t.org/LICENSE_1_0.txt)
  6.  
  7. // e-mail: E?????[dot]P???????[at]gmail.???
  8.  
  9. #ifndef BOOST_UNCAUGHT_EXCEPTION_COUNT_HPP_39A1E90FC11647e08D5F6ED16CD34B34
  10. #define BOOST_UNCAUGHT_EXCEPTION_COUNT_HPP_39A1E90FC11647e08D5F6ED16CD34B34
  11.  
  12. #if defined(_MSC_VER) || defined(__GNUG__) || defined(__CLANG__)
  13. #define BOOST_UNCAUGHT_EXCEPTION_COUNT_SUPPORTED 1
  14. #endif
  15.  
  16. namespace boost
  17. {
  18. namespace exception_detail
  19. {
  20. template<typename To> inline
  21. To *unrelated_pointer_cast(void *from)
  22. {
  23. return static_cast<To*>(from);
  24. }
  25. }
  26.  
  27. // uncaught_exception_count is a function similar to std::uncaught_exception from standard library,
  28. // but instead of boolean result it returns unsigned int showing current count of uncaught exceptions.
  29.  
  30. #if defined(_MSC_VER)
  31. namespace exception_detail
  32. {
  33. extern "C" char * __cdecl _getptd();
  34. }
  35. inline unsigned uncaught_exception_count()
  36. {
  37. // MSVC specific. Tested on {MSVC2005SP1,MSVC2008SP1,MSVC2010SP1,MSVC2012}x{x32,x64}.
  38. return *exception_detail::unrelated_pointer_cast<unsigned>
  39. (
  40. exception_detail::_getptd() + (sizeof(void*)==8 ? 0x100 : 0x90)
  41. );
  42. }
  43. #elif defined(__GNUG__) || defined(__CLANG__)
  44. namespace exception_detail
  45. {
  46. extern "C" char * __cxa_get_globals();
  47. }
  48. inline unsigned uncaught_exception_count()
  49. {
  50. // Tested on {Clang 3.2,GCC 3.4.6,GCC 4.1.2,GCC 4.4.6,GCC 4.4.7}x{x32,x64}
  51. return *exception_detail::unrelated_pointer_cast<unsigned>
  52. (
  53. exception_detail::__cxa_get_globals() + (sizeof(void*)==8 ? 0x8 : 0x4)
  54. );
  55. }
  56. #endif
  57.  
  58. // Within one scope uncaught_exception_count can be changed only by +1.
  59. // uncaught_exception_count_latch is primitive which helps to determine such transition.
  60. // Internally it stores and compares only last bit of uncaught_exception_count value
  61. #ifdef BOOST_UNCAUGHT_EXCEPTION_COUNT_SUPPORTED
  62. class uncaught_exception_count_latch
  63. {
  64. unsigned char enter_state;
  65. public:
  66. uncaught_exception_count_latch()
  67. : enter_state(static_cast<unsigned char>( uncaught_exception_count() & 1 ))
  68. {
  69. }
  70. bool transitioned() const
  71. {
  72. return enter_state != ( uncaught_exception_count() & 1 );
  73. }
  74. };
  75. #endif
  76.  
  77. }
  78.  
  79. #endif
  80. // ________________________________________________________________________________________________________
  81.  
  82. #include <type_traits>
  83. #include <iostream>
  84. #include <ostream>
  85. #include <utility>
  86. #include <string>
  87.  
  88. // Proof-of-concept:
  89.  
  90. enum Scope {scope_exit, scope_failure, scope_success};
  91.  
  92. template<Scope scope_type, typename F>
  93. class Guard
  94. {
  95. boost::uncaught_exception_count_latch latch;
  96. F f;
  97. public:
  98. Guard(F &&f)
  99. : f(std::move(f))
  100. {}
  101. ~Guard() noexcept(noexcept(f()))
  102. {
  103. if(latch.transitioned() != (scope_type == scope_success))
  104. f();
  105. }
  106. };
  107.  
  108. template<typename F>
  109. class Guard<scope_exit, F>
  110. {
  111. F f;
  112. public:
  113. Guard(F &&f)
  114. : f(std::move(f))
  115. {}
  116. ~Guard() noexcept(noexcept(f()))
  117. {
  118. f();
  119. }
  120. };
  121.  
  122. // http://c...content-available-to-author-only...n.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C
  123. template<Scope scope_type>
  124. struct Aux
  125. {
  126. template<typename F>
  127. Guard<scope_type, typename std::decay<F>::type>
  128. operator*(F &&f) const
  129. {
  130. return Guard<scope_type, typename std::decay<F>::type>( std::forward<F>(f) );
  131. }
  132. };
  133.  
  134. #define CONCAT(x, y) CONCAT2(x, y)
  135. #define CONCAT2(x, y) x ## y
  136.  
  137. #define scope(scope_type) auto CONCAT(aux, __LINE__)= Aux<scope_##scope_type>()*[&]
  138.  
  139. int main()
  140. {
  141. using namespace std;
  142. {
  143. cout << "success case:" << endl;
  144. scope(exit)
  145. {
  146. cout << "exit" << endl;
  147. };
  148. scope(success)
  149. {
  150. cout << "success" << endl;
  151. };
  152. scope(failure)
  153. {
  154. cout << "failure" << endl;
  155. };
  156. }
  157. cout << string(16,'_') << endl;
  158. try
  159. {
  160. cout << "failure case:" << endl;
  161. scope(exit)
  162. {
  163. cout << "exit" << endl;
  164. };
  165. scope(success)
  166. {
  167. cout << "success" << endl;
  168. };
  169. scope(failure)
  170. {
  171. cout << "failure" << endl;
  172. };
  173. throw 1;
  174. }
  175. catch(int){}
  176. }
  177.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
success case:
success
exit
________________
failure case:
failure
exit