Zakero's C++ Header Libraries
A collection of reusable C++ libraries
Public Member Functions | List of all members
zakero::messagepack::Map Struct Reference

A Key/Value collection of Objects. More...

Public Member Functions

const Objectat (const Object &) const noexcept
 Get the value of a key. More...
 
Objectat (Object &) noexcept
 Get the value of a key. More...
 
void clear () noexcept
 Remove the contents of the Map. More...
 
void erase (const Object &) noexcept
 Erase a key/value pair. More...
 
bool keyExists (const Object &) const noexcept
 Check if a key exists. More...
 
std::error_code set (const Object &, const Object &) noexcept
 Set a key/value pair. More...
 
std::error_code set (Object &, Object &) noexcept
 Set a key/value pair. More...
 
size_t size () const noexcept
 Get the size of the Map. More...
 

Detailed Description

The MessagePack specification allows any Object to be the "key" of a map.
As interesting as it would be to having a Map as the key to another Map, programmatically, it is not practical. This implementation limits the types of the "keys" to include all MessagePack types except Array, Binary, Ext, and Map.

The Map uses several std::map's to hold the "values". While it is possible to directly access these std::map's, it is recommended to use Map's methods since they ensure the uniqueness of the key and other checks.

Objects can be tested to find out if they are Map by using Object::isMap() and converted into a Map with Object::asMap(). A Map can not be converted into an Object. However, an Object can be constructed using a Map.

Member Function Documentation

◆ at() [1/2]

const Object & zakero::messagepack::Map::at ( const Object key) const
noexcept

The value associated with the provided key will be returned. If the key does not exist, then a reference to the key will be returned.

Example
map.set(Object{uint64_t(42)}, Object{std::string("The Answer"});
const zakero::messagepack::Object key = Object{true};
const zakero::messagepack::Object& object = map.at({true});
if(&object == &key)
{
// key not found
}
A Key/Value collection of Objects.
Definition: Zakero_MessagePack.h:313
std::error_code set(Object &, Object &) noexcept
Set a key/value pair.
Definition: Zakero_MessagePack.h:3108
Object & at(Object &) noexcept
Get the value of a key.
Definition: Zakero_MessagePack.h:3521
A Data Object.
Definition: Zakero_MessagePack.h:337
Returns
The value.
Parameters
keyThe key

◆ at() [2/2]

Object & zakero::messagepack::Map::at ( Object key)
noexcept

The value associated with the provided key will be returned. If the key does not exist, then a reference to the key will be returned.
Checking if the key exists (keyExists()) is more reliable than using this error handling.

Example
map.set(Object{uint64_t(42)}, Object{std::string("The Answer"});
zakero::messagepack::Object& thing = map.at({uint64_t(42)});
zakero::messagepack::Object key = Object{true};
zakero::messagepack::Object& object = map.at(key);
if(&object == &key)
{
// key not found
}
Returns
The value.
Parameters
keyThe key

◆ clear()

void zakero::messagepack::Map::clear ( )
inlinenoexcept

All of the key/value pairs will be removed.

◆ erase()

void zakero::messagepack::Map::erase ( const Object key)
noexcept

If the specified key exists, the key and matching value will be removed frame the Map.

Example
// What is true?
map.set({true}, {uint64_t(123456)});
// Truth can not be defined...
map.erase({true});
void erase(const Object &) noexcept
Erase a key/value pair.
Definition: Zakero_MessagePack.h:3227
Parameters
keyThe key

◆ keyExists()

bool zakero::messagepack::Map::keyExists ( const Object key) const
noexcept

Search the Map and return true if the key exists. If the key was not found, return false.

Example
map.set(Object{}, Object{});
if(map.keyExists(Object{}))
{
// In the Key of Null
}
bool keyExists(const Object &) const noexcept
Check if a key exists.
Definition: Zakero_MessagePack.h:3323
Return values
trueThe key exists.
falseThe key does not exist.
Parameters
keyThe key Object

◆ set() [1/2]

std::error_code zakero::messagepack::Map::set ( const Object key,
const Object value 
)
noexcept

The provided key / value pair will be added to the Map. If the key already exists, its value will be replaced with value.

Example
map.set({std::string("Error Code")} , {uint64_t(42)});
map.set({std::string("Error Message")}, {std::string("All the errors!")});
Returns
An error code.
Parameters
keyThe key
valueThe value

◆ set() [2/2]

std::error_code zakero::messagepack::Map::set ( Object key,
Object value 
)
noexcept

The provided key / value pair will be added to the Map. If the key already exists, its value will be replaced with value.

Example
map.set({std::string("Error Code")} , {uint64_t(42)});
map.set({std::string("Error Message")}, {std::string("All the errors!")});
Returns
An error code.
Parameters
keyThe key
valueThe value

◆ size()

size_t zakero::messagepack::Map::size ( ) const
inlinenoexcept
Returns
The number of key/value pairs.

The documentation for this struct was generated from the following file: