Skip to content

Commit

Permalink
Doc update
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Oct 24, 2024
1 parent dda6b12 commit 196b912
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions doc/ref/corelib/json/allocators.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,63 @@ or a <a href=https://en.cppreference.com/w/cpp/memory/scoped_allocator_adaptor>s
Non-propagating stateful allocators, such as the [Boost.Interprocess allocators](https://www.boost.org/doc/libs/1_82_0/doc/html/interprocess/allocators_containers.html#interprocess.allocators_containers.allocator_introduction),
must be wrapped by a [std::scoped_allocator_adaptor](https://en.cppreference.com/w/cpp/memory/scoped_allocator_adaptor).

Every constructor has a version that accepts an allocator argument.
Every constructor has a version that accepts an allocator argument.
The allocator is used to allocate memory for a long string, byte string, array, or object,
and it is retained in the long string, byte string, array, or object itself.
and it is retained in the long string, byte string, array, or object itself.
For other data members the allocator argument is ignored.

#### Move construction

`basic_json` move construction

```
Json j1(std::move(j));
```

copies the pointer or scalar value in `j` to `j1`, and changes the data member in `j` to `null`. For example:

```
char buffer[1024] = {};
std::pmr::monotonic_buffer_resource pool{ std::data(buffer), std::size(buffer) };
std::pmr::polymorphic_allocator<char> alloc(&pool);
jsoncons::pmr::json j{ "String too long for short string", alloc};
assert(j.is_string());
assert(alloc == j.get_allocator());
jsoncons::pmr::json j1(std::move(j));
assert(j1.is_string());
assert(alloc == j1.get_allocator());
assert(j.is_null());
```

#### Move assignment

If `j` and `j1` both hold pointers, `basic_json` move assignment

```
j1 = std::move(j);
```

swaps the two pointers. If `j` holds a pointer and `j1` holds a non-pointer value,
move assignment copies `j`'s pointer to `j1` and changes `j`'s value to null.
If `j` holds a non-pointer and `j1` holds a pointer value,
move assignment copies `j`'s value to `j1` and leaves `j` alone. For example:

```cpp
char buffer[1024];
std::pmr::monotonic_buffer_resource pool{ std::data(buffer), std::size(buffer) };
std::pmr::polymorphic_allocator<char> alloc(&pool);

jsoncons::pmr::json j{ "String too long for short string", alloc };
assert(j.is_string());
assert(alloc == j.get_allocator());

jsoncons::pmr::json j1{10};
assert(j1.is_number());

j1 = std::move(j);
assert(j1.is_string());
assert(alloc == j1.get_allocator());
assert(j.is_null());
```

0 comments on commit 196b912

Please sign in to comment.