/src/boost/boost/json/impl/null_resource.ipp
Line | Count | Source (jump to first uncovered line) |
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/json/detail/except.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 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 | | /** Destructor |
39 | | |
40 | | This destroys the resource. |
41 | | |
42 | | @par Complexity |
43 | | Constant. |
44 | | |
45 | | @part Exception Safety |
46 | | No-throw guarantee. |
47 | | */ |
48 | | ~null_resource() noexcept = default; |
49 | | |
50 | | /** Constructor |
51 | | |
52 | | This constructs the resource. |
53 | | |
54 | | @par Complexity |
55 | | Constant. |
56 | | |
57 | | @par Exception Safety |
58 | | No-throw guarantee. |
59 | | */ |
60 | | /** @{ */ |
61 | | null_resource() noexcept = default; |
62 | | |
63 | | protected: |
64 | | void* |
65 | | do_allocate( |
66 | | std::size_t, |
67 | | std::size_t) override |
68 | 80 | { |
69 | 80 | detail::throw_bad_alloc(); |
70 | 80 | } |
71 | | |
72 | | void |
73 | | do_deallocate( |
74 | | void*, |
75 | | std::size_t, |
76 | | std::size_t) override |
77 | 0 | { |
78 | | // do nothing |
79 | 0 | } |
80 | | |
81 | | bool |
82 | | do_is_equal( |
83 | | memory_resource const& mr |
84 | | ) const noexcept override |
85 | 0 | { |
86 | 0 | return this == &mr; |
87 | 0 | } |
88 | | }; |
89 | | |
90 | | } // detail |
91 | | |
92 | | memory_resource* |
93 | | get_null_resource() noexcept |
94 | 2.66k | { |
95 | 2.66k | static detail::null_resource mr; |
96 | 2.66k | return &mr; |
97 | 2.66k | } |
98 | | |
99 | | } // namespace json |
100 | | } // namespace boost |
101 | | |
102 | | #endif |