iutest  1.17.99.14
iutest_exception.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_EXCEPTION_HPP_B73B2E25_97C8_4DFC_BC32_5E53039A1A64_
16 #define INCG_IRIS_IUTEST_EXCEPTION_HPP_B73B2E25_97C8_4DFC_BC32_5E53039A1A64_
17 
18 //======================================================================
19 // include
20 
21 #if IUTEST_HAS_EXCEPTIONS
22 // IWYU pragma: begin_exports
23 #include <exception>
24 // IWYU pragma: end_exports
25 
26 namespace iutest {
27 namespace detail
28 {
29 
35 inline ::std::string FormatCxxException(const char* description)
36 {
37  iu_stringstream strm;
38  if( description != NULL )
39  {
40  strm << "C++ exception with description \"" << description << "\"";
41  }
42  else
43  {
44  strm << "Unknown C++ exception";
45  }
46  return strm.str();
47 }
48 
49 } // end of namespace detail
50 } // end of namespace iutest
51 
52 #if IUTEST_HAS_SEH
53 #include <iomanip>
54 
55 namespace iutest {
56 namespace detail
57 {
58 
62 class seh_exception : public ::std::exception
63 {
64 public:
65  seh_exception() : ::std::exception() {}
66  explicit seh_exception(const char *const& what) : ::std::exception(what) {}
67 public:
68  static void translator(DWORD code, _EXCEPTION_POINTERS* ep)
69  {
70  IUTEST_UNUSED_VAR(ep);
71  iu_stringstream strm;
72 #if IUTEST_HAS_IOMANIP
73  strm << "SEH exception with code 0x" << ::std::setbase(16) << code;
74 #else
75  strm << "SEH exception with code " << code;
76 #endif
77  throw seh_exception(strm.str().c_str());
78  }
79  static int should_process_through_break_and_cppexceptions(DWORD code)
80  {
81  bool should_handle = true;
82  // break point と C++ 例外はハンドリングしない
83  if( code == EXCEPTION_BREAKPOINT )
84  should_handle = false;
85  if( code == kCxxExceptionCode )
86  should_handle = false;
87  return should_handle ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
88  }
89 public:
90  static const DWORD kCxxExceptionCode = 0xe06d7363;
91 };
92 
93 template<typename T>
94 void seh_passthrough(T func)
95 {
96  _EXCEPTION_POINTERS* ep = NULL;
97  __try
98  {
99  (func)();
100  }
101  __except( ep = GetExceptionInformation()
102  , seh_exception::should_process_through_break_and_cppexceptions(GetExceptionCode()) )
103  {
104  seh_exception::translator(GetExceptionCode(), ep);
105  }
106 }
107 
108 
109 } // end of namespace detail
110 } // end of namespace iutest
111 
112 #endif
113 
114 #endif
115 
116 #endif // INCG_IRIS_IUTEST_EXCEPTION_HPP_B73B2E25_97C8_4DFC_BC32_5E53039A1A64_
iutest root namespace
Definition: iutest_charcode.hpp:33