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

Concept for valid element types within Field or ArrayField. More...

#include <crunch_field.hpp>

Public Types

using ValueType = ElementType
 
using FieldType = ArrayField
 

Public Member Functions

constexpr std::optional< Erroradd (const ElementType &val) noexcept
 Adds an element to the array.
 
template<std::size_t N>
requires (N <= MaxSize)
constexpr std::optional< Errorset (const std::array< ElementType, N > &other) noexcept
 Set array contents from a std::array.
 
constexpr std::optional< Errorset (const ArrayField &other) noexcept
 Set array contents from another ArrayField.
 
constexpr void clear () noexcept
 Clear the array (sets size to 0).
 
constexpr std::size_t size () const noexcept
 Get the current number of elements.
 
constexpr bool empty () const noexcept
 Check if the array is empty.
 
constexpr auto get () const noexcept
 Get read-only span of active elements.
 
constexpr const ElementType & operator[] (std::size_t index) const noexcept
 Access element at index (unchecked).
 
constexpr const ElementType & at (std::size_t index) const
 Access element at index.
 
constexpr auto Validate () const noexcept -> std::optional< Error >
 Validates the array and its elements.
 
constexpr bool operator== (const ArrayField &other) const noexcept
 
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 ElementType, std::size_t MaxSize, typename... Validators>
class Crunch::messages::ArrayField< Id, ElementType, MaxSize, Validators >

Concept for valid element types within Field or ArrayField.

Self-contained array field with storage, validation, and field metadata.

Arrays do not have Required/Optional presence - "set" is derived from size()

‍0. Use array-level validators like LengthAtLeast<N> instead.

Template Parameters
IdThe unique FieldId.
ElementTypeThe element type (Scalar, String, or Message).
MaxSizeMaximum number of elements.
ValidatorsValidators to apply to the Array (e.g., LengthAtLeast).

Member Function Documentation

◆ add()

template<FieldId Id, typename ElementType , std::size_t MaxSize, typename... Validators>
constexpr std::optional< Error > Crunch::messages::ArrayField< Id, ElementType, MaxSize, Validators >::add ( const ElementType &  val)
inlineconstexprnoexcept

Adds an element to the array.

Parameters
valThe value to add.
Returns
std::nullopt on success, or CapacityExceeded error.

◆ at()

template<FieldId Id, typename ElementType , std::size_t MaxSize, typename... Validators>
constexpr const ElementType & Crunch::messages::ArrayField< Id, ElementType, MaxSize, Validators >::at ( std::size_t  index) const
inlineconstexpr

Access element at index.

Returns
Const reference to element.

◆ empty()

template<FieldId Id, typename ElementType , std::size_t MaxSize, typename... Validators>
constexpr bool Crunch::messages::ArrayField< Id, ElementType, MaxSize, Validators >::empty ( ) const
inlineconstexprnoexcept

Check if the array is empty.

Returns
true if size() == 0, false otherwise.

◆ get()

template<FieldId Id, typename ElementType , std::size_t MaxSize, typename... Validators>
constexpr auto Crunch::messages::ArrayField< Id, ElementType, MaxSize, Validators >::get ( ) const
inlineconstexprnoexcept

Get read-only span of active elements.

Returns
std::span<const ElementType> of current elements.

◆ operator[]()

template<FieldId Id, typename ElementType , std::size_t MaxSize, typename... Validators>
constexpr const ElementType & Crunch::messages::ArrayField< Id, ElementType, MaxSize, Validators >::operator[] ( std::size_t  index) const
inlineconstexprnoexcept

Access element at index (unchecked).

Returns
Const reference to element.

◆ set() [1/2]

template<FieldId Id, typename ElementType , std::size_t MaxSize, typename... Validators>
constexpr std::optional< Error > Crunch::messages::ArrayField< Id, ElementType, MaxSize, Validators >::set ( const ArrayField< Id, ElementType, MaxSize, Validators > &  other)
inlineconstexprnoexcept

Set array contents from another ArrayField.

Returns
std::nullopt on success.

◆ set() [2/2]

template<FieldId Id, typename ElementType , std::size_t MaxSize, typename... Validators>
template<std::size_t N>
requires (N <= MaxSize)
constexpr std::optional< Error > Crunch::messages::ArrayField< Id, ElementType, MaxSize, Validators >::set ( const std::array< ElementType, N > &  other)
inlineconstexprnoexcept

Set array contents from a std::array.

Template Parameters
NSize of the input array. Must be <= MaxSize.
Returns
std::nullopt on success.

◆ size()

template<FieldId Id, typename ElementType , std::size_t MaxSize, typename... Validators>
constexpr std::size_t Crunch::messages::ArrayField< Id, ElementType, MaxSize, Validators >::size ( ) const
inlineconstexprnoexcept

Get the current number of elements.

Returns
Number of elements in the array.

◆ Validate()

template<FieldId Id, typename ElementType , std::size_t MaxSize, typename... Validators>
constexpr auto Crunch::messages::ArrayField< Id, ElementType, MaxSize, Validators >::Validate ( ) const -> std::optional<Error>
inlineconstexprnoexcept

Validates the array and its elements.

Returns
std::nullopt on success, or Error on validation failure.

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