nlib
nn::nlib::msgpack::MpWalker Class Referencefinal

Quickly accesses MessagePack expanded in memory. More...

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

Public Member Functions

const nlib_byte_tGetPtr () const noexcept
 Gets a pointer to the current location of MessagePack data. More...
 
size_t GetSize () const noexcept
 Gets the size of the MessagePack data. More...
 
MpWalker Skip () const noexcept
 Returns the MpWalker object pointing to the data following the next data by skipping it. In the case of an array or an associative array, the all of it is skipped.
 
 operator bool () const
 Returns true if the object is initialized and readable.
 
Constructor, Destructor, and Initialization
constexpr MpWalker () noexcept
 Instantiates the object with default parameters (default constructor).
 
constexpr MpWalker (const void *p, size_t n) noexcept
 The constructor to initialize. More...
 
bool Init (const void *p, size_t n) noexcept
 Initialize with this function when using the default constructor. More...
 
Array
MpWalker operator[] (size_t array_idx) const noexcept
 Specifies an index and accesses the element in the array.
 
MpWalker operator[] (int array_idx) const noexcept
 Specifies an index and accesses the element in the array.
 
MpWalker At (size_t idx) const noexcept
 Specifies an index and accesses the element in the array. More...
 
MpWalker GetArrayCount (uint32_t *n) const noexcept
 If an array is stored, gets the array size. More...
 
std::pair< MpWalker, uint32_t > GetArrayCount () const noexcept
 If an array is stored, gets the array size. More...
 
Associative Array
MpWalker operator[] (const nlib_utf8_t *key) const noexcept
 Specifies a string and accesses the element in the associative array.
 
MpWalker At (size_t idx, const nlib_utf8_t **key, size_t *n) const noexcept
 Specifies an index and accesses the element in the associative array. More...
 
MpWalker Find (const nlib_utf8_t *key) const noexcept
 Specifies a string and accesses the element in the associative array. More...
 
MpWalker GetMapCount (uint32_t *n) const noexcept
 If an associative array is stored, gets the associative array size. More...
 
std::pair< MpWalker, uint32_t > GetMapCount () const noexcept
 If an associative array is stored, gets the associative array size. More...
 
String
MpWalker GetString (const nlib_utf8_t **str, uint32_t *n) const noexcept
 Gets the str format data. More...
 
std::tuple< MpWalker, const nlib_utf8_t *, uint32_t > GetString () const noexcept
 If a string is stored, gets the string (str format). More...
 
Binary
MpWalker GetBinary (const void **bin, uint32_t *n) const noexcept
 Gets the bin format data. More...
 
std::tuple< MpWalker, const nlib_byte_t *, uint32_t > GetBinary () const noexcept
 If a string is stored, gets the binary (bin format) data. More...
 
Extended Data
MpWalker GetExt (int8_t *tp, const void **bin, uint32_t *n) const noexcept
 Gets the ext format data. More...
 
MpWalker GetTimestamp (nlib_time *val) const noexcept
 Obtains the timestamp. More...
 
std::tuple< MpWalker, int8_t, const nlib_byte_t *, uint32_t > GetExt () const noexcept
 If the extended data type is stored, gets extended data type (ext format) data. More...
 
std::pair< MpWalker, nlib_timeGetTimestamp () const noexcept
 If the timestamp extended type is stored, gets data as the nlib_time type value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed. More...
 
Null and Boolean Values
MpWalker GetNil () const noexcept
 Gets nil. More...
 
MpWalker GetBoolean (bool *val) const noexcept
 If a boolean value is stored, gets the boolean value. More...
 
std::pair< MpWalker, bool > GetBoolean () const noexcept
 If a boolean value is stored, gets the boolean value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed. More...
 
Numerical Value
MpWalker GetInt (int32_t *val) const noexcept
 Gets a signed integer value. More...
 
MpWalker GetInt (int64_t *val) const noexcept
 Gets a signed integer value. More...
 
MpWalker GetUint (uint32_t *val) const noexcept
 Gets an unsigned integer value. More...
 
MpWalker GetUint (uint64_t *val) const noexcept
 Gets an unsigned integer value. More...
 
MpWalker GetFloat (float *val) const noexcept
 Gets a single-precision floating-point number. More...
 
MpWalker GetDouble (double *val) const noexcept
 Gets a double-precision floating-point number. More...
 
std::pair< MpWalker, int32_t > GetInt32 () const noexcept
 If an integer that can be stored in the int32_t type is stored, gets the integer value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed. More...
 
std::pair< MpWalker, int64_t > GetInt64 () const noexcept
 If an integer that can be stored in the int64_t type is stored, gets the integer value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed. More...
 
std::pair< MpWalker, uint32_t > GetUint32 () const noexcept
 If an integer that can be stored in the uint32_t type is stored, gets the integer value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed. More...
 
std::pair< MpWalker, uint64_t > GetUint64 () const noexcept
 If an integer that can be stored in the uint64_t type is stored, gets the integer value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed. More...
 
std::pair< MpWalker, float > GetFloat () const noexcept
 If a numerical value that can be stored in the float type is stored, gets the numerical value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed. More...
 
std::pair< MpWalker, double > GetDouble () const noexcept
 If a numerical value that can be stored in the double type is stored, gets the numerical value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed. More...
 

Detailed Description

Quickly accesses MessagePack expanded in memory.

Description
Dynamically constructs MpObject when using JsonStreamParser to read MessagePack data. Though this behavior is useful, an overhead caused by, for example, MpObject built even for unnecessary data may pose an issue.
Use MessagePack data more efficiently by building the MpObject for only the necessary data. To do this, use MpWalker to specify the key of an associative array or an index of an array, move to the required location, and then use JsonStreamParser.
The following sample code describes how to create MpObject using only information under the key attribute by skipping.
const nlib_utf8_t* json = R"({ "a": { "key": "bad" }, "b" : [1, "key", 3], "key": ["That", "is", "correct"] })";
size_t n = ToMsgpack(buf, json);
MpWalker root(&buf[0], n);
MpWalker x = root["key"]; // moving to right after the toplevel key 'key'
SUCCEED_IF(x);
SUCCEED_IF(r.first == 0);
ToJson(parsed_value, *r.second.get());
nlib_printf("parsed value: %s\n", parsed_value);
/*
Output:
parsed value: ["That","is","correct"]
*/

Definition at line 36 of file MpWalker.h.

Constructor & Destructor Documentation

◆ MpWalker()

nn::nlib::msgpack::MpWalker::MpWalker ( const void *  p,
size_t  n 
)
inlinenoexcept

The constructor to initialize.

Parameters
[in]pPointer to MessagePack data.
[in]nData size.

Definition at line 39 of file MpWalker.h.

Member Function Documentation

◆ At() [1/2]

nn::nlib::msgpack::MpWalker::At ( size_t  idx) const
noexcept

Specifies an index and accesses the element in the array.

Parameters
[in]idxThe array index.
Returns
If successful, returns the MpWalker object pointing to the value of an array element.
Description
Returns the MpWalker indicating an array element. If not an array, or the specified index does not exist, returns an MpWalker object constructed by the default constructor.
The results can be determined by evaluating objects as shown in the code below.
const nlib_utf8_t* json = R"(["a", "b", "c", "d", "e"])";
size_t n = ToMsgpack(buf, json);
MpWalker root(&buf[0], n);
nlib_printf("root[3]: %s\n", root[3] ? "exists" : "not exists");
nlib_printf("root[5]: %s\n", root[5] ? "exists" : "not exists");

◆ At() [2/2]

nn::nlib::msgpack::MpWalker::At ( size_t  idx,
const nlib_utf8_t **  key,
size_t *  n 
) const
noexcept

Specifies an index and accesses the element in the associative array.

Parameters
[in]idxIndex of the associative array index.
[out]keyCorresponding key of the associative array (Not null terminated).
[out]nLength of the key of the corresponding associative array.
Returns
If successful, returns the MpWalker object pointing to the value of an associative array.

◆ Find()

nn::nlib::msgpack::MpWalker::Find ( const nlib_utf8_t key) const
noexcept

Specifies a string and accesses the element in the associative array.

Parameters
[in]keyAssociative array key.
Returns
If successful, returns the MpWalker object pointing to the value of an associative array.
Description
Returns the MpWalker indicating an associative array element. If not an associative array, or the specified index does not exist, returns an MpWalker object constructed by the default constructor.
The results can be determined by evaluating objects as shown in the code below.
const nlib_utf8_t* json = R"({"key1": 1, "key2" : 2, "key3" : 3})";
size_t n = ToMsgpack(buf, json);
MpWalker root(&buf[0], n);
nlib_printf("root[key2]: %s\n", root["key2"] ? "exists" : "not exists");
nlib_printf("root[key4]: %s\n", root["key4"] ? "exists" : "not exists");

◆ GetArrayCount() [1/2]

nn::nlib::msgpack::MpWalker::GetArrayCount ( uint32_t *  n) const
noexcept

If an array is stored, gets the array size.

Parameters
[out]nSize of the array.
Returns
If successful, returns the MpWalker object pointing to the next element (the first element in the array).

◆ GetArrayCount() [2/2]

nn::nlib::msgpack::MpWalker::GetArrayCount ( ) const
inlinenoexcept

If an array is stored, gets the array size.

Returns
A pair of the MpWalker object and the array size.
Description
The return value, the MpWalker object, points to the next element (the first element in an array) if successful, or is an invalid value if failed.
The following sample code gets the array size, and then array elements one by one.
const nlib_utf8_t* json = R"([[1,2,3], "a", { "b" : 1 }, false, null])";
size_t n = ToMsgpack(buf, json);
MpWalker root(&buf[0], n);
auto ac_result = root.GetArrayCount();
SUCCEED_IF(ac_result.first);
nlib_printf("Array Count: %u\n", ac_result.second);
for (uint32_t i = 0; i < ac_result.second; ++i) {
auto r = JsonStreamParser::Parse(ac_result.first);
SUCCEED_IF(r.first == 0);
ToJson(parsed_value, *r.second.get());
nlib_printf("root[%u]: %s\n", i, parsed_value);
ac_result.first = ac_result.first.Skip();
SUCCEED_IF(ac_result.first);
}
/*
Output:
Array Count: 5
root[0]: [1,2,3]
root[1]: "a"
root[2]: {"b":1}
root[3]: false
root[4]: null
*/

Definition at line 106 of file MpWalker.h.

◆ GetBinary() [1/2]

nn::nlib::msgpack::MpWalker::GetBinary ( const void **  bin,
uint32_t *  n 
) const
noexcept

Gets the bin format data.

Parameters
[out]binPointer to the binary.
[out]nLength of the binary.
Returns
If successful, returns the MpWalker object pointing to the next element.
See also
https://github.com/msgpack/msgpack/blob/master/spec.md#formats-bin

◆ GetBinary() [2/2]

nn::nlib::msgpack::MpWalker::GetBinary ( ) const
inlinenoexcept

If a string is stored, gets the binary (bin format) data.

Returns
A tupple of the MpWalker object, a pointer to binary, and the binary length.
See also
https://github.com/msgpack/msgpack/blob/master/spec.md#formats-bin

Definition at line 87 of file MpWalker.h.

◆ GetBoolean() [1/2]

nn::nlib::msgpack::MpWalker::GetBoolean ( bool *  val) const
noexcept

If a boolean value is stored, gets the boolean value.

Parameters
[out]valPointer to the boolean value.
Returns
If successful, returns the MpWalker object pointing to the next element.

◆ GetBoolean() [2/2]

nn::nlib::msgpack::MpWalker::GetBoolean ( ) const
inlinenoexcept

If a boolean value is stored, gets the boolean value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed.

Returns
A pair of the MpWalker object and a boolean value.

Definition at line 111 of file MpWalker.h.

◆ GetDouble() [1/2]

nn::nlib::msgpack::MpWalker::GetDouble ( double *  val) const
noexcept

Gets a double-precision floating-point number.

Parameters
[out]valPointer to the double-precision floating-point number.
Returns
If successful, returns the MpWalker object pointing to the next element.

◆ GetDouble() [2/2]

nn::nlib::msgpack::MpWalker::GetDouble ( ) const
inlinenoexcept

If a numerical value that can be stored in the double type is stored, gets the numerical value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed.

Returns
A pair of the MpWalker object and the double type value.

Definition at line 141 of file MpWalker.h.

◆ GetExt() [1/2]

nn::nlib::msgpack::MpWalker::GetExt ( int8_t *  tp,
const void **  bin,
uint32_t *  n 
) const
noexcept

Gets the ext format data.

Parameters
[out]tpNumeric value indicating the object type.
[out]binPointer to the binary.
[out]nLength of the binary.
Returns
If successful, returns the MpWalker object pointing to the next element.
See also
https://github.com/msgpack/msgpack/blob/master/spec.md#formats-ext

◆ GetExt() [2/2]

nn::nlib::msgpack::MpWalker::GetExt ( ) const
inlinenoexcept

If the extended data type is stored, gets extended data type (ext format) data.

Returns
A tupple of the MpWalker object, a numerical value indicating the object type, a pointer to the binary, and the binary length.
See also
https://github.com/msgpack/msgpack/blob/master/spec.md#formats-ext

Definition at line 93 of file MpWalker.h.

◆ GetFloat() [1/2]

nn::nlib::msgpack::MpWalker::GetFloat ( float *  val) const
noexcept

Gets a single-precision floating-point number.

Parameters
[out]valPointer to the single-precision floating-point number.
Returns
If successful, returns the MpWalker object pointing to the next element.

◆ GetFloat() [2/2]

nn::nlib::msgpack::MpWalker::GetFloat ( ) const
inlinenoexcept

If a numerical value that can be stored in the float type is stored, gets the numerical value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed.

Returns
A pair of the MpWalker object and the float type value.

Definition at line 136 of file MpWalker.h.

◆ GetInt() [1/2]

nn::nlib::msgpack::MpWalker::GetInt ( int32_t *  val) const
noexcept

Gets a signed integer value.

Parameters
[out]valPointer to the signed integer value.
Returns
If successful, returns the MpWalker object pointing to the next element.

◆ GetInt() [2/2]

nn::nlib::msgpack::MpWalker::GetInt ( int64_t *  val) const
noexcept

Gets a signed integer value.

Parameters
[out]valPointer to the signed integer value.
Returns
If successful, returns the MpWalker object pointing to the next element.

◆ GetInt32()

nn::nlib::msgpack::MpWalker::GetInt32 ( ) const
inlinenoexcept

If an integer that can be stored in the int32_t type is stored, gets the integer value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed.

Returns
A pair of the MpWalker object and the int32_t type value.

Definition at line 116 of file MpWalker.h.

◆ GetInt64()

nn::nlib::msgpack::MpWalker::GetInt64 ( ) const
inlinenoexcept

If an integer that can be stored in the int64_t type is stored, gets the integer value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed.

Returns
A pair of the MpWalker object and the int64_t type value.

Definition at line 121 of file MpWalker.h.

◆ GetMapCount() [1/2]

nn::nlib::msgpack::MpWalker::GetMapCount ( uint32_t *  n) const
noexcept

If an associative array is stored, gets the associative array size.

Parameters
[out]nSize of the associative array.
Returns
If successful, returns the MpWalker object pointing to the next element (the first key in the associative array).

◆ GetMapCount() [2/2]

nn::nlib::msgpack::MpWalker::GetMapCount ( ) const
inlinenoexcept

If an associative array is stored, gets the associative array size.

Returns
A pair of the MpWalker object and the associative array size.
Description
The return value, the MpWalker object, points to the next element (the first key in an associative array) if successful, or is an invalid value if failed.
The following sample code gets the associative array size, and then associative array elements one by one.
const nlib_utf8_t* json = R"({"key1" : "value", "key2" : [1,2,3], "key3" : [true, null]})";
size_t n = ToMsgpack(buf, json);
MpWalker root(&buf[0], n);
auto ac_result = root.GetMapCount();
SUCCEED_IF(ac_result.first);
nlib_printf("Map Count: %u\n", ac_result.second);
for (uint32_t i = 0; i < ac_result.second; ++i) {
auto r = JsonStreamParser::Parse(ac_result.first);
SUCCEED_IF(r.first == 0);
ToJson(parsed_key, *r.second.get());
ac_result.first = ac_result.first.Skip();
SUCCEED_IF(ac_result.first);
r = JsonStreamParser::Parse(ac_result.first);
SUCCEED_IF(r.first == 0);
ToJson(parsed_value, *r.second.get());
nlib_printf("root[%s]: %s\n", parsed_key, parsed_value);
ac_result.first = ac_result.first.Skip();
SUCCEED_IF(ac_result.first);
}
/*
Output:
Map Count: 3
root["key1"]: "value"
root["key2"]: [1,2,3]
root["key3"]: [true,null]
*/

Definition at line 101 of file MpWalker.h.

◆ GetNil()

nn::nlib::msgpack::MpWalker::GetNil ( ) const
noexcept

Gets nil.

Returns
If successful, returns the MpWalker object pointing to the next element.

◆ GetPtr()

nn::nlib::msgpack::MpWalker::GetPtr ( ) const
inlinenoexcept

Gets a pointer to the current location of MessagePack data.

Returns
Pointer to MessagePack data.

Definition at line 57 of file MpWalker.h.

◆ GetSize()

nn::nlib::msgpack::MpWalker::GetSize ( ) const
inlinenoexcept

Gets the size of the MessagePack data.

Returns
Size of the MessagePack data.

Definition at line 60 of file MpWalker.h.

◆ GetString() [1/2]

nn::nlib::msgpack::MpWalker::GetString ( const nlib_utf8_t **  str,
uint32_t *  n 
) const
noexcept

Gets the str format data.

Parameters
[out]strPointer to the string (Not null terminated).
[out]nLength of the string.
Returns
If successful, returns the MpWalker object pointing to the next element.
Description
Whether the string is UTF-8 is not checked. It is the user's responsibility to check it if necessary.
See also
https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str

◆ GetString() [2/2]

nn::nlib::msgpack::MpWalker::GetString ( ) const
inlinenoexcept

If a string is stored, gets the string (str format).

Returns
A tupple of the MpWalker object, a pointer to the string, and the string length.
Description
The pointer to a string indicates an address in the buffer. Whether the string is UTF-8 is not checked. It is the user's responsibility to check it if necessary.
See also
https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str

Definition at line 82 of file MpWalker.h.

◆ GetTimestamp() [1/2]

nn::nlib::msgpack::MpWalker::GetTimestamp ( nlib_time val) const
noexcept

Obtains the timestamp.

Parameters
[out]valPointer to the time value.
Returns
If successful, returns the MpWalker object pointing to the next element.

◆ GetTimestamp() [2/2]

nn::nlib::msgpack::MpWalker::GetTimestamp ( ) const
inlinenoexcept

If the timestamp extended type is stored, gets data as the nlib_time type value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed.

Returns
A pair of the MpWalker object and the nlib_time type value.

Definition at line 146 of file MpWalker.h.

◆ GetUint() [1/2]

nn::nlib::msgpack::MpWalker::GetUint ( uint32_t *  val) const
noexcept

Gets an unsigned integer value.

Parameters
[out]valPointer to the unsigned integer value.
Returns
If successful, returns the MpWalker object pointing to the next element.

◆ GetUint() [2/2]

nn::nlib::msgpack::MpWalker::GetUint ( uint64_t *  val) const
noexcept

Gets an unsigned integer value.

Parameters
[out]valPointer to the unsigned integer value.
Returns
If successful, returns the MpWalker object pointing to the next element.

◆ GetUint32()

nn::nlib::msgpack::MpWalker::GetUint32 ( ) const
inlinenoexcept

If an integer that can be stored in the uint32_t type is stored, gets the integer value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed.

Returns
A pair of the MpWalker object and the uint32_t type value.

Definition at line 126 of file MpWalker.h.

◆ GetUint64()

nn::nlib::msgpack::MpWalker::GetUint64 ( ) const
inlinenoexcept

If an integer that can be stored in the uint64_t type is stored, gets the integer value. The return value, the MpWalker object, points to the next element if successful, or is an invalid value if failed.

Returns
A pair of the MpWalker object and the uint64_t type value.

Definition at line 131 of file MpWalker.h.

◆ Init()

nn::nlib::msgpack::MpWalker::Init ( const void *  p,
size_t  n 
)
inlinenoexcept

Initialize with this function when using the default constructor.

Parameters
[in]pPointer to MessagePack data.
[in]nData size.
Returns
Returns true on success. (Always successful.)

Definition at line 42 of file MpWalker.h.


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