Crunch
A Message Definition Language for Getting Things Right
Loading...
Searching...
No Matches
Crunch::messages::Field< Id, PresenceValidator, Type > Class Template Reference

Wrapper for a message field within a CrunchMessage. More...

#include <crunch_field.hpp>

Public Types

using FieldType = Type
 

Public Member Functions

constexpr auto validate_presence () const noexcept -> std::optional< Error >
 Check presence requirement.
 
template<typename T >
constexpr auto set (const T &val) noexcept
 Set the value.
 
template<typename T >
requires (!fields::is_string_v<Type> && !IsMessage<Type>)
constexpr void set_without_validation (const T &val) noexcept
 Set the value, bypassing validation.
 
constexpr auto get () const noexcept
 Get the user-facing value.
 
constexpr void clear () noexcept
 Clear the field value.
 
constexpr auto Validate () const noexcept -> std::optional< Error >
 Validate the field value.
 
constexpr bool operator== (const Field &other) const noexcept
 

Static Public Attributes

static constexpr FieldId field_id = Id
 

Friends

template<std::size_t Alignment>
struct Crunch::serdes::StaticLayout
 The serdes layout needs access to the internal state for efficent deserialization. TODO: Figure out a way to manage this without friends and give plugin serializers access to the internal state without adding each new serializer to the Field class. Maybe via a proxy class.
 
struct Crunch::serdes::TlvLayout
 

Detailed Description

template<FieldId Id, typename PresenceValidator, typename Type>
requires IsPresenceValidator<PresenceValidator>
class Crunch::messages::Field< Id, PresenceValidator, Type >

Wrapper for a message field within a CrunchMessage.

Associates a FieldId and Presence requirements with a data Type.

Template Parameters
IdThe unique FieldId.
PresenceValidatorThe presence validation policy (Optional/Required).
TypeThe underlying field type (Scalar, String, Array, or Message).

Member Function Documentation

◆ get()

template<FieldId Id, typename PresenceValidator , typename Type >
constexpr auto Crunch::messages::Field< Id, PresenceValidator, Type >::get ( ) const
inlineconstexprnoexcept

Get the user-facing value.

Returns
- Messages: const T* (nullptr if unset).
- Scalars/Strings: std::optional<ValueType> (nullopt if unset).
- Arrays: std::optional<std::span<const ValueType>> (nullopt if unset).

◆ set()

template<FieldId Id, typename PresenceValidator , typename Type >
template<typename T >
constexpr auto Crunch::messages::Field< Id, PresenceValidator, Type >::set ( const T &  val)
inlineconstexprnoexcept

Set the value.

Returns
- std::optional<Error> for types that validate on set (String, Array).
- void for types that cannot fail on set (Scalar, Message).

◆ Validate()

template<FieldId Id, typename PresenceValidator , typename Type >
constexpr auto Crunch::messages::Field< Id, PresenceValidator, Type >::Validate ( ) const -> std::optional<Error>
inlineconstexprnoexcept

Validate the field value.

Returns
std::optional<Error> Error if validation fails, otherwise std::nullopt.

◆ validate_presence()

template<FieldId Id, typename PresenceValidator , typename Type >
constexpr auto Crunch::messages::Field< Id, PresenceValidator, Type >::validate_presence ( ) const -> std::optional<Error>
inlineconstexprnoexcept

Check presence requirement.

Returns
std::optional<Error> Error if presence check fails, otherwise std::nullopt.

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