errors.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision$
  10. //
  11. // Description : defines runtime parameters setup error
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_UTILS_RUNTIME_INIT_ERROR_HPP
  14. #define BOOST_TEST_UTILS_RUNTIME_INIT_ERROR_HPP
  15. // Boost.Test Runtime parameters
  16. #include <boost/test/utils/runtime/fwd.hpp>
  17. // Boost.Test
  18. #include <boost/test/utils/string_cast.hpp>
  19. // Boost.Test
  20. #include <boost/config.hpp>
  21. // STL
  22. #include <exception>
  23. #include <vector>
  24. #include <boost/test/detail/suppress_warnings.hpp>
  25. namespace boost {
  26. namespace runtime {
  27. // ************************************************************************** //
  28. // ************** runtime::param_error ************** //
  29. // ************************************************************************** //
  30. class BOOST_SYMBOL_VISIBLE param_error : public std::exception {
  31. public:
  32. ~param_error() BOOST_NOEXCEPT_OR_NOTHROW {}
  33. virtual const char * what() const BOOST_NOEXCEPT_OR_NOTHROW
  34. {
  35. return msg.c_str();
  36. }
  37. cstring param_name;
  38. std::string msg;
  39. protected:
  40. explicit param_error( cstring param_name_ ) : param_name( param_name_) {}
  41. };
  42. //____________________________________________________________________________//
  43. class BOOST_SYMBOL_VISIBLE init_error : public param_error {
  44. protected:
  45. explicit init_error( cstring param_name ) : param_error( param_name ) {}
  46. ~init_error() BOOST_NOEXCEPT_OR_NOTHROW {}
  47. };
  48. class BOOST_SYMBOL_VISIBLE input_error : public param_error {
  49. protected:
  50. explicit input_error( cstring param_name ) : param_error( param_name ) {}
  51. ~input_error() BOOST_NOEXCEPT_OR_NOTHROW {}
  52. };
  53. //____________________________________________________________________________//
  54. template<typename Derived, typename Base>
  55. class BOOST_SYMBOL_VISIBLE specific_param_error : public Base {
  56. protected:
  57. explicit specific_param_error( cstring param_name ) : Base( param_name ) {}
  58. ~specific_param_error() BOOST_NOEXCEPT_OR_NOTHROW {}
  59. public:
  60. //____________________________________________________________________________//
  61. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
  62. !defined(BOOST_NO_CXX11_REF_QUALIFIERS)
  63. Derived operator<<(char const* val) &&
  64. {
  65. this->msg.append( val );
  66. return static_cast<Derived&&>(*this);
  67. }
  68. //____________________________________________________________________________//
  69. template<typename T>
  70. Derived operator<<(T const& val) &&
  71. {
  72. this->msg.append( unit_test::utils::string_cast( val ) );
  73. return static_cast<Derived&&>(*this);
  74. }
  75. //____________________________________________________________________________//
  76. #else
  77. Derived const& operator<<(char const* val) const
  78. {
  79. const_cast<specific_param_error<Derived, Base>&>(*this).msg.append( val );
  80. return static_cast<Derived const&>(*this);
  81. }
  82. //____________________________________________________________________________//
  83. template<typename T>
  84. Derived const& operator<<(T const& val) const
  85. {
  86. const_cast<specific_param_error<Derived, Base>&>(*this).msg.append( unit_test::utils::string_cast( val ) );
  87. return static_cast<Derived const&>(*this);
  88. }
  89. //____________________________________________________________________________//
  90. #endif
  91. };
  92. // ************************************************************************** //
  93. // ************** specific exception types ************** //
  94. // ************************************************************************** //
  95. #define SPECIFIC_EX_TYPE( type, base ) \
  96. class BOOST_SYMBOL_VISIBLE type : public specific_param_error<type,base> { \
  97. public: \
  98. explicit type( cstring param_name = cstring() ) \
  99. : specific_param_error<type,base>( param_name ) \
  100. {} \
  101. } \
  102. /**/
  103. SPECIFIC_EX_TYPE( invalid_cla_id, init_error );
  104. SPECIFIC_EX_TYPE( duplicate_param, init_error );
  105. SPECIFIC_EX_TYPE( conflicting_param, init_error );
  106. SPECIFIC_EX_TYPE( unknown_param, init_error );
  107. SPECIFIC_EX_TYPE( access_to_missing_argument, init_error );
  108. SPECIFIC_EX_TYPE( arg_type_mismatch, init_error );
  109. SPECIFIC_EX_TYPE( invalid_param_spec, init_error );
  110. SPECIFIC_EX_TYPE( format_error, input_error );
  111. SPECIFIC_EX_TYPE( duplicate_arg, input_error );
  112. SPECIFIC_EX_TYPE( missing_req_arg, input_error );
  113. #undef SPECIFIC_EX_TYPE
  114. class BOOST_SYMBOL_VISIBLE ambiguous_param : public specific_param_error<ambiguous_param, input_error> {
  115. public:
  116. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  117. explicit ambiguous_param( std::vector<cstring>&& amb_candidates )
  118. : specific_param_error<ambiguous_param,input_error>( "" )
  119. , m_amb_candidates( std::move( amb_candidates ) ) {}
  120. #else
  121. explicit ambiguous_param( std::vector<cstring> const& amb_candidates )
  122. : specific_param_error<ambiguous_param,input_error>( "" )
  123. , m_amb_candidates( amb_candidates ) {}
  124. #endif
  125. ~ambiguous_param() BOOST_NOEXCEPT_OR_NOTHROW {}
  126. std::vector<cstring> m_amb_candidates;
  127. };
  128. class BOOST_SYMBOL_VISIBLE unrecognized_param : public specific_param_error<unrecognized_param, input_error> {
  129. public:
  130. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  131. explicit unrecognized_param( std::vector<cstring>&& type_candidates )
  132. : specific_param_error<unrecognized_param,input_error>( "" )
  133. , m_typo_candidates( std::move( type_candidates ) ) {}
  134. #else
  135. explicit unrecognized_param( std::vector<cstring> const& type_candidates )
  136. : specific_param_error<unrecognized_param,input_error>( "" )
  137. , m_typo_candidates( type_candidates ) {}
  138. #endif
  139. ~unrecognized_param() BOOST_NOEXCEPT_OR_NOTHROW {}
  140. std::vector<cstring> m_typo_candidates;
  141. };
  142. } // namespace runtime
  143. } // namespace boost
  144. #include <boost/test/detail/enable_warnings.hpp>
  145. #endif // BOOST_TEST_UTILS_RUNTIME_INIT_ERROR_HPP