1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
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)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/boostorg/json
7  
// Official repository: https://github.com/boostorg/json
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_JSON_IMPL_NULL_RESOURCE_IPP
10  
#ifndef BOOST_JSON_IMPL_NULL_RESOURCE_IPP
11  
#define BOOST_JSON_IMPL_NULL_RESOURCE_IPP
11  
#define BOOST_JSON_IMPL_NULL_RESOURCE_IPP
12  

12  

13  
#include <boost/json/null_resource.hpp>
13  
#include <boost/json/null_resource.hpp>
14  
#include <boost/throw_exception.hpp>
14  
#include <boost/throw_exception.hpp>
15  

15  

16  
namespace boost {
16  
namespace boost {
17  
namespace json {
17  
namespace json {
18  

18  

19  
namespace detail {
19  
namespace detail {
20  

20  

21  
/** A resource which always fails.
21  
/** A resource which always fails.
22  

22  

23  
    This memory resource always throws the exception
23  
    This memory resource always throws the exception
24  
    `std::bad_alloc` in calls to `allocate`.
24  
    `std::bad_alloc` in calls to `allocate`.
25  
*/
25  
*/
26  
class null_resource final
26  
class null_resource final
27  
    : public container::pmr::memory_resource
27  
    : public container::pmr::memory_resource
28  
{
28  
{
29  
public:
29  
public:
30  
    /// Copy constructor (deleted)
30  
    /// Copy constructor (deleted)
31  
    null_resource(
31  
    null_resource(
32  
        null_resource const&) = delete;
32  
        null_resource const&) = delete;
33  

33  

34  
    /// Copy assignment (deleted)
34  
    /// Copy assignment (deleted)
35  
    null_resource& operator=(
35  
    null_resource& operator=(
36  
        null_resource const&) = delete;
36  
        null_resource const&) = delete;
37  

37  

38  
    /** Constructor
38  
    /** Constructor
39  

39  

40  
        This constructs the resource.
40  
        This constructs the resource.
41  

41  

42  
        @par Complexity
42  
        @par Complexity
43  
        Constant.
43  
        Constant.
44  

44  

45  
        @par Exception Safety
45  
        @par Exception Safety
46  
        No-throw guarantee.
46  
        No-throw guarantee.
47  
    */
47  
    */
48  
    /** @{ */
48  
    /** @{ */
49  
    null_resource() noexcept = default;
49  
    null_resource() noexcept = default;
50  

50  

51  
protected:
51  
protected:
52  
    void*
52  
    void*
53  
    do_allocate(
53  
    do_allocate(
54  
        std::size_t,
54  
        std::size_t,
55  
        std::size_t) override
55  
        std::size_t) override
56  
    {
56  
    {
57  
        throw_exception( std::bad_alloc(), BOOST_CURRENT_LOCATION );
57  
        throw_exception( std::bad_alloc(), BOOST_CURRENT_LOCATION );
58  
    }
58  
    }
59  

59  

60  
    void
60  
    void
61  
    do_deallocate(
61  
    do_deallocate(
62  
        void*,
62  
        void*,
63  
        std::size_t,
63  
        std::size_t,
64  
        std::size_t) override
64  
        std::size_t) override
65  
    {
65  
    {
66  
        // do nothing
66  
        // do nothing
67  
    }
67  
    }
68  

68  

69  
    bool
69  
    bool
70  
    do_is_equal(
70  
    do_is_equal(
71  
        memory_resource const& mr
71  
        memory_resource const& mr
72  
            ) const noexcept override
72  
            ) const noexcept override
73  
    {
73  
    {
74  
        return this == &mr;
74  
        return this == &mr;
75  
    }
75  
    }
76  
};
76  
};
77  

77  

78  
} // detail
78  
} // detail
79  

79  

80  
container::pmr::memory_resource*
80  
container::pmr::memory_resource*
81  
get_null_resource() noexcept
81  
get_null_resource() noexcept
82  
{
82  
{
83  
    static detail::null_resource mr;
83  
    static detail::null_resource mr;
84  
    return &mr;
84  
    return &mr;
85  
}
85  
}
86  

86  

87  
} // namespace json
87  
} // namespace json
88  
} // namespace boost
88  
} // namespace boost
89  

89  

90  
#endif
90  
#endif