except.ipp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/json
  8. //
  9. #ifndef BOOST_JSON_DETAIL_IMPL_EXCEPT_IPP
  10. #define BOOST_JSON_DETAIL_IMPL_EXCEPT_IPP
  11. #include <boost/json/detail/except.hpp>
  12. #ifndef BOOST_JSON_STANDALONE
  13. # include <boost/version.hpp>
  14. # include <boost/throw_exception.hpp>
  15. #elif defined(BOOST_JSON_STANDALONE) && defined(BOOST_NO_EXCEPTIONS)
  16. # include <exception>
  17. #endif
  18. #include <stdexcept>
  19. #if defined(BOOST_JSON_STANDALONE)
  20. namespace boost {
  21. #if defined(BOOST_NO_EXCEPTIONS)
  22. // When exceptions are disabled
  23. // in standalone, you must provide
  24. // this function.
  25. BOOST_NORETURN
  26. void
  27. throw_exception(std::exception const&);
  28. #endif
  29. } // boost
  30. #endif
  31. BOOST_JSON_NS_BEGIN
  32. namespace detail {
  33. #if defined(BOOST_JSON_STANDALONE) && \
  34. ! defined(BOOST_NO_EXCEPTIONS)
  35. // this is in the json namespace to avoid
  36. // colliding with boost::throw_exception
  37. template<class E>
  38. void
  39. BOOST_NORETURN
  40. throw_exception(E e)
  41. {
  42. throw e;
  43. }
  44. #endif
  45. void
  46. throw_bad_alloc(
  47. source_location const& loc)
  48. {
  49. (void)loc;
  50. throw_exception(
  51. std::bad_alloc()
  52. #if ! defined(BOOST_JSON_STANDALONE)
  53. , loc
  54. #endif
  55. );
  56. }
  57. void
  58. throw_length_error(
  59. char const* what,
  60. source_location const& loc)
  61. {
  62. (void)loc;
  63. throw_exception(
  64. std::length_error(what)
  65. #if ! defined(BOOST_JSON_STANDALONE)
  66. , loc
  67. #endif
  68. );
  69. }
  70. void
  71. throw_invalid_argument(
  72. char const* what,
  73. source_location const& loc)
  74. {
  75. (void)loc;
  76. throw_exception(
  77. std::invalid_argument(what)
  78. #if ! defined(BOOST_JSON_STANDALONE)
  79. , loc
  80. #endif
  81. );
  82. }
  83. void
  84. throw_out_of_range(
  85. source_location const& loc)
  86. {
  87. (void)loc;
  88. throw_exception(
  89. std::out_of_range(
  90. "out of range")
  91. #if ! defined(BOOST_JSON_STANDALONE)
  92. , loc
  93. #endif
  94. );
  95. }
  96. void
  97. throw_system_error(
  98. error_code const& ec,
  99. source_location const& loc)
  100. {
  101. (void)loc;
  102. throw_exception(
  103. system_error(ec)
  104. #if ! defined(BOOST_JSON_STANDALONE)
  105. , loc
  106. #endif
  107. );
  108. }
  109. } // detail
  110. BOOST_JSON_NS_END
  111. #endif