You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
7.1 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/error/unrecoverable.hpp
  3. *
  4. * Purpose: Definition of the \c unrecoverable exception class.
  5. *
  6. * Created: 14th October 2004
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2004-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file stlsoft/error/unrecoverable.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::unrecoverable
  42. * exception class
  43. * (\ref group__library__error "Error" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_ERROR_HPP_UNRECOVERABLE
  46. #define STLSOFT_INCL_STLSOFT_ERROR_HPP_UNRECOVERABLE
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_ERROR_HPP_UNRECOVERABLE_MAJOR 2
  49. # define STLSOFT_VER_STLSOFT_ERROR_HPP_UNRECOVERABLE_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_ERROR_HPP_UNRECOVERABLE_REVISION 2
  51. # define STLSOFT_VER_STLSOFT_ERROR_HPP_UNRECOVERABLE_EDIT 29
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  57. # include <stlsoft/stlsoft.h>
  58. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  59. #ifdef STLSOFT_CF_std_NAMESPACE
  60. # include <exception>
  61. #else /* ? STLSOFT_CF_std_NAMESPACE */
  62. # if defined(STLSOFT_COMPILER_IS_WATCOM)
  63. # include <except.h> // for terminate()
  64. # include <stdexcep.h> // for 'std' exceptions
  65. # else /* ? compiler */
  66. # error No other non-std compiler supported
  67. # endif /* compiler */
  68. #endif /* STLSOFT_CF_std_NAMESPACE */
  69. #if defined(STLSOFT_UNRECOVERABLE_EXCEPTION_USE_WIN32_EXITPROCESS) || \
  70. defined(STLSOFT_COMPILER_IS_MWERKS)
  71. # include <stdlib.h> // for EXIT_FAILURE / exit()
  72. #endif /* compiler */
  73. #if defined(STLSOFT_UNRECOVERABLE_EXCEPTION_USE_WIN32_EXITPROCESS)
  74. # include <windows.h> // for ExitProcess()
  75. #endif /* STLSOFT_UNRECOVERABLE_EXCEPTION_USE_WIN32_EXITPROCESS */
  76. /* /////////////////////////////////////////////////////////////////////////
  77. * Namespace
  78. */
  79. #ifndef _STLSOFT_NO_NAMESPACE
  80. namespace stlsoft
  81. {
  82. #endif /* _STLSOFT_NO_NAMESPACE */
  83. /* /////////////////////////////////////////////////////////////////////////
  84. * Classes
  85. */
  86. /** \brief Unrecoverable exception class
  87. *
  88. * \ingroup group__library__error
  89. *
  90. * Exceptions deriving from this class may be caught, but they result in
  91. * process termination at the end of the catch clause, or if they're not caught.
  92. */
  93. class unrecoverable
  94. : public stlsoft_ns_qual_std(exception)
  95. {
  96. /// \name Types
  97. /// @{
  98. public:
  99. /// The parent type
  100. typedef stlsoft_ns_qual_std(exception) parent_class_type;
  101. /// The type of the current instantiation
  102. typedef unrecoverable class_type;
  103. /// @}
  104. /// \name Construction
  105. /// @{
  106. protected:
  107. /// Default constructor
  108. ss_explicit_k unrecoverable(void (*pfnHandler)() = NULL)
  109. : m_refcnt(new long(1))
  110. , m_pfnHandler(pfnHandler)
  111. {}
  112. public:
  113. /// Copy constructor
  114. ///
  115. /// \note The copy constructor effects a sharing of the internal 'instance count'
  116. /// in order to provide
  117. unrecoverable(class_type const& rhs)
  118. : m_refcnt(rhs.m_refcnt)
  119. , m_pfnHandler(rhs.m_pfnHandler)
  120. {
  121. ++*m_refcnt;
  122. }
  123. /// Destructor
  124. ///
  125. /// \note The destructor of each instance decrements the reference count of the
  126. ///
  127. virtual ~unrecoverable() stlsoft_throw_0()
  128. {
  129. if(0 == --*m_refcnt)
  130. {
  131. delete m_refcnt; // We could forgo this, but might as well be clean
  132. // Invoke the supplied handler
  133. if(NULL != m_pfnHandler)
  134. {
  135. (*m_pfnHandler)();
  136. }
  137. // If no handler was supplied, or it didn't close the process, we
  138. // call terminate to make sure. Terminate is chosen since it often
  139. // results in an "uglier" closedown than exit(), and that's a good
  140. // thing for unrecoverable exceptions
  141. #if defined(STLSOFT_UNRECOVERABLE_EXCEPTION_USE_WIN32_EXITPROCESS)
  142. ::ExitProcess(EXIT_FAILURE);
  143. #else /* ? STLSOFT_UNRECOVERABLE_EXCEPTION_USE_WIN32_EXITPROCESS */
  144. # if defined(STLSOFT_COMPILER_IS_MWERKS)
  145. exit(EXIT_FAILURE);
  146. # else /* ? compiler */
  147. { // Because many compilers (including Borland, DMC++, GCC, etc.) place
  148. // terminate() within std, we introduce a block here, and 'use' the
  149. // std namespace. (Not a thing one normally does, you understand.)
  150. # ifdef STLSOFT_CF_std_NAMESPACE
  151. using namespace ::std;
  152. # endif /* !STLSOFT_CF_std_NAMESPACE */
  153. terminate();
  154. }
  155. # endif /* compiler */
  156. #endif /* STLSOFT_UNRECOVERABLE_EXCEPTION_USE_WIN32_EXITPROCESS */
  157. }
  158. }
  159. /// @}
  160. /// \name Accessors
  161. /// @{
  162. public:
  163. /// Returns a human-readable string describing the exception condition
  164. virtual char const* what() const throw()
  165. {
  166. return "unrecoverable condition";
  167. }
  168. /// @}
  169. /// \name Members
  170. /// @{
  171. private:
  172. long *const m_refcnt;
  173. void (*m_pfnHandler)();
  174. /// @}
  175. // Not to be implemented
  176. private:
  177. class_type& operator =(class_type const&);
  178. void *operator new(ss_size_t );
  179. };
  180. /* ////////////////////////////////////////////////////////////////////// */
  181. #ifndef _STLSOFT_NO_NAMESPACE
  182. } // namespace stlsoft
  183. #endif /* _STLSOFT_NO_NAMESPACE */
  184. /* ////////////////////////////////////////////////////////////////////// */
  185. #endif /* !STLSOFT_INCL_STLSOFT_ERROR_HPP_UNRECOVERABLE */
  186. /* ///////////////////////////// end of file //////////////////////////// */