2
0
mirror of https://github.com/boostorg/json.git synced 2026-02-20 02:42:21 +00:00

Support pilfer construction

This commit is contained in:
Vinnie Falco
2019-09-27 15:06:30 -07:00
parent e0e46cdf4a
commit ec197d4bce
7 changed files with 101 additions and 21 deletions

View File

@@ -13,6 +13,7 @@
#include <boost/json/detail/config.hpp>
#include <boost/json/allocator.hpp>
#include <boost/json/storage.hpp>
#include <boost/pilfer.hpp>
#include <cstdlib>
#include <initializer_list>
#include <iterator>
@@ -105,6 +106,9 @@ public:
BOOST_JSON_DECL
array(array&& other) noexcept;
BOOST_JSON_DECL
array(pilfered<array> other) noexcept;
BOOST_JSON_DECL
array(
array&& other,

View File

@@ -13,6 +13,7 @@
#include <boost/json/array.hpp>
#include <boost/core/exchange.hpp>
#include <boost/assert.hpp>
#include <boost/pilfer.hpp>
#include <algorithm>
#include <cstdlib>
#include <limits>
@@ -220,6 +221,14 @@ array(array&& other) noexcept
{
}
array::
array(pilfered<array> other) noexcept
: tab_(boost::exchange(
other.get().tab_, nullptr))
, sp_(other.get().sp_)
{
}
array::
array(
array&& other,
@@ -497,10 +506,12 @@ reserve(size_type new_capacity)
}
for(size_type i = 0; i < tab_->size; ++i)
::new(&tab->begin()[i]) value_type(
std::move(tab_->begin()[i]));
relocate(
&tab->begin()[i],
tab_->begin()[i]);
tab->size = tab_->size;
std::swap(tab, tab_);
tab->size = 0; // VFALCO Hack
table::destroy(tab, sp_);
}

View File

@@ -327,6 +327,14 @@ object(object&& other) noexcept
{
}
object::
object(pilfered<object> other) noexcept
: sp_(std::move(other.get().sp_))
, tab_(boost::exchange(
other.get().tab_, nullptr))
{
}
object::
object(
object&& other,

View File

@@ -82,6 +82,48 @@ value(value&& other) noexcept
other.kind_ = json::kind::null;
}
value::
value(pilfered<value> p) noexcept
{
auto& other = p.get();
switch(other.kind_)
{
case json::kind::object:
::new(&obj_) object(
pilfer(other.obj_));
break;
case json::kind::array:
::new(&arr_) array(
std::move(other.arr_));
break;
case json::kind::string:
::new(&str_) string(
std::move(other.str_));
break;
case json::kind::number:
::new(&nat_.sp_) storage_ptr(
std::move(other.nat_.sp_));
::new(&nat_.num_) number(
std::move(other.nat_.num_));
break;
case json::kind::boolean:
::new(&nat_.sp_) storage_ptr(
std::move(other.nat_.sp_));
nat_.bool_ = other.nat_.bool_;
break;
case json::kind::null:
::new(&nat_.sp_) storage_ptr(
std::move(other.nat_.sp_));
break;
}
kind_ = other.kind_;
}
value::
value(
value&& other,

View File

@@ -13,6 +13,7 @@
#include <boost/json/detail/config.hpp>
#include <boost/json/storage.hpp>
#include <boost/utility/string_view.hpp>
#include <boost/pilfer.hpp>
#include <cstdlib>
#include <initializer_list>
#include <iterator>
@@ -139,6 +140,9 @@ public:
BOOST_JSON_DECL
object(object&& other) noexcept;
BOOST_JSON_DECL
object(pilfered<object> other) noexcept;
BOOST_JSON_DECL
object(
object&& other,

View File

@@ -21,6 +21,7 @@
#include <boost/json/detail/value.hpp>
#include <boost/type_traits/make_void.hpp>
#include <boost/utility/string_view.hpp>
#include <boost/pilfer.hpp>
#include <cstdlib>
#include <initializer_list>
#include <iosfwd>
@@ -133,6 +134,10 @@ public:
BOOST_JSON_DECL
value(value&& other) noexcept;
/// Pilfer constructor
BOOST_JSON_DECL
value(pilfered<value> other) noexcept;
/// Move construct a value, using the specified storage
BOOST_JSON_DECL
value(