| 19730 | json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {} |
| 19731 | |
| 19732 | void destroy(value_t t) |
| 19733 | { |
| 19734 | if (t == value_t::array || t == value_t::object) |
| 19735 | { |
| 19736 | // flatten the current json_value to a heap-allocated stack |
| 19737 | std::vector<basic_json> stack; |
| 19738 | |
| 19739 | // move the top-level items to stack |
| 19740 | if (t == value_t::array) |
| 19741 | { |
| 19742 | stack.reserve(array->size()); |
| 19743 | std::move(array->begin(), array->end(), std::back_inserter(stack)); |
| 19744 | } |
| 19745 | else |
| 19746 | { |
| 19747 | stack.reserve(object->size()); |
| 19748 | for (auto&& it : *object) |
| 19749 | { |
| 19750 | stack.push_back(std::move(it.second)); |
| 19751 | } |
| 19752 | } |
| 19753 | |
| 19754 | while (!stack.empty()) |
| 19755 | { |
| 19756 | // move the last item to local variable to be processed |
| 19757 | basic_json current_item(std::move(stack.back())); |
| 19758 | stack.pop_back(); |
| 19759 | |
| 19760 | // if current_item is array/object, move |
| 19761 | // its children to the stack to be processed later |
| 19762 | if (current_item.is_array()) |
| 19763 | { |
| 19764 | std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(), std::back_inserter(stack)); |
| 19765 | |
| 19766 | current_item.m_value.array->clear(); |
| 19767 | } |
| 19768 | else if (current_item.is_object()) |
| 19769 | { |
| 19770 | for (auto&& it : *current_item.m_value.object) |
| 19771 | { |
| 19772 | stack.push_back(std::move(it.second)); |
| 19773 | } |
| 19774 | |
| 19775 | current_item.m_value.object->clear(); |
| 19776 | } |
| 19777 | |
| 19778 | // it's now safe that current_item get destructed |
| 19779 | // since it doesn't have any children |
| 19780 | } |
| 19781 | } |
| 19782 | |
| 19783 | switch (t) |
| 19784 | { |
| 19785 | case value_t::object: |
| 19786 | { |
| 19787 | AllocatorType<object_t> alloc; |
| 19788 | std::allocator_traits<decltype(alloc)>::destroy(alloc, object); |
| 19789 | std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1); |