Skip to content

Commit 133b48d

Browse files
committed
use value_from for initializer_list construction
1 parent 4df9a86 commit 133b48d

File tree

7 files changed

+61
-25
lines changed

7 files changed

+61
-25
lines changed

doc/pages/dom/init_lists.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ Do not create variables of type {std_initializer_list}. This may result in
9797
temporaries being destroyed before the initializer list is used.
9898
====
9999

100+
Initializer lists use <<ref_value_from>> conversion under the hood if the
101+
element is not natively convertible to `value`. For example:
102+
103+
[source]
104+
----
105+
include::../../../test/snippets.cpp[tag=snippet_init_list_10,indent=0]
106+
----
107+
100108
In all cases, the <<ref_storage_ptr>> owned by an <<ref_object>>,
101109
<<ref_array>>, or <<ref_value>> constructed from an initializer list will be
102110
propagated to each element, recursively.

include/boost/json/detail/parse_into.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <boost/json/error.hpp>
1717
#include <boost/json/conversion.hpp>
18+
#include <boost/json/value.hpp>
1819
#include <boost/describe/enum_from_string.hpp>
1920

2021
#include <vector>

include/boost/json/detail/value_from.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ifndef BOOST_JSON_DETAIL_VALUE_FROM_HPP
1313
#define BOOST_JSON_DETAIL_VALUE_FROM_HPP
1414

15+
#include <boost/json/value.hpp>
1516
#include <boost/json/conversion.hpp>
1617
#include <boost/describe/enum_to_string.hpp>
1718
#include <boost/mp11/algorithm.hpp>

include/boost/json/impl/conversion.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#define BOOST_JSON_IMPL_CONVERSION_HPP
1313

1414
#include <boost/json/fwd.hpp>
15-
#include <boost/json/value.hpp>
1615
#include <boost/json/string_view.hpp>
1716
#include <boost/describe/enumerators.hpp>
1817
#include <boost/describe/members.hpp>
@@ -30,6 +29,9 @@
3029

3130
namespace boost {
3231
namespace json {
32+
33+
class value_ref;
34+
3335
namespace detail {
3436

3537
#ifdef __cpp_lib_nonmember_container_access
@@ -196,6 +198,7 @@ struct object_conversion_tag : native_conversion_tag { };
196198
struct array_conversion_tag : native_conversion_tag { };
197199
struct string_conversion_tag : native_conversion_tag { };
198200
struct bool_conversion_tag : native_conversion_tag { };
201+
struct value_ref_tag : native_conversion_tag { };
199202
struct number_conversion_tag : native_conversion_tag { };
200203
struct integral_conversion_tag : number_conversion_tag { };
201204
struct floating_point_conversion_tag : number_conversion_tag { };
@@ -358,6 +361,9 @@ using native_conversion_category = mp11::mp_cond<
358361
// generic conversions
359362
template< class T >
360363
using generic_conversion_category = mp11::mp_cond<
364+
// std::is_same<T,std::initializer_list<value_ref>>, init_list_tag,
365+
std::is_same<T, value_ref>, value_ref_tag,
366+
361367
std::is_same<T, bool>, bool_conversion_tag,
362368
std::is_integral<T>, integral_conversion_tag,
363369
std::is_floating_point<T>, floating_point_conversion_tag,

include/boost/json/impl/value_ref.hpp

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,30 @@
1010
#ifndef BOOST_JSON_IMPL_VALUE_REF_HPP
1111
#define BOOST_JSON_IMPL_VALUE_REF_HPP
1212

13+
#include <boost/json/value_from.hpp>
14+
1315
namespace boost {
1416
namespace json {
1517

1618
template<class T>
1719
value
18-
value_ref::
19-
from_builtin(
20-
void const* p,
21-
storage_ptr sp) noexcept
20+
value_ref::from_builtin(void const* p, storage_ptr sp) noexcept
2221
{
23-
return value(
24-
*reinterpret_cast<
25-
T const*>(p),
26-
std::move(sp));
22+
return value( *reinterpret_cast<T const*>(p), std::move(sp) );
2723
}
2824

2925
template<class T>
3026
value
31-
value_ref::
32-
from_const(
33-
void const* p,
34-
storage_ptr sp)
27+
value_ref::from_const(void const* p, storage_ptr sp)
3528
{
36-
return value(
37-
*reinterpret_cast<
38-
T const*>(p),
39-
std::move(sp));
29+
return value_from( *reinterpret_cast<T const*>(p), std::move(sp) );
4030
}
4131

4232
template<class T>
4333
value
44-
value_ref::
45-
from_rvalue(
46-
void* p,
47-
storage_ptr sp)
34+
value_ref::from_rvalue(void* p, storage_ptr sp)
4835
{
49-
return value(
50-
std::move(
51-
*reinterpret_cast<T*>(p)),
52-
std::move(sp));
36+
return value_from( std::move(*reinterpret_cast<T*>(p)), std::move(sp) );
5337
}
5438

5539
} // namespace json

test/snippets.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,20 @@ usingInitLists()
616616

617617
(void)jo2;
618618
}
619+
620+
{
621+
// tag::snippet_init_list_10[]
622+
623+
value jv = { { "fibs", std::vector<int>{1,2,3,5,8} } };
624+
625+
array& fibs = jv.at("fibs").as_array();
626+
627+
assert(( fibs == array{1,2,3,5,8} ));
628+
629+
// end::snippet_init_list_10[]
630+
631+
(void)fibs;
632+
}
619633
}
620634

621635
//----------------------------------------------------------

test/value_ref.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,27 @@ class value_ref_test
389389
}
390390
}
391391

392+
void
393+
testAutoConversion()
394+
{
395+
value jv = { 1, false, std::vector<int>{1,2,3} };
396+
auto& ja = jv.as_array();
397+
BOOST_TEST(ja.size() == 3);
398+
BOOST_TEST(ja[0] == 1);
399+
BOOST_TEST(ja[1] == false);
400+
BOOST_TEST(( ja[2].as_array() == array{1,2,3} ));
401+
402+
jv = { {"a", std::vector<int>{1,2,3}}, {"b", std::string("foo")} };
403+
auto& jo = jv.as_object();
404+
BOOST_TEST(jo.size() == 2);
405+
BOOST_TEST(( jo["a"] == array{1,2,3} ));
406+
BOOST_TEST(( jo["b"] == "foo" ));
407+
408+
jv = {std::string("bar")};
409+
BOOST_TEST( jv.is_string() );
410+
BOOST_TEST(( jv == "bar" ));
411+
}
412+
392413
void
393414
run()
394415
{
@@ -397,6 +418,7 @@ class value_ref_test
397418
testMakeValue();
398419
testObjects();
399420
testMoveFrom();
421+
testAutoConversion();
400422
}
401423
};
402424

0 commit comments

Comments
 (0)