null_resource.ipp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  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/throw_exception.hpp>
  13. namespace boost {
  14. namespace json {
  15. namespace detail {
  16. /** A resource which always fails.
  17. This memory resource always throws the exception
  18. `std::bad_alloc` in calls to `allocate`.
  19. */
  20. class null_resource final
  21. : public container::pmr::memory_resource
  22. {
  23. public:
  24. /// Copy constructor (deleted)
  25. null_resource(
  26. null_resource const&) = delete;
  27. /// Copy assignment (deleted)
  28. null_resource& operator=(
  29. null_resource const&) = delete;
  30. /** Constructor
  31. This constructs the resource.
  32. @par Complexity
  33. Constant.
  34. @par Exception Safety
  35. No-throw guarantee.
  36. */
  37. /** @{ */
  38. null_resource() noexcept = default;
  39. protected:
  40. void*
  41. do_allocate(
  42. std::size_t,
  43. std::size_t) override
  44. {
  45. throw_exception( std::bad_alloc(), BOOST_CURRENT_LOCATION );
  46. }
  47. void
  48. do_deallocate(
  49. void*,
  50. std::size_t,
  51. std::size_t) override
  52. {
  53. // do nothing
  54. }
  55. bool
  56. do_is_equal(
  57. memory_resource const& mr
  58. ) const noexcept override
  59. {
  60. return this == &mr;
  61. }
  62. };
  63. } // detail
  64. container::pmr::memory_resource*
  65. get_null_resource() noexcept
  66. {
  67. static detail::null_resource mr;
  68. return &mr;
  69. }
  70. } // namespace json
  71. } // namespace boost
  72. #endif