Crunch
A Message Definition Language for Getting Things Right
Loading...
Searching...
No Matches
Crunch::messages::MapField< Id, KeyField, ValueField, MaxSize, Validators > Class Template Reference

Map field mapping keys to values. More...

#include <crunch_field.hpp>

Public Types

using KeyType = field_value_type_t< KeyField >
 
using ValueType = field_value_type_t< ValueField >
 
using PairType = std::pair< KeyField, ValueField >
 
using FieldType = MapField
 

Public Member Functions

constexpr std::optional< Errorinsert (const KeyType &key, const ValueType &value) noexcept
 Inserts a key-value pair into the map.
 
constexpr std::optional< Errorinsert (const std::pair< KeyType, ValueType > &p) noexcept
 
constexpr bool remove (const KeyType &key) noexcept
 Removes a key and its value from the map.
 
constexpr std::optional< ValueField * > at (const KeyType &key) noexcept
 Get reference to value by key.
 
constexpr std::size_t size () const noexcept
 Get the current number of elements.
 
constexpr bool empty () const noexcept
 Check if the map is empty.
 
constexpr void clear () noexcept
 Clear the map.
 
constexpr auto Validate () const noexcept -> std::optional< Error >
 Validate the map and its elements.
 
constexpr bool operator== (const MapField &other) const noexcept
 Checks if two maps are equal (set equality).
 
auto begin () const noexcept
 
auto end () const noexcept
 
auto begin () noexcept
 
auto end () noexcept
 

Static Public Attributes

static constexpr FieldId field_id = Id
 
static constexpr std::size_t max_size = MaxSize
 

Friends

template<std::size_t Alignment>
struct Crunch::serdes::StaticLayout
 
struct Crunch::serdes::TlvLayout
 

Detailed Description

template<FieldId Id, typename KeyField, typename ValueField, std::size_t MaxSize, typename... Validators>
class Crunch::messages::MapField< Id, KeyField, ValueField, MaxSize, Validators >

Map field mapping keys to values.

Backed by std::array<std::pair<KeyField, ValueField>, MaxSize>. Keys and Values are other Crunch fields.

Template Parameters
IdThe unique FieldId.
KeyFieldThe type of the key field.
ValueFieldThe type of the value field.
MaxSizeMaximum number of key-value pairs.
ValidatorsValidators to apply to the Map.

Member Function Documentation

◆ at()

template<FieldId Id, typename KeyField , typename ValueField , std::size_t MaxSize, typename... Validators>
constexpr std::optional< ValueField * > Crunch::messages::MapField< Id, KeyField, ValueField, MaxSize, Validators >::at ( const KeyType &  key)
inlineconstexprnoexcept

Get reference to value by key.

Parameters
keyThe key to look up.
Returns
Optional pointer to the ValueField (empty if not found).

◆ insert()

template<FieldId Id, typename KeyField , typename ValueField , std::size_t MaxSize, typename... Validators>
constexpr std::optional< Error > Crunch::messages::MapField< Id, KeyField, ValueField, MaxSize, Validators >::insert ( const KeyType &  key,
const ValueType &  value 
)
inlineconstexprnoexcept

Inserts a key-value pair into the map.

Parameters
keyThe key to insert.
valueThe value to insert.
Returns
std::nullopt on success, or Error (CapacityExceeded or InvalidValue).

◆ operator==()

template<FieldId Id, typename KeyField , typename ValueField , std::size_t MaxSize, typename... Validators>
constexpr bool Crunch::messages::MapField< Id, KeyField, ValueField, MaxSize, Validators >::operator== ( const MapField< Id, KeyField, ValueField, MaxSize, Validators > &  other) const
inlineconstexprnoexcept

Checks if two maps are equal (set equality).

Warning
This operation is O(N^2) as it performs a linear scan for each element.

◆ remove()

template<FieldId Id, typename KeyField , typename ValueField , std::size_t MaxSize, typename... Validators>
constexpr bool Crunch::messages::MapField< Id, KeyField, ValueField, MaxSize, Validators >::remove ( const KeyType &  key)
inlineconstexprnoexcept

Removes a key and its value from the map.

Parameters
keyThe key to remove.
Returns
true if the key was found and removed, false otherwise.

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