]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/test/doc_storage_ptr.cpp
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
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)
7 // Official repository: https://github.com/boostorg/json
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>
18 #include "test_suite.hpp"
22 //----------------------------------------------------------
26 //----------------------------------------------------------
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
37 //----------------------------------------------------------
40 array arr
; // default construction
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
50 //----------------------------------------------------------
53 monotonic_resource mr
;
55 value
const jv
= parse( "[1,2,3]", &mr
);
58 //----------------------------------------------------------
62 //----------------------------------------------------------
65 value
parse_value( string_view s
)
67 return parse( s
, make_shared_resource
< monotonic_resource
>() );
71 //----------------------------------------------------------
74 template< class Handler
>
75 void do_rpc( string_view s
, Handler
&& h
)
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
84 //----------------------------------------------------------
88 //----------------------------------------------------------
91 unsigned char buffer
[ 8192 ];
92 static_resource
mr( buffer
); // The resource will use our local buffer
95 //----------------------------------------------------------
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
104 //----------------------------------------------------------
108 monotonic_resource mr
;
110 array
arr( &mr
); // construct an array using our resource
112 assert( ! arr
.storage().is_shared() ); // no shared ownership
116 //----------------------------------------------------------
119 storage_ptr sp
= make_shared_resource
< monotonic_resource
>();
123 assert( sp
.is_shared() ); // shared ownership
124 assert( str
.storage().is_shared() ); // shared ownership
127 //----------------------------------------------------------
131 //----------------------------------------------------------
132 //[doc_storage_ptr_10
133 class logging_resource
: public memory_resource
136 void* do_allocate( std::size_t bytes
, std::size_t align
) override
138 std::cout
<< "Allocating " << bytes
<< " bytes with alignment " << align
<< '\n';
140 return ::operator new( bytes
);
143 void do_deallocate( void* ptr
, std::size_t bytes
, std::size_t align
) override
145 std::cout
<< "Deallocating " << bytes
<< " bytes with alignment " << align
<< " @ address " << ptr
<< '\n';
147 return ::operator delete( ptr
);
150 bool do_is_equal( memory_resource
const& other
) const noexcept override
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
156 return dynamic_cast< logging_resource
const* >( &other
) != nullptr;
161 //----------------------------------------------------------
163 class doc_storage_ptr_test
174 TEST_SUITE(doc_storage_ptr_test
, "boost.json.doc_storage_ptr");