regex_error.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file regex_error.hpp
  3. /// Contains the definition of the regex_error exception class.
  4. //
  5. // Copyright 2008 Eric Niebler. Distributed under the Boost
  6. // Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_XPRESSIVE_REGEX_ERROR_HPP_EAN_10_04_2005
  9. #define BOOST_XPRESSIVE_REGEX_ERROR_HPP_EAN_10_04_2005
  10. // MS compatible compilers support #pragma once
  11. #if defined(_MSC_VER)
  12. # pragma once
  13. #endif
  14. #include <string>
  15. #include <stdexcept>
  16. #include <boost/throw_exception.hpp>
  17. #include <boost/current_function.hpp>
  18. #include <boost/exception/exception.hpp>
  19. #include <boost/exception/info.hpp>
  20. #include <boost/xpressive/regex_constants.hpp>
  21. //{{AFX_DOC_COMMENT
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // This is a hack to get Doxygen to show the inheritance relation between
  24. // regex_error and std::runtime_error.
  25. #ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED
  26. /// INTERNAL ONLY
  27. namespace std
  28. {
  29. /// INTERNAL ONLY
  30. struct runtime_error {};
  31. }
  32. #endif
  33. //}}AFX_DOC_COMMENT
  34. namespace boost { namespace xpressive
  35. {
  36. ////////////////////////////////////////////////////////////////////////////////
  37. // regex_error
  38. //
  39. /// \brief The class regex_error defines the type of objects thrown as
  40. /// exceptions to report errors during the conversion from a string representing
  41. /// a regular expression to a finite state machine.
  42. struct regex_error
  43. : std::runtime_error
  44. , boost::exception
  45. {
  46. /// Constructs an object of class regex_error.
  47. /// \param code The error_type this regex_error represents.
  48. /// \param str The message string of this regex_error.
  49. /// \post code() == code
  50. explicit regex_error(regex_constants::error_type code, char const *str = "")
  51. : std::runtime_error(str)
  52. , boost::exception()
  53. , code_(code)
  54. {
  55. }
  56. /// Accessor for the error_type value
  57. /// \return the error_type code passed to the constructor
  58. /// \throw nothrow
  59. regex_constants::error_type code() const
  60. {
  61. return this->code_;
  62. }
  63. /// Destructor for class regex_error
  64. /// \throw nothrow
  65. virtual ~regex_error() throw()
  66. {}
  67. private:
  68. regex_constants::error_type code_;
  69. };
  70. namespace detail
  71. {
  72. inline bool ensure_(
  73. bool cond
  74. , regex_constants::error_type code
  75. , char const *msg
  76. , char const *fun
  77. , char const *file
  78. , unsigned long line
  79. )
  80. {
  81. if(!cond)
  82. {
  83. #ifndef BOOST_EXCEPTION_DISABLE
  84. boost::throw_exception(
  85. boost::xpressive::regex_error(code, msg)
  86. << boost::throw_function(fun)
  87. << boost::throw_file(file)
  88. << boost::throw_line((int)line)
  89. );
  90. #else
  91. boost::throw_exception(boost::xpressive::regex_error(code, msg));
  92. #endif
  93. }
  94. return true;
  95. }
  96. }
  97. #define BOOST_XPR_ENSURE_(pred, code, msg) \
  98. boost::xpressive::detail::ensure_(!!(pred), code, msg, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__) \
  99. /**/
  100. }} // namespace boost::xpressive
  101. #endif