Skip to content

Commit

Permalink
Improve basic_json storage
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Oct 28, 2024
1 parent 1ba1412 commit ca74bc8
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions include/jsoncons/basic_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,23 @@ namespace jsoncons {
semantic_tag tag_;
pointer ptr_;

template <typename... Args>
static pointer create_ptr(allocator_type alloc, Args&& ... args)
{
auto ptr = std::allocator_traits<allocator_type>::allocate(alloc, 1);
JSONCONS_TRY
{
std::allocator_traits<allocator_type>::construct(alloc, extension_traits::to_plain_pointer(ptr),
std::forward<Args>(args)...);
}
JSONCONS_CATCH(...)
{
std::allocator_traits<allocator_type>::deallocate(alloc, ptr,1);
JSONCONS_RETHROW;
}
return ptr;
}

template <typename... Args>
void create(allocator_type alloc, Args&& ... args)
{
Expand Down Expand Up @@ -2068,12 +2085,17 @@ namespace jsoncons {
case json_storage_kind::byte_str:
construct<byte_string_storage>(other.cast<byte_string_storage>());
break;
case json_storage_kind::array:
{
auto ptr = array_storage::create_ptr(
std::allocator_traits<Allocator>::select_on_container_copy_construction(other.cast<array_storage>().get_allocator()),
*(other.cast<array_storage>().ptr_));
construct<array_storage>(ptr, other.tag());
break;
}
case json_storage_kind::object:
construct<object_storage>(other.cast<object_storage>());
break;
case json_storage_kind::array:
construct<array_storage>(other.cast<array_storage>());
break;
default:
JSONCONS_UNREACHABLE();
break;
Expand All @@ -2098,8 +2120,11 @@ namespace jsoncons {
construct<byte_string_storage>(other.cast<byte_string_storage>(),alloc);
break;
case json_storage_kind::array:
construct<array_storage>(other.cast<array_storage>(),alloc);
break;
{
auto ptr = array_storage::create_ptr(alloc, *(other.cast<array_storage>().ptr_));
construct<array_storage>(ptr, other.tag());
break;
}
case json_storage_kind::object:
construct<object_storage>(other.cast<object_storage>(),alloc);
break;
Expand Down

0 comments on commit ca74bc8

Please sign in to comment.