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