nlib
nn::nlib::msgpack::MpObjectAsMap Class Referencefinal

The class for providing access with a pair of the key and the value as an element for MpObject that has an associative array type. More...

#include "nn/nlib/msgpack/MpObject.h"

Public Types

typedef Nlist< MpObjectKv >::iterator iterator
 A forward iterator.
 
typedef Nlist< MpObjectKv >::const_iterator const_iterator
 Read-only forward iterator.
 
typedef MpObjectKvreference
 A reference to an element.
 
typedef const MpObjectKvconst_reference
 Read-only reference to an element.
 
typedef MpObjectKvpointer
 Pointer to the element.
 
typedef const MpObjectKvconst_pointer
 Read-only pointer to an element.
 

Public Member Functions

 MpObjectAsMap (MpObject &obj) noexcept
 
iterator begin () noexcept
 Gets the iterator pointing to the first element.
 
iterator end () noexcept
 Gets the iterator pointing beyond the last element.
 
const_iterator begin () const noexcept
 Gets the read-only iterator pointing to the first element.
 
const_iterator end () const noexcept
 Gets the read-only iterator pointing beyond the last element.
 
reference operator[] (size_t n) noexcept
 Gets the nth element. n must be less than the number of stored elements. More...
 
const_reference operator[] (size_t n) const noexcept
 The const decoration version of the above function.
 
iterator GetByKey (const MpObject &key) noexcept
 Gets the iterator pointing to a pair of the key and the value that has key as the key. More...
 
const_iterator GetByKey (const MpObject &key) const noexcept
 The const decoration version of the above function.
 
template<class StringIterator >
iterator GetByKey (StringIterator first, StringIterator last) noexcept
 Gets the iterator pointing to a pair of the key and the value that has the specified key. More...
 
template<class StringIterator >
const_iterator GetByKey (StringIterator first, StringIterator last) const noexcept
 The const decoration version of the above function.
 
iterator GetByKey (const nlib_utf8_t *key) noexcept
 Gets the iterator pointing to a pair of the key and the value that has the specified key. More...
 
const_iterator GetByKey (const nlib_utf8_t *key) const noexcept
 The const decoration version of the above function.
 
uint32_t GetSize () const noexcept
 Returns the number of stored elements.
 
errno_t Resize (size_t n) noexcept
 Changes the number of elements for an associative array. More...
 
errno_t PushBack (MpObjectKv &&rhs) noexcept
 Adds a pair of the key and the value to the end of an associative array. The content in rhs is moved. More...
 
errno_t PushBack (const nlib_utf8_t *key, MpObject &&value) noexcept
 Adds a pair of the key and the value to the end of an associative array. The content in value is moved. More...
 
errno_t PopBack () noexcept
 Removes the end element. More...
 
errno_t Erase (iterator it) noexcept
 Removes a pair of the key and the value specified by it. More...
 
errno_t Erase (size_t n) noexcept
 Removes the pair of the nth key in an associative array and the value. More...
 
errno_t Erase (const nlib_utf8_t *key) noexcept
 Removes one pair of the key and the value that has key as the key from an associative array. More...
 

Detailed Description

The class for providing access with a pair of the key and the value as an element for MpObject that has an associative array type.

Description
The begin() and end() functions are defined for associative arrays and you can write a loop as follows.
const char* json = R"({"key1" : 1, "key2" : 2, "key3" : 3})";
auto obj = ToMpObject(json);
for (auto& item : obj->AsMap()) {
int val;
item.second.Unbox(&val);
nlib_printf("%s : %d\n", item.first.GetString(), val);
}
/*
Output:
key1 : 1
key2 : 2
key3 : 3
*/

Definition at line 534 of file MpObject.h.

Constructor & Destructor Documentation

◆ MpObjectAsMap()

nn::nlib::msgpack::MpObjectAsMap::MpObjectAsMap ( MpObject obj)
inlineexplicitnoexcept
Parameters
[in]objMpObject to be accessed as an associative array. obj.IsMap() must be true.

Definition at line 546 of file MpObject.h.

Member Function Documentation

◆ Erase() [1/3]

nn::nlib::msgpack::MpObjectAsMap::Erase ( iterator  it)
noexcept

Removes a pair of the key and the value specified by it.

Parameters
[in]itThe iterator pointing to a pair of the key and the value.
Return values
0Success.
ERANGEIndicates that it is out of the range.

◆ Erase() [2/3]

nn::nlib::msgpack::MpObjectAsMap::Erase ( size_t  n)
inlinenoexcept

Removes the pair of the nth key in an associative array and the value.

Parameters
[in]nAn integer equal to or greater than 0.
Return values
0Success.
ERANGEIndicates that n is out of the range.

Definition at line 601 of file MpObject.h.

◆ Erase() [3/3]

nn::nlib::msgpack::MpObjectAsMap::Erase ( const nlib_utf8_t key)
noexcept

Removes one pair of the key and the value that has key as the key from an associative array.

Parameters
[in]keyA key string.
Return values
0Success.
ENOENTIndicates that a pair of the corresponding key and the value does not exist.

◆ GetByKey() [1/3]

nn::nlib::msgpack::MpObjectAsMap::GetByKey ( const MpObject key)
noexcept

Gets the iterator pointing to a pair of the key and the value that has key as the key.

Parameters
[in]keyThe search-target key.
Returns
The iterator pointing to a pair of the key and the value.
Description
Returns end() if the lement does not exists. In addition, MpObject that is the key of the return value is not the same as key, but is the one stored in an associative array.

◆ GetByKey() [2/3]

template<class StringIterator >
MpObjectAsMap::iterator nn::nlib::msgpack::MpObjectAsMap::GetByKey ( StringIterator  first,
StringIterator  last 
)
noexcept

Gets the iterator pointing to a pair of the key and the value that has the specified key.

Template Parameters
StringIteratorThe iterator for strings such as std::string::iterator.
Parameters
[in]firstThe first character of the key string.
[in]lastThe last character of the key string.
Returns
The iterator pointing to a pair of the key and the value.
Description
Returns end() if the lement does not exists. If there are multiple elements with the same key, the closest element to the first character is returned.

Definition at line 802 of file MpObject.h.

◆ GetByKey() [3/3]

nn::nlib::msgpack::MpObjectAsMap::GetByKey ( const nlib_utf8_t key)
noexcept

Gets the iterator pointing to a pair of the key and the value that has the specified key.

Parameters
[in]keyA key string.
Returns
The iterator pointing to a pair of the key and the value.
Description
Returns end() if the lement does not exists. If there are multiple elements with the same key, the closest element to the first character is returned.

◆ operator[]()

nn::nlib::msgpack::MpObjectAsMap::operator[] ( size_t  n)
inlinenoexcept

Gets the nth element. n must be less than the number of stored elements.

Parameters
[in]nThe position of an element.
Returns
Reference to the nth element.

Definition at line 551 of file MpObject.h.

◆ PopBack()

nn::nlib::msgpack::MpObjectAsMap::PopBack ( )
inlinenoexcept

Removes the end element.

Returns
Returns 0 on success. Any value other than 0 indicates an error.

Definition at line 592 of file MpObject.h.

◆ PushBack() [1/2]

nn::nlib::msgpack::MpObjectAsMap::PushBack ( MpObjectKv &&  rhs)
inlinenoexcept

Adds a pair of the key and the value to the end of an associative array. The content in rhs is moved.

Parameters
[in,out]rhsA pair of the key and the value to add to the end of an associative array.
Returns
Returns 0 on success. Any value other than 0 indicates an error.

Definition at line 578 of file MpObject.h.

◆ PushBack() [2/2]

nn::nlib::msgpack::MpObjectAsMap::PushBack ( const nlib_utf8_t key,
MpObject &&  value 
)
inlinenoexcept

Adds a pair of the key and the value to the end of an associative array. The content in value is moved.

Parameters
[in]keyA key.
[in,out]valueThe value corresponding to a key.
Returns
Returns 0 on success. Any value other than 0 indicates an error.

Definition at line 584 of file MpObject.h.

◆ Resize()

nn::nlib::msgpack::MpObjectAsMap::Resize ( size_t  n)
inlinenoexcept

Changes the number of elements for an associative array.

Parameters
[in]nSize after resizing.
Returns
Returns 0 on success. Any value other than 0 indicates an error.

Definition at line 569 of file MpObject.h.


The documentation for this class was generated from the following files: