BWAPI
|
00001 // Copyright (c) 2007-08 INRIA Sophia-Antipolis (France). 00002 // All rights reserved. 00003 // 00004 // This file is part of CGAL (www.cgal.org); you may redistribute it under 00005 // the terms of the Q Public License version 1.0. 00006 // See the file LICENSE.QPL distributed with CGAL. 00007 // 00008 // Licensees holding a valid commercial license may use this file in 00009 // accordance with the commercial license agreement provided with the software. 00010 // 00011 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00012 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00013 // 00014 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Surface_mesh_parameterization/include/CGAL/Win32_exception.h $ 00015 // $Id: Win32_exception.h 46214 2008-10-13 08:33:33Z lsaboret $ 00016 // 00017 // Author(s) : Laurent Saboret 00018 00019 #ifndef CGAL_WIN32_EXCEPTION_H 00020 #define CGAL_WIN32_EXCEPTION_H 00021 00022 #include <exception> 00023 00024 #include <windows.h> 00025 #include <eh.h> 00026 00027 00028 CGAL_BEGIN_NAMESPACE 00029 00031 class Win32_exception 00032 { 00033 // Data 00034 private: 00035 unsigned int m_seNumber; 00036 00037 // Public operations 00038 public: 00039 Win32_exception(unsigned int structuredExceptionNumber) 00040 : m_seNumber(structuredExceptionNumber) 00041 {} 00042 00043 unsigned int getStructuredExceptionNumber() const { return m_seNumber; } 00044 }; 00045 00046 00053 class Win32_exception_handler 00054 { 00055 // Data 00056 private: 00057 _se_translator_function m_previous_translator; 00058 00059 // Public operations 00060 public: 00061 00062 Win32_exception_handler() 00063 { 00064 // Protect application against next stack overflow 00065 if (needs_stack_reset(false)) 00066 { 00067 std::cerr << "Win32_exception_handler: reset stack using _resetstkoflw()\n"; 00068 if(!_resetstkoflw()) 00069 { 00070 std::cerr << "Win32_exception_handler: _resetstkoflw() failed, exiting!\n"; 00071 abort(); 00072 } 00073 } 00074 00075 // Set up a function to handle win32 exceptions, 00076 // including stack overflow exceptions. 00077 m_previous_translator = _set_se_translator(translate); 00078 } 00079 00080 ~Win32_exception_handler() 00081 { 00082 // Do not call _resetstkoflw() in this function! 00083 00084 // Restore previous win32 exceptions handler 00085 _set_se_translator(m_previous_translator); 00086 } 00087 00088 // Private operations 00089 private: 00090 00091 // Translate Win32 structured exceptions to C++ exceptions 00092 static 00093 void __cdecl translate(unsigned int code, _EXCEPTION_POINTERS*) 00094 { 00095 // For stack overflow exceptions, set m_needs_stack_reset. 00096 // Use minimal stack space in this function. 00097 // Do not call _resetstkoflw() in this function! 00098 if (code == EXCEPTION_STACK_OVERFLOW) 00099 { 00100 std::cerr << "Win32_exception_handler: stack overflow!\n"; 00101 needs_stack_reset(true); 00102 } 00103 00104 // Throw our own C++ exception object 00105 throw Win32_exception(code); 00106 } 00107 00108 // This method is a trick to allocate a global variable in a header file: 00109 // - get the previous value of m_needs_stack_reset 00110 // - set the new value of m_needs_stack_reset 00111 static 00112 bool needs_stack_reset(bool new_value) 00113 { 00114 // default value 00115 static bool m_needs_stack_reset = false; 00116 00117 bool previous_value = m_needs_stack_reset; 00118 m_needs_stack_reset = new_value; 00119 return previous_value; 00120 } 00121 }; 00122 00123 00124 CGAL_END_NAMESPACE 00125 00126 #endif // CGAL_WIN32_EXCEPTION_H