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 ScopeType{st_exit,st_failure,st_success};
  91.  
  92. template<ScopeType scope_type,typename Fun>
  93. class Guard;
  94.  
  95. template<typename Fun>
  96. class Guard<st_success,Fun>
  97. {
  98. boost::uncaught_exception_count_latch latch;
  99. Fun f;
  100. public:
  101. Guard(Fun &&f_)
  102. : f(std::move(f_))
  103. {}
  104. ~Guard()
  105. {
  106. if(!latch.transitioned())
  107. f();
  108. }
  109. };
  110.  
  111. template<typename Fun>
  112. class Guard<st_failure,Fun>
  113. {
  114. boost::uncaught_exception_count_latch latch;
  115. Fun f;
  116. public:
  117. Guard(Fun &&f_)
  118. : f(std::move(f_))
  119. {}
  120. ~Guard()
  121. {
  122. if(latch.transitioned())
  123. f();
  124. }
  125. };
  126.  
  127. template<typename Fun>
  128. class Guard<st_exit,Fun>
  129. {
  130. Fun f;
  131. public:
  132. Guard(Fun &&f_)
  133. : f(std::move(f_))
  134. {}
  135. ~Guard()
  136. {
  137. f();
  138. }
  139. };
  140.  
  141. // http://c...content-available-to-author-only...n.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C
  142. template<ScopeType scope_type>
  143. struct Aux
  144. {
  145. template<typename Fun>
  146. Guard<scope_type,typename std::decay<Fun>::type>
  147. operator*(Fun &&f) const
  148. {
  149. return Guard<scope_type,typename std::decay<Fun>::type>(std::forward<Fun>(f));
  150. }
  151. };
  152.  
  153. #define CONCAT(x,y) CONCAT2(x,y)
  154. #define CONCAT2(x,y) x ## y
  155.  
  156. #define scope(scope_type) auto CONCAT(aux,__LINE__)= Aux<st_##scope_type>()*[&]
  157.  
  158. int main()
  159. {
  160. using namespace std;
  161. {
  162. cout << "success case:" << endl;
  163. scope(exit)
  164. {
  165. cout << "exit" << endl;
  166. };
  167. scope(success)
  168. {
  169. cout << "success" << endl;
  170. };
  171. scope(failure)
  172. {
  173. cout << "failure" << endl;
  174. };
  175. }
  176. cout << string(16,'_') << endl;
  177. try
  178. {
  179. cout << "failure case:" << endl;
  180. scope(exit)
  181. {
  182. cout << "exit" << endl;
  183. };
  184. scope(success)
  185. {
  186. cout << "success" << endl;
  187. };
  188. scope(failure)
  189. {
  190. cout << "failure" << endl;
  191. };
  192. throw 1;
  193. }
  194. catch(int){}
  195. }
  196.  
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
success case:
success
exit
________________
failure case:
failure
exit