]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/test/doc_storage_ptr.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / json / test / doc_storage_ptr.cpp
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 #include <boost/json/monotonic_resource.hpp>
11 #include <boost/json/parse.hpp>
12 #include <boost/json/static_resource.hpp>
13 #include <boost/json/storage_ptr.hpp>
14 #include <boost/json/value.hpp>
15
16 #include <iostream>
17
18 #include "test_suite.hpp"
19
20 BOOST_JSON_NS_BEGIN
21
22 //----------------------------------------------------------
23
24 static void set1() {
25
26 //----------------------------------------------------------
27 {
28 //[doc_storage_ptr_1
29 storage_ptr sp1;
30 storage_ptr sp2;
31
32 assert( sp1.get() != nullptr ); // always points to a valid resource
33 assert( sp1.get() == sp2.get() ); // both point to the default resource
34 assert( *sp1.get() == *sp2.get() ); // the default resource compares equal
35 //]
36 }
37 //----------------------------------------------------------
38 {
39 //[doc_storage_ptr_2
40 array arr; // default construction
41 object obj;
42 string str;
43 value jv;
44
45 assert( jv.storage().get() == storage_ptr().get() ); // uses the default memory resource
46 assert( jv.storage().get() == arr.storage().get() ); // both point to the default resource
47 assert( *arr.storage() == *obj.storage() ); // containers use equivalent resources
48 //]
49 }
50 //----------------------------------------------------------
51 {
52 //[doc_storage_ptr_3
53 monotonic_resource mr;
54
55 value const jv = parse( "[1,2,3]", &mr );
56 //]
57 }
58 //----------------------------------------------------------
59
60 } // set1
61
62 //----------------------------------------------------------
63
64 //[doc_storage_ptr_4
65 value parse_value( string_view s)
66 {
67 return parse( s, make_shared_resource< monotonic_resource >() );
68 }
69 //]
70
71 //----------------------------------------------------------
72
73 //[doc_storage_ptr_5
74 template< class Handler >
75 void do_rpc( string_view s, Handler&& h )
76 {
77 unsigned char buffer[ 8192 ]; // Small stack buffer to avoid most allocations during parse
78 monotonic_resource mr( buffer ); // This resource will use our local buffer first
79 value const jv = parse( s, &mr ); // Parse the input string into a value that uses our resource
80 h( jv ); // Call the handler to perform the RPC command
81 }
82 //]
83
84 //----------------------------------------------------------
85
86 void set2() {
87
88 //----------------------------------------------------------
89 {
90 //[doc_storage_ptr_6
91 unsigned char buffer[ 8192 ];
92 static_resource mr( buffer ); // The resource will use our local buffer
93 //]
94 }
95 //----------------------------------------------------------
96 {
97 //[doc_storage_ptr_7
98 monotonic_resource mr;
99 array arr( &mr ); // construct an array using our resource
100 arr.emplace_back( "boost" ); // insert a string
101 assert( *arr[0].as_string().storage() == mr ); // the resource is propagated to the string
102 //]
103 }
104 //----------------------------------------------------------
105 {
106 //[doc_storage_ptr_8
107 {
108 monotonic_resource mr;
109
110 array arr( &mr ); // construct an array using our resource
111
112 assert( ! arr.storage().is_shared() ); // no shared ownership
113 }
114 //]
115 }
116 //----------------------------------------------------------
117 {
118 //[doc_storage_ptr_9
119 storage_ptr sp = make_shared_resource< monotonic_resource >();
120
121 string str( sp );
122
123 assert( sp.is_shared() ); // shared ownership
124 assert( str.storage().is_shared() ); // shared ownership
125 //]
126 }
127 //----------------------------------------------------------
128
129 } // set2
130
131 //----------------------------------------------------------
132 //[doc_storage_ptr_10
133 class logging_resource : public memory_resource
134 {
135 private:
136 void* do_allocate( std::size_t bytes, std::size_t align ) override
137 {
138 std::cout << "Allocating " << bytes << " bytes with alignment " << align << '\n';
139
140 return ::operator new( bytes );
141 }
142
143 void do_deallocate( void* ptr, std::size_t bytes, std::size_t align ) override
144 {
145 std::cout << "Deallocating " << bytes << " bytes with alignment " << align << " @ address " << ptr << '\n';
146
147 return ::operator delete( ptr );
148 }
149
150 bool do_is_equal( memory_resource const& other ) const noexcept override
151 {
152 // since the global allocation and deallocation functions are used,
153 // any instance of a logging_resource can deallocate memory allocated
154 // by another instance of a logging_resource
155
156 return dynamic_cast< logging_resource const* >( &other ) != nullptr;
157 }
158 };
159 //]
160
161 //----------------------------------------------------------
162
163 class doc_storage_ptr_test
164 {
165 public:
166 void
167 run()
168 {
169 (void)&set1;
170 BOOST_TEST_PASS();
171 }
172 };
173
174 TEST_SUITE(doc_storage_ptr_test, "boost.json.doc_storage_ptr");
175
176 BOOST_JSON_NS_END