mirror of
https://github.com/nlohmann/json.git
synced 2026-11-02 19:05:35 +00:00
insert(): strong guarantee for single elements, basic for the rest
Rework of the previous two commits, following the discussion in #5996: the standard containers give the strong guarantee only for a single element (std::vector: at the end, or if the exception does not come from copying T; std::map: single-element insert), and copying a basic_json allocates. Rebuilding a rollback for the count, range and object-range overloads on top of array_t/object_t made the code verbose and slower. - insert(pos, const basic_json&): at end() forward as before (the standard covers it); elsewhere copy first and insert by move, as libstdc++ does internally (libc++ shifts the tail before copying). - insert_iterator() and object insert(first, last) set the parents in a scope guard, so a failed insertion leaves a valid value with JSON_DIAGNOSTICS too (the next assert_invariant() aborted before). - Docs: strong guarantee for (1), basic guarantee for (2)-(5); the argument checks throw before anything is changed. - Test: allocation-failure sweep checks the strong guarantee for (1) and validity, type and parents for the other overloads. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
4 files changed
+157
-648
No files matched your search
@@ -65,9 +65,17 @@ the same index, which is now a different value.
|
||||
|
||||
## Exception safety
|
||||
|
||||
Strong exception safety: if an exception occurs, the original value stays intact. This includes an exception thrown
|
||||
while the new elements are copied (for example, `std::bad_alloc`): elements and members that were already added
|
||||
are removed again.
|
||||
1. Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
|
||||
2. Basic guarantee: if an exception is thrown while the elements are copied (for example, `std::bad_alloc`), the JSON
|
||||
value may be partially modified, but it stays a valid array.
|
||||
3. Basic guarantee: if an exception is thrown while the elements are copied (for example, `std::bad_alloc`), the JSON
|
||||
value may be partially modified, but it stays a valid array.
|
||||
4. Basic guarantee: if an exception is thrown while the elements are copied (for example, `std::bad_alloc`), the JSON
|
||||
value may be partially modified, but it stays a valid array.
|
||||
5. Basic guarantee: if an exception is thrown while the members are copied (for example, `std::bad_alloc`), the JSON
|
||||
value may be partially modified, but it stays a valid object.
|
||||
|
||||
The exceptions listed below are thrown before the JSON value is changed.
|
||||
|
||||
## Exceptions
|
||||
|
||||
|
||||
Reference in new issue
Block a user