♻️ move capacity check to set_parent function
This commit is contained in:
parent
bc7e8faa4f
commit
3bb9467073
@ -1304,9 +1304,21 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
reference set_parent(reference j)
|
reference set_parent(reference j, std::size_t old_capacity = -1)
|
||||||
{
|
{
|
||||||
#if JSON_DIAGNOSTICS
|
#if JSON_DIAGNOSTICS
|
||||||
|
if (old_capacity != -1)
|
||||||
|
{
|
||||||
|
// see https://github.com/nlohmann/json/issues/2838
|
||||||
|
JSON_ASSERT(type() == value_t::array);
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity))
|
||||||
|
{
|
||||||
|
// capacity has changed: update all parents
|
||||||
|
set_parents();
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
j.m_parent = this;
|
j.m_parent = this;
|
||||||
#else
|
#else
|
||||||
static_cast<void>(j);
|
static_cast<void>(j);
|
||||||
@ -5371,18 +5383,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add element to array (move semantics)
|
// add element to array (move semantics)
|
||||||
const auto capacity = m_value.array->capacity();
|
const auto old_capacity = m_value.array->capacity();
|
||||||
m_value.array->push_back(std::move(val));
|
m_value.array->push_back(std::move(val));
|
||||||
if (capacity == m_value.array->capacity())
|
set_parent(m_value.array->back(), old_capacity);
|
||||||
{
|
|
||||||
// capacity has not changed: updating parent of last element is sufficient
|
|
||||||
set_parent(m_value.array->back());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// capacity has changed: update all elements' parents
|
|
||||||
set_parents();
|
|
||||||
}
|
|
||||||
// if val is moved from, basic_json move constructor marks it null so we do not call the destructor
|
// if val is moved from, basic_json move constructor marks it null so we do not call the destructor
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -5417,18 +5420,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add element to array
|
// add element to array
|
||||||
const auto capacity = m_value.array->capacity();
|
const auto old_capacity = m_value.array->capacity();
|
||||||
m_value.array->push_back(val);
|
m_value.array->push_back(val);
|
||||||
if (capacity == m_value.array->capacity())
|
set_parent(m_value.array->back(), old_capacity);
|
||||||
{
|
|
||||||
// capacity has not changed: updating parent of last element is sufficient
|
|
||||||
set_parent(m_value.array->back());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// capacity has changed: update all elements' parents
|
|
||||||
set_parents();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -5582,18 +5576,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add element to array (perfect forwarding)
|
// add element to array (perfect forwarding)
|
||||||
const auto capacity = m_value.array->capacity();
|
const auto old_capacity = m_value.array->capacity();
|
||||||
m_value.array->emplace_back(std::forward<Args>(args)...);
|
m_value.array->emplace_back(std::forward<Args>(args)...);
|
||||||
|
return set_parent(m_value.array->back(), old_capacity);
|
||||||
if (capacity == m_value.array->capacity())
|
|
||||||
{
|
|
||||||
// capacity has not changed: updating parent of last element is sufficient
|
|
||||||
return set_parent(m_value.array->back());
|
|
||||||
}
|
|
||||||
|
|
||||||
// capacity has changed: update all elements' parents
|
|
||||||
set_parents();
|
|
||||||
return m_value.array->back();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -18339,9 +18339,21 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
reference set_parent(reference j)
|
reference set_parent(reference j, std::size_t old_capacity = -1)
|
||||||
{
|
{
|
||||||
#if JSON_DIAGNOSTICS
|
#if JSON_DIAGNOSTICS
|
||||||
|
if (old_capacity != -1)
|
||||||
|
{
|
||||||
|
// see https://github.com/nlohmann/json/issues/2838
|
||||||
|
JSON_ASSERT(type() == value_t::array);
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity))
|
||||||
|
{
|
||||||
|
// capacity has changed: update all parents
|
||||||
|
set_parents();
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
j.m_parent = this;
|
j.m_parent = this;
|
||||||
#else
|
#else
|
||||||
static_cast<void>(j);
|
static_cast<void>(j);
|
||||||
@ -22406,18 +22418,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add element to array (move semantics)
|
// add element to array (move semantics)
|
||||||
const auto capacity = m_value.array->capacity();
|
const auto old_capacity = m_value.array->capacity();
|
||||||
m_value.array->push_back(std::move(val));
|
m_value.array->push_back(std::move(val));
|
||||||
if (capacity == m_value.array->capacity())
|
set_parent(m_value.array->back(), old_capacity);
|
||||||
{
|
|
||||||
// capacity has not changed: updating parent of last element is sufficient
|
|
||||||
set_parent(m_value.array->back());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// capacity has changed: update all elements' parents
|
|
||||||
set_parents();
|
|
||||||
}
|
|
||||||
// if val is moved from, basic_json move constructor marks it null so we do not call the destructor
|
// if val is moved from, basic_json move constructor marks it null so we do not call the destructor
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22452,18 +22455,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add element to array
|
// add element to array
|
||||||
const auto capacity = m_value.array->capacity();
|
const auto old_capacity = m_value.array->capacity();
|
||||||
m_value.array->push_back(val);
|
m_value.array->push_back(val);
|
||||||
if (capacity == m_value.array->capacity())
|
set_parent(m_value.array->back(), old_capacity);
|
||||||
{
|
|
||||||
// capacity has not changed: updating parent of last element is sufficient
|
|
||||||
set_parent(m_value.array->back());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// capacity has changed: update all elements' parents
|
|
||||||
set_parents();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -22617,18 +22611,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add element to array (perfect forwarding)
|
// add element to array (perfect forwarding)
|
||||||
const auto capacity = m_value.array->capacity();
|
const auto old_capacity = m_value.array->capacity();
|
||||||
m_value.array->emplace_back(std::forward<Args>(args)...);
|
m_value.array->emplace_back(std::forward<Args>(args)...);
|
||||||
|
return set_parent(m_value.array->back(), old_capacity);
|
||||||
if (capacity == m_value.array->capacity())
|
|
||||||
{
|
|
||||||
// capacity has not changed: updating parent of last element is sufficient
|
|
||||||
return set_parent(m_value.array->back());
|
|
||||||
}
|
|
||||||
|
|
||||||
// capacity has changed: update all elements' parents
|
|
||||||
set_parents();
|
|
||||||
return m_value.array->back();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
Loading…
Reference in New Issue
Block a user