FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
json/example/use_allocator.cpp at develop · boostorg/json · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
boostorg
/
json
Public
Notifications
You must be signed in to change notification settings
Fork
109
Star
478
Code
Issues
66
Pull requests
26
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
json
/
example
/
use_allocator.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
99 lines (79 loc) · 2.46 KB
Breadcrumbs
json
/
example
/
use_allocator.cpp
Copy path
File metadata and controls
99 lines (79 loc) · 2.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
//
Copyright (c) 2023 Dmitry Arkhipov (grisumbras@yandex.ru)
//
//
Distributed under the Boost Software License, Version 1.0. (See accompanying
//
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
//
Official repository: https://github.com/boostorg/json
//
//
tag::example_use_allocator[]
/*
This example uses a context that stores an allocator to create sequences during conversions
*/
#
include
<
boost/container/pmr/monotonic_buffer_resource.hpp
>
#
include
<
boost/json.hpp
>
#
include
<
boost/system/errc.hpp
>
#
if
defined(BOOST_CLANG) && BOOST_CLANG_VERSION > 40000
#
pragma
clang diagnostic push
#
pragma
clang diagnostic ignored "-Wunused-template"
#
endif
#
include
<
boost/container/pmr/vector.hpp
>
#
if
defined(BOOST_CLANG) && BOOST_CLANG_VERSION > 40000
#
pragma
clang diagnostic pop
#
endif
using
namespace
boost
::json
;
using
namespace
boost
::container
;
template
<
class
Alloc
>
struct
use_allocator_t
{
Alloc allocator;
};
template
<
class
Alloc
>
use_allocator_t
< Alloc >
use_allocator
( Alloc alloc )
noexcept
{
return
{ alloc };
}
template
<
class
T
,
class
Alloc
,
class
FullContext
,
class
=
typename
std::enable_if<
is_sequence_like<T>::value &&
std::uses_allocator<T, Alloc>::value
>::type >
boost::system::result<T>
tag_invoke
( try_value_to_tag<T>,
const
value& jv,
const
use_allocator_t
<Alloc>& ctx,
const
FullContext& full_ctx )
{
array
const
* arr = jv.
if_array
();
if
( !arr )
return
{
boost::system::in_place_error,
make_error_code
(boost::system::errc::invalid_argument) };
T
result
(ctx.
allocator
);
auto
ins =
std::inserter
(result, result.
end
());
for
( value
const
& val: *arr )
{
using
ValueType =
typename
T::value_type;
auto
elem_res = try_value_to<ValueType>( val, full_ctx );
if
( elem_res.
has_error
() )
return
{boost::system::in_place_error, elem_res.
error
()};
*ins++ =
std::move
(elem_res.
unsafe_value
());
}
return
result;
}
int
main
(
int
,
char
**)
{
value
const
jv = {
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
};
unsigned
char
buf[
1024
];
pmr::monotonic_buffer_resource
mr
( buf,
sizeof
(buf) );
auto
v = value_to< pmr::vector<
int
> >(
jv,
use_allocator
( pmr::polymorphic_allocator<
int
>(&mr) ) );
assert
( v.
size
() == jv.
as_array
().
size
() );
for
(
auto
i =
0u
; i < v.
size
(); ++i )
assert
( v[i] == jv.
at
(i).
to_number
<
int
>() );
return
EXIT_SUCCESS
;
}
//
end::example_use_allocator[]
Back
|
FazBrowse Home
|
New Git URL