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.

206 lines
6.8 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/error/contract_violation.hpp
  3. *
  4. * Purpose: Definition of the \c contract_violation exception class.
  5. *
  6. * Created: 17th 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/contract_violation.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::contract_violation
  42. * exception class
  43. * (\ref group__library__error "Error" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_ERROR_HPP_CONTRACT_VIOLATION
  46. #define STLSOFT_INCL_STLSOFT_ERROR_HPP_CONTRACT_VIOLATION
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_ERROR_HPP_CONTRACT_VIOLATION_MAJOR 2
  49. # define STLSOFT_VER_STLSOFT_ERROR_HPP_CONTRACT_VIOLATION_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_ERROR_HPP_CONTRACT_VIOLATION_REVISION 2
  51. # define STLSOFT_VER_STLSOFT_ERROR_HPP_CONTRACT_VIOLATION_EDIT 20
  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. #ifndef STLSOFT_INCL_STLSOFT_ERROR_HPP_UNRECOVERABLE
  60. # include <stlsoft/error/unrecoverable.hpp>
  61. #endif /* !STLSOFT_INCL_STLSOFT_ERROR_HPP_UNRECOVERABLE */
  62. #ifndef STLSOFT_INCL_STRING
  63. # define STLSOFT_INCL_STRING
  64. # include <string>
  65. #endif /* !STLSOFT_INCL_STRING */
  66. /* /////////////////////////////////////////////////////////////////////////
  67. * Namespace
  68. */
  69. #ifndef _STLSOFT_NO_NAMESPACE
  70. namespace stlsoft
  71. {
  72. #endif /* _STLSOFT_NO_NAMESPACE */
  73. /* /////////////////////////////////////////////////////////////////////////
  74. * Classes
  75. */
  76. /** \brief Thrown when a contract has been violated
  77. *
  78. * \ingroup group__library__error
  79. *
  80. * Exceptions deriving from this class may be caught, but they result in
  81. * process termination at the end of the catch clause, or if they're not caught.
  82. */
  83. class contract_violation
  84. : public unrecoverable
  85. {
  86. /// \name Types
  87. /// @{
  88. private:
  89. #ifdef STLSOFT_CF_std_NAMESPACE
  90. typedef stlsoft_ns_qual_std(string) string_type;
  91. #else /* ? STLSOFT_CF_std_NAMESPACE */
  92. # if defined(STLSOFT_COMPILER_IS_WATCOM)
  93. class string_type
  94. : public stlsoft_ns_qual_std(string)
  95. {
  96. private:
  97. typedef stlsoft_ns_qual_std(string) parent_class_type;
  98. public:
  99. string_type(char const* s) : parent_class_type(s) {}
  100. string_type() : parent_class_type() {}
  101. string_type(string_type const& rhs) : parent_class_type(rhs) {}
  102. public:
  103. char const* c_str() const { return *this; }
  104. bool empty() const { return (NULL == this->c_str() || '\0' == *this->c_str()); }
  105. };
  106. # else /* ? compiler */
  107. # error No other non-std compiler supported
  108. # endif /* compiler */
  109. #endif /* STLSOFT_CF_std_NAMESPACE */
  110. public:
  111. /// The parent type
  112. typedef unrecoverable parent_class_type;
  113. /// The type of the current instantiation
  114. typedef contract_violation class_type;
  115. /// @}
  116. /// \name Construction
  117. /// @{
  118. public:
  119. /// Default constructor
  120. contract_violation()
  121. : parent_class_type(NULL)
  122. , m_error()
  123. {}
  124. /// Creates an instance which will call the given function when termination
  125. /// is effected
  126. ///
  127. /// \note Only when the last copy of this instance is destroyed will the
  128. /// given function by executed
  129. ss_explicit_k contract_violation( void (*pfnHandler)())
  130. : parent_class_type(pfnHandler)
  131. , m_error()
  132. {}
  133. ss_explicit_k contract_violation( char const *error
  134. , void (*pfnHandler)() = NULL)
  135. : parent_class_type(pfnHandler)
  136. , m_error(error)
  137. {}
  138. ss_explicit_k contract_violation( string_type const &error
  139. , void (*pfnHandler)() = NULL)
  140. : parent_class_type(pfnHandler)
  141. , m_error(error)
  142. {}
  143. /// Copy constructor
  144. ///
  145. /// \note Only when the last copy is destroyed will the termination be effected
  146. contract_violation(class_type const& rhs)
  147. : parent_class_type(rhs)
  148. , m_error(rhs.m_error)
  149. {}
  150. virtual ~contract_violation() stlsoft_throw_0()
  151. {}
  152. /// @}
  153. /// \name Accessors
  154. /// @{
  155. public:
  156. /// Returns a human-readable string describing the exception condition
  157. virtual char const* what() const throw()
  158. {
  159. return m_error.empty() ? default_message_() : m_error.c_str();
  160. }
  161. /// @}
  162. /// \name Implementation
  163. /// @{
  164. private:
  165. static char const *default_message_()
  166. {
  167. return "contract violation";
  168. }
  169. /// @}
  170. /// \name Members
  171. /// @{
  172. private:
  173. const string_type m_error;
  174. /// @}
  175. // Not to be implemented
  176. private:
  177. class_type& operator =(class_type const&);
  178. };
  179. /* ////////////////////////////////////////////////////////////////////// */
  180. #ifndef _STLSOFT_NO_NAMESPACE
  181. } // namespace stlsoft
  182. #endif /* _STLSOFT_NO_NAMESPACE */
  183. /* ////////////////////////////////////////////////////////////////////// */
  184. #endif /* !STLSOFT_INCL_STLSOFT_ERROR_HPP_CONTRACT_VIOLATION */
  185. /* ///////////////////////////// end of file //////////////////////////// */