♻️ split set_parent function

This commit is contained in:
Niels Lohmann
2021-01-14 21:55:49 +01:00
parent 10751d1189
commit b9d3aa4067
4 changed files with 110 additions and 114 deletions
+44 -46
View File
@@ -1258,44 +1258,42 @@ class basic_json
#endif
}
reference set_parent(reference j, bool recursive)
void set_parents()
{
#if JSON_DIAGNOSTICS
if (recursive)
switch (m_type)
{
switch (m_type)
case value_t::array:
{
case value_t::array:
for (auto& element : *m_value.array)
{
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
break;
element.m_parent = this;
}
case value_t::object:
{
for (auto& element : *m_value.object)
{
element.second.m_parent = this;
}
break;
}
default:
break;
break;
}
case value_t::object:
{
for (auto& element : *m_value.object)
{
element.second.m_parent = this;
}
break;
}
default:
break;
}
else
{
j.m_parent = this;
}
#endif
}
reference set_parent(reference j)
{
#if JSON_DIAGNOSTICS
j.m_parent = this;
#else
static_cast<void>(j);
static_cast<void>(recursive);
#endif
return j;
}
@@ -1513,7 +1511,7 @@ class basic_json
std::forward<CompatibleType>(val))))
{
JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
set_parent(*this, true);
set_parents();
assert_invariant();
}
@@ -1592,7 +1590,7 @@ class basic_json
default: // LCOV_EXCL_LINE
JSON_ASSERT(false); // LCOV_EXCL_LINE
}
set_parent(*this, true);
set_parents();
assert_invariant();
}
@@ -1719,7 +1717,7 @@ class basic_json
m_value.array = create<array_t>(init.begin(), init.end());
}
set_parent(*this, true);
set_parents();
assert_invariant();
}
@@ -1929,7 +1927,7 @@ class basic_json
: m_type(value_t::array)
{
m_value.array = create<array_t>(cnt, val);
set_parent(*this, true);
set_parents();
assert_invariant();
}
@@ -2082,7 +2080,7 @@ class basic_json
JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), diagnostics_t()));
}
set_parent(*this, true);
set_parents();
assert_invariant();
}
@@ -2181,7 +2179,7 @@ class basic_json
break;
}
set_parent(*this, true);
set_parents();
assert_invariant();
}
@@ -2222,7 +2220,7 @@ class basic_json
other.m_type = value_t::null;
other.m_value = {};
set_parent(*this, true);
set_parents();
assert_invariant();
}
@@ -2263,7 +2261,7 @@ class basic_json
swap(m_type, other.m_type);
swap(m_value, other.m_value);
set_parent(*this, true);
set_parents();
assert_invariant();
return *this;
}
@@ -3388,7 +3386,7 @@ class basic_json
{
JSON_TRY
{
return set_parent(m_value.array->at(idx), false);
return set_parent(m_value.array->at(idx));
}
JSON_CATCH (std::out_of_range&)
{
@@ -3486,7 +3484,7 @@ class basic_json
{
JSON_TRY
{
return set_parent(m_value.object->at(key), false);
return set_parent(m_value.object->at(key));
}
JSON_CATCH (std::out_of_range&)
{
@@ -3684,7 +3682,7 @@ class basic_json
// operator[] only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
return set_parent(m_value.object->operator[](key), false);
return set_parent(m_value.object->operator[](key));
}
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this)));
@@ -3774,7 +3772,7 @@ class basic_json
// at only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
return set_parent(m_value.object->operator[](key), false);
return set_parent(m_value.object->operator[](key));
}
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this)));
@@ -5330,7 +5328,7 @@ class basic_json
// add element to array (move semantics)
m_value.array->push_back(std::move(val));
set_parent(m_value.array->back(), false);
set_parent(m_value.array->back());
// if val is moved from, basic_json move constructor marks it null so we do not call the destructor
}
@@ -5366,7 +5364,7 @@ class basic_json
// add element to array
m_value.array->push_back(val);
set_parent(m_value.array->back(), false);
set_parent(m_value.array->back());
}
/*!
@@ -5417,7 +5415,7 @@ class basic_json
// add element to object
auto res = m_value.object->insert(val);
set_parent(res.first->second, false);
set_parent(res.first->second);
}
/*!
@@ -5521,10 +5519,10 @@ class basic_json
// add element to array (perfect forwarding)
#ifdef JSON_HAS_CPP_17
return set_parent(m_value.array->emplace_back(std::forward<Args>(args)...), false);
return set_parent(m_value.array->emplace_back(std::forward<Args>(args)...));
#else
m_value.array->emplace_back(std::forward<Args>(args)...);
return set_parent(m_value.array->back(), false);
return set_parent(m_value.array->back());
#endif
}
@@ -5574,7 +5572,7 @@ class basic_json
// add element to array (perfect forwarding)
auto res = m_value.object->emplace(std::forward<Args>(args)...);
set_parent(res.first->second, false);
set_parent(res.first->second);
// create result iterator and set iterator to the result of emplace
auto it = begin();
@@ -6007,7 +6005,7 @@ class basic_json
std::swap(m_type, other.m_type);
std::swap(m_value, other.m_value);
set_parent(*this, true);
set_parents();
assert_invariant();
}