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 29, 2024
1 parent 38f181d commit eeee29b
Showing 1 changed file with 13 additions and 34 deletions.
47 changes: 13 additions & 34 deletions include/jsoncons/basic_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,38 +788,11 @@ namespace jsoncons {
semantic_tag tag_;
pointer ptr_;

template <typename... Args>
void create(allocator_type alloc, Args&& ... args)
{
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;
}
}

object_storage(pointer ptr, semantic_tag tag)
: storage_kind_(static_cast<uint8_t>(json_storage_kind::object)), short_str_length_(0), tag_(tag), ptr_(ptr)
{
}

object_storage(const object& val, semantic_tag tag)
: storage_kind_(static_cast<uint8_t>(json_storage_kind::object)), short_str_length_(0), tag_(tag), ptr_(nullptr)
{
create(val.get_allocator(), val);
}

object_storage(object&& val, semantic_tag tag)
: storage_kind_(static_cast<uint8_t>(json_storage_kind::object)), short_str_length_(0), tag_(tag), ptr_(nullptr)
{
create(val.get_allocator(), std::move(val));
}

explicit object_storage(const object_storage& other)
: storage_kind_(other.storage_kind_), short_str_length_(0), tag_(other.tag_), ptr_(other.ptr_)
{
Expand Down Expand Up @@ -2783,12 +2756,14 @@ namespace jsoncons {

explicit basic_json(const Allocator& alloc)
{
construct<object_storage>(object(alloc), semantic_tag::none);
auto ptr = create_object(alloc);
construct<object_storage>(ptr, semantic_tag::none);
}

basic_json(semantic_tag tag, const Allocator& alloc)
{
construct<object_storage>(object(alloc), tag);
auto ptr = create_object(alloc);
construct<object_storage>(ptr, tag);
}

basic_json(semantic_tag tag)
Expand Down Expand Up @@ -2821,12 +2796,12 @@ namespace jsoncons {
semantic_tag tag,
const Allocator& alloc = Allocator())
{
construct<object_storage>(object(alloc), tag);
auto ptr = create_object(alloc);
construct<object_storage>(ptr, tag);
}

explicit basic_json(json_object_arg_t, const Allocator& alloc = Allocator())
{
//construct<object_storage>(object(alloc), semantic_tag::none);
auto ptr = create_object(alloc);
construct<object_storage>(ptr, semantic_tag::none);
}
Expand Down Expand Up @@ -2905,18 +2880,22 @@ namespace jsoncons {

basic_json(array&& val, semantic_tag tag = semantic_tag::none)
{
auto ptr = create_array(val.get_allocator(), std::move(val));
auto alloc = val.get_allocator();
auto ptr = create_array(alloc, std::move(val));
construct<array_storage>(ptr, tag);
}

basic_json(const object& val, semantic_tag tag = semantic_tag::none)
{
construct<object_storage>(val, tag);
auto ptr = create_object(val.get_allocator(), val);
construct<object_storage>(ptr, tag);
}

basic_json(object&& val, semantic_tag tag = semantic_tag::none)
{
construct<object_storage>(std::move(val), tag);
auto alloc = val.get_allocator();
auto ptr = create_object(alloc);
construct<object_storage>(ptr, tag);
}

template <typename T,
Expand Down

0 comments on commit eeee29b

Please sign in to comment.