TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ emplace()

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... Args>
std::pair< iterator, bool > nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::emplace ( Args &&...  args)
inline

add an object to an object if key does not exist

Inserts a new element into a JSON object constructed in-place with the given args if there is no element with the key in the container. If the function is called on a JSON null value, an empty object is created before appending the value created from args.

Parameters
[in]argsarguments to forward to a constructor of basic_json
Template Parameters
Argscompatible types to create a basic_json object
Returns
a pair consisting of an iterator to the inserted element, or the already-existing element if no insertion happened, and a bool denoting whether the insertion took place.
Exceptions
type_error.311when called on a type other than JSON object or null; example: "cannot use emplace() with number"

@complexity Logarithmic in the size of the container, O(log(size())).

@liveexample{The example shows how emplace() can be used to add elements to a JSON object. Note how the null value was silently converted to a JSON object. Further note how no value is added if there was already one value stored with the same key.,emplace}

Since
version 2.0.8

Definition at line 23392 of file json.hpp.

23393 {
23394 // emplace only works for null objects or arrays
23395 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
23396 {
23397 JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), *this));
23398 }
23399
23400 // transform null object into an object
23401 if (is_null())
23402 {
23403 m_type = value_t::object;
23406 }
23407
23408 // add element to array (perfect forwarding)
23409 auto res = m_value.object->emplace(std::forward<Args>(args)...);
23410 set_parent(res.first->second);
23411 // create result iterator and set iterator to the result of emplace
23412 auto it = begin();
23413 it.m_it.object_iterator = res.first;
23414 // return pair of iterator and boolean
23415 return {it, res.second};
23416 }
iterator begin() noexcept
returns an iterator to the first element
Definition: json.hpp:22377
void assert_invariant(bool check_parents=true) const noexcept
checks the class invariants
Definition: json.hpp:18935
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:20410
json_value m_value
the value of the current element
Definition: json.hpp:24928
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:24883
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:20251
@ object
object (unordered set of name/value pairs)