null_resource.ipp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_IMPL_NULL_RESOURCE_IPP
  10. #define BOOST_JSON_IMPL_NULL_RESOURCE_IPP
  11. #include <boost/json/null_resource.hpp>
  12. #include <boost/json/detail/except.hpp>
  13. BOOST_JSON_NS_BEGIN
  14. namespace detail {
  15. /** A resource which always fails.
  16. This memory resource always throws the exception
  17. `std::bad_alloc` in calls to `allocate`.
  18. */
  19. class null_resource final
  20. : public memory_resource
  21. {
  22. public:
  23. /// Copy constructor (deleted)
  24. null_resource(
  25. null_resource const&) = delete;
  26. /// Copy assignment (deleted)
  27. null_resource& operator=(
  28. null_resource const&) = delete;
  29. /** Destructor
  30. This destroys the resource.
  31. @par Complexity
  32. Constant.
  33. @part Exception Safety
  34. No-throw guarantee.
  35. */
  36. ~null_resource() noexcept = default;
  37. /** Constructor
  38. This constructs the resource.
  39. @par Complexity
  40. Constant.
  41. @par Exception Safety
  42. No-throw guarantee.
  43. */
  44. /** @{ */
  45. null_resource() noexcept = default;
  46. protected:
  47. void*
  48. do_allocate(
  49. std::size_t,
  50. std::size_t) override
  51. {
  52. detail::throw_bad_alloc(
  53. BOOST_JSON_SOURCE_POS);
  54. }
  55. void
  56. do_deallocate(
  57. void*,
  58. std::size_t,
  59. std::size_t) override
  60. {
  61. // do nothing
  62. }
  63. bool
  64. do_is_equal(
  65. memory_resource const& mr
  66. ) const noexcept override
  67. {
  68. return this == &mr;
  69. }
  70. };
  71. } // detail
  72. memory_resource*
  73. get_null_resource() noexcept
  74. {
  75. static detail::null_resource mr;
  76. return &mr;
  77. }
  78. BOOST_JSON_NS_END
  79. #endif