TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ erase() [4/4]

template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer, class BinaryType = std::vector<std::uint8_t>>
template<class IteratorType , typename std::enable_if< std::is_same< IteratorType, typename basic_json_t::iterator >::value||std::is_same< IteratorType, typename basic_json_t::const_iterator >::value, int >::type = 0>
IteratorType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::erase ( IteratorType  pos)
inline

remove element given an iterator

Removes the element specified by iterator pos. The iterator pos must be valid and dereferenceable. Thus the end() iterator (which is valid, but is not dereferenceable) cannot be used as a value for pos.

If called on a primitive type other than null, the resulting JSON value will be null.

Parameters
[in]positerator to the element to remove
Returns
Iterator following the last removed element. If the iterator pos refers to the last element, the end() iterator is returned.
Template Parameters
IteratorTypean iterator or const_iterator
Postcondition
Invalidates iterators and references at or after the point of the erase, including the end() iterator.
Exceptions
type_error.307if called on a null value; example: "cannot use erase() with null"
invalid_iterator.202if called on an iterator which does not belong to the current JSON value; example: "iterator does not fit current value"
invalid_iterator.205if called on a primitive type with invalid iterator (i.e., any iterator which is not begin()); example: "iterator out of range"

@complexity The complexity depends on the type:

  • objects: amortized constant
  • arrays: linear in distance between pos and the end of the container
  • strings and binary: linear in the length of the member
  • other types: constant

@liveexample{The example shows the result of erase() for different JSON types.,erase__IteratorType}

See also
see erase(IteratorType, IteratorType) – removes the elements in the given range
see erase(const typename object_t::key_type&) – removes the element from an object at the given key
see erase(const size_type) – removes the element from an array at the given index
Since
version 1.0.0

Definition at line 21920 of file json.hpp.

21921 {
21922 // make sure iterator fits the current value
21923 if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
21924 {
21925 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this));
21926 }
21927
21928 IteratorType result = end();
21929
21930 switch (m_type)
21931 {
21932 case value_t::boolean:
21936 case value_t::string:
21937 case value_t::binary:
21938 {
21939 if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
21940 {
21941 JSON_THROW(invalid_iterator::create(205, "iterator out of range", *this));
21942 }
21943
21944 if (is_string())
21945 {
21946 AllocatorType<string_t> alloc;
21947 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
21948 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
21949 m_value.string = nullptr;
21950 }
21951
21952 else if (is_binary())
21953 {
21954 AllocatorType<binary_t> alloc;
21955 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
21956 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
21957 m_value.binary = nullptr;
21958 }
21959
21960 m_type = value_t::null;
21962 break;
21963 }
21964
21965 case value_t::object:
21966 {
21967 result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator);
21968 break;
21969 }
21970
21971 case value_t::array:
21972 {
21973 result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator);
21974 break;
21975 }
21976
21977 case value_t::null:
21978 case value_t::discarded:
21979 default:
21980 JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this));
21981 }
21982
21983 return result;
21984 }
void assert_invariant(bool check_parents=true) const noexcept
checks the class invariants
Definition: json.hpp:18935
json_value m_value
the value of the current element
Definition: json.hpp:24928
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:22448
constexpr bool is_binary() const noexcept
return whether value is a binary array
Definition: json.hpp:20476
constexpr bool is_string() const noexcept
return whether value is a string
Definition: json.hpp:20454
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:24883
@ number_integer
number value (signed integer)
@ discarded
discarded by the parser callback function
@ binary
binary array (ordered collection of bytes)
@ object
object (unordered set of name/value pairs)
@ number_float
number value (floating-point)
@ number_unsigned
number value (unsigned integer)
@ array
array (ordered collection of values)