♻️ simplify destroy() function for primitive types

This commit is contained in:
Niels Lohmann 2021-07-15 12:46:48 +02:00 committed by Chaoya Li
parent 97eb6414e4
commit e77ae77d7e
2 changed files with 76 additions and 72 deletions

View File

@ -1132,6 +1132,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
}
void destroy(value_t t) noexcept
{
if (t == value_t::array || t == value_t::object)
{
// flatten the current json_value to a heap-allocated stack
std::vector<basic_json> stack;
@ -1142,7 +1144,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
stack.reserve(array->size());
std::move(array->begin(), array->end(), std::back_inserter(stack));
}
else if (t == value_t::object)
else
{
stack.reserve(object->size());
for (auto&& it : *object)
@ -1161,8 +1163,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// its children to the stack to be processed later
if (current_item.is_array())
{
std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(),
std::back_inserter(stack));
std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(), std::back_inserter(stack));
current_item.m_value.array->clear();
}
@ -1179,6 +1180,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// it's now safe that current_item get destructed
// since it doesn't have any children
}
}
switch (t)
{

View File

@ -18176,6 +18176,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
}
void destroy(value_t t) noexcept
{
if (t == value_t::array || t == value_t::object)
{
// flatten the current json_value to a heap-allocated stack
std::vector<basic_json> stack;
@ -18186,7 +18188,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
stack.reserve(array->size());
std::move(array->begin(), array->end(), std::back_inserter(stack));
}
else if (t == value_t::object)
else
{
stack.reserve(object->size());
for (auto&& it : *object)
@ -18205,8 +18207,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// its children to the stack to be processed later
if (current_item.is_array())
{
std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(),
std::back_inserter(stack));
std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(), std::back_inserter(stack));
current_item.m_value.array->clear();
}
@ -18223,6 +18224,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// it's now safe that current_item get destructed
// since it doesn't have any children
}
}
switch (t)
{