Botan 3.7.1
Crypto and TLS for C&
Botan::CT::Option< T > Class Template Referencefinal

#include <ct_utils.h>

Public Member Functions

constexpr std::optional< Tas_optional_vartime () const
 
constexpr Choice has_value () const
 Return true if this Option contains a value.
 
constexpr CT::Option< Toperator&& (CT::Choice also)
 Return a new CT::Option that is set if also is set as well.
 
constexpr Option ()
 Construct an unset option with a default inner value.
 
constexpr Option (T v)
 Construct a set option with the provided value.
 
constexpr Option (T v, Choice valid)
 Construct an Option which contains the specified value, and is set or not.
 
template<std::invocable< const T & > F>
constexpr auto transform (F f) const -> Option< std::remove_cvref_t< std::invoke_result_t< F, const T & > > >
 
constexpr const Tvalue () const
 Either returns the value or throws an exception.
 
constexpr T value_or (T other) const
 
constexpr T value_or (T other) const
 

Detailed Description

template<typename T>
class Botan::CT::Option< T >

A CT::Option<T> is either a valid T, or not

To maintain constant time behavior a value must always be stored. A CT::Choice tracks if the value is valid or not. It is not possible to access the inner value if the Choice is unset.

Definition at line 655 of file ct_utils.h.

Constructor & Destructor Documentation

◆ Option() [1/3]

template<typename T >
Botan::CT::Option< T >::Option ( T v,
Choice valid )
inlineconstexpr

Construct an Option which contains the specified value, and is set or not.

Definition at line 658 of file ct_utils.h.

658: m_has_value(valid), m_value(std::move(v)) {}

◆ Option() [2/3]

template<typename T >
Botan::CT::Option< T >::Option ( T v)
inlineconstexpr

Construct a set option with the provided value.

Definition at line 661 of file ct_utils.h.

661: Option(std::move(v), Choice::yes()) {}
static constexpr Choice yes()
Definition ct_utils.h:325
constexpr Option()
Construct an unset option with a default inner value.
Definition ct_utils.h:664

◆ Option() [3/3]

template<typename T >
Botan::CT::Option< T >::Option ( )
inlineconstexpr

Construct an unset option with a default inner value.

Definition at line 664 of file ct_utils.h.

666 : Option(T(), Choice::no()) {}
static constexpr Choice no()
Definition ct_utils.h:327
FE_25519 T
Definition ge.cpp:34

Member Function Documentation

◆ as_optional_vartime()

template<typename T >
std::optional< T > Botan::CT::Option< T >::as_optional_vartime ( ) const
inlineconstexpr

Convert this Option into a std::optional

This is not constant time, leaking if the Option had a value or not

Definition at line 715 of file ct_utils.h.

715 {
716 if(m_has_value.as_bool()) {
717 return {m_value};
718 } else {
719 return {};
720 }
721 }
constexpr bool as_bool() const
Definition ct_utils.h:349

References Botan::CT::Choice::as_bool().

◆ has_value()

template<typename T >
Choice Botan::CT::Option< T >::has_value ( ) const
inlineconstexpr

Return true if this Option contains a value.

Definition at line 669 of file ct_utils.h.

669{ return m_has_value; }

◆ operator&&()

template<typename T >
CT::Option< T > Botan::CT::Option< T >::operator&& ( CT::Choice also)
inlineconstexpr

Return a new CT::Option that is set if also is set as well.

Definition at line 724 of file ct_utils.h.

724{ return CT::Option<T>(m_value, m_has_value && also); }

◆ transform()

template<typename T >
template<std::invocable< const T & > F>
auto Botan::CT::Option< T >::transform ( F f) const -> Option<std::remove_cvref_t<std::invoke_result_t<F, const T&>>>
inlineconstexpr

Apply a function to the inner value and return a new Option which contains that value. This is constant time only if f is.

Note
The function will always be called, even if the Option is None. It must be prepared to handle any possible state of T.

Definition at line 679 of file ct_utils.h.

679 {
680 return {f(m_value), m_has_value};
681 }

◆ value()

template<typename T >
const T & Botan::CT::Option< T >::value ( ) const
inlineconstexpr

Either returns the value or throws an exception.

Definition at line 684 of file ct_utils.h.

684 {
685 BOTAN_STATE_CHECK(m_has_value.as_bool());
686 return m_value;
687 }
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:41

References Botan::CT::Choice::as_bool(), and BOTAN_STATE_CHECK.

◆ value_or() [1/2]

template<typename T >
T Botan::CT::Option< T >::value_or ( T other) const
inlineconstexpr

Returns either the inner value or the alternative, in constant time

This variant is used for types which explicitly define a function conditional_assign which takes a CT::Choice as the conditional.

Definition at line 693 of file ct_utils.h.

695 {
696 other.conditional_assign(m_has_value, m_value);
697 return other;
698 }

◆ value_or() [2/2]

template<typename T >
T Botan::CT::Option< T >::value_or ( T other) const
inlineconstexpr

Returns either the inner value or the alternative, in constant time

This variant is used for integer types where CT::Mask can perform a constant time selection

Definition at line 704 of file ct_utils.h.

706 {
707 auto mask = CT::Mask<T>::from_choice(m_has_value);
708 return mask.select(m_value, other);
709 }
static constexpr Mask< T > from_choice(Choice c)
Definition ct_utils.h:413

References Botan::CT::Mask< T >::from_choice().


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