null_resource.ipp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. 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 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. /** Destructor
  31. This destroys the resource.
  32. @par Complexity
  33. Constant.
  34. @part Exception Safety
  35. No-throw guarantee.
  36. */
  37. ~null_resource() noexcept = default;
  38. /** Constructor
  39. This constructs the resource.
  40. @par Complexity
  41. Constant.
  42. @par Exception Safety
  43. No-throw guarantee.
  44. */
  45. /** @{ */
  46. null_resource() noexcept = default;
  47. protected:
  48. void*
  49. do_allocate(
  50. std::size_t,
  51. std::size_t) override
  52. {
  53. detail::throw_bad_alloc();
  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. } // namespace json
  79. } // namespace boost
  80. #endif