1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-29 23:01:16 +03:00

♻️ simplify destroy() function for primitive types

This commit is contained in:
Niels Lohmann
2021-07-15 12:46:48 +02:00
parent db98073930
commit 149ded856f
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

@ -18167,6 +18167,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;
@ -18177,7 +18179,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)
@ -18196,8 +18198,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();
}
@ -18214,6 +18215,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)
{