Zserio C++17 runtime library  0.5.0
Built for Zserio 2.17.0
zserio::BasicOptional< ALLOC, T > Class Template Reference

#include <Optional.h>

Inheritance diagram for zserio::BasicOptional< ALLOC, T >:
Collaboration diagram for zserio::BasicOptional< ALLOC, T >:

Public Types

using allocator_type = ALLOC
 
using OptionalType = std::optional< typename detail::optional_element< T >::type >
 
- Public Types inherited from zserio::AllocatorHolder< ALLOC, EMPTY >
using allocator_type = ALLOC
 

Public Member Functions

constexpr BasicOptional () noexcept
 
constexpr BasicOptional (std::nullopt_t, const ALLOC &allocator={}) noexcept
 
constexpr BasicOptional (const T &value, const ALLOC &allocator={})
 
constexpr BasicOptional (T &&value, const ALLOC &allocator={})
 
constexpr BasicOptional (const ALLOC &allocator)
 
template<typename... ARGS, typename TT = T, std::enable_if_t<!detail::is_optional_heap_allocated_v< TT >> * = nullptr, std::enable_if_t<!is_first_allocator< ARGS... >::value > * = nullptr>
constexpr BasicOptional (std::in_place_t, ARGS &&... args)
 
template<typename... ARGS, typename TT = T, std::enable_if_t<!detail::is_optional_heap_allocated_v< TT >> * = nullptr>
constexpr BasicOptional (std::in_place_t, const ALLOC &allocator, ARGS &&... args)
 
template<typename... ARGS, typename TT = T, std::enable_if_t< detail::is_optional_heap_allocated_v< TT >> * = nullptr>
constexpr BasicOptional (std::in_place_t, const ALLOC &allocator, ARGS &&... args)
 
template<typename... ARGS, typename TT = T, std::enable_if_t< detail::is_optional_heap_allocated_v< TT >> * = nullptr, std::enable_if_t<!is_first_allocator< ARGS... >::value > * = nullptr>
constexpr BasicOptional (std::in_place_t, ARGS &&... args)
 
 ~BasicOptional ()
 
constexpr BasicOptional (const BasicOptional &other)
 
template<typename U >
constexpr BasicOptional (const BasicOptional< ALLOC, U > &other)
 
constexpr BasicOptional (const BasicOptional &other, const ALLOC &allocator)
 
template<typename A , typename U >
 BasicOptional (const BasicOptional< A, U > &other, const ALLOC &allocator)
 
constexpr BasicOptionaloperator= (const BasicOptional &other)
 
template<typename A , typename U >
BasicOptionaloperator= (const BasicOptional< A, U > &other)
 
BasicOptionaloperator= (std::nullopt_t)
 
template<typename U = T, std::enable_if_t<!detail::is_optional_v< std::decay_t< U >> &&!(std::is_same_v< T, std::decay_t< U >> &&std::is_scalar_v< U >)> * = nullptr>
BasicOptionaloperator= (U &&value)
 
 BasicOptional (BasicOptional &&other)
 
 BasicOptional (BasicOptional &&other, const ALLOC &allocator)
 
BasicOptionaloperator= (BasicOptional &&other)
 
constexpr bool has_value () const noexcept
 
void reset ()
 
void swap (BasicOptional &other)
 
constexpr operator bool () const noexcept
 
template<typename... ARGS>
T & emplace (ARGS &&... args)
 
constexpr T & value ()
 
constexpr const T & value () const
 
T & operator* ()
 
const T & operator* () const
 
T * operator-> ()
 
const T * operator-> () const
 
template<typename U >
value_or (U &&def)
 
template<typename U >
value_or (U &&def) const
 
- Public Member Functions inherited from zserio::AllocatorHolder< ALLOC, EMPTY >
 AllocatorHolder ()
 
 AllocatorHolder (const allocator_type &allocator)
 
 AllocatorHolder (allocator_type &&allocator)
 
allocator_type get_allocator () const
 
 ~AllocatorHolder ()=default
 
 AllocatorHolder (const AllocatorHolder &other)=default
 
AllocatorHolderoperator= (const AllocatorHolder &other)=default
 
 AllocatorHolder (AllocatorHolder &&other)=default
 
AllocatorHolderoperator= (AllocatorHolder &&other)=default
 

Additional Inherited Members

- Protected Member Functions inherited from zserio::AllocatorHolder< ALLOC, EMPTY >
void set_allocator (const allocator_type &allocator)
 
void set_allocator (allocator_type &&allocator)
 
allocator_typeget_allocator_ref ()
 
const allocator_typeget_allocator_ref () const
 

Detailed Description

template<typename ALLOC, typename T>
class zserio::BasicOptional< ALLOC, T >

Implementation of optional type which allocates memory when type

  1. the type is explicitly tagged with IS_RECURSIVE type OR
  2. doesn't fit under 3*sizeof(void*)

Largely compatible with std::optional with following differences:

  • BasicOptional(U&&) implemented only with T because the required is_constructible constrain can't be used on incomplete types
  • operator=(U&&) implemented without is_constructible constrain because that can't be used on incomplete types
  • BasicOptional(std::in_place_t, std::initializer_list) not implemented
  • Monadic operations from C++23 not implemented
  • Conditional noexcept and conditional explicit-ness on certain members not implemented

Definition at line 105 of file Optional.h.

Member Typedef Documentation

◆ allocator_type

template<typename ALLOC , typename T >
using zserio::BasicOptional< ALLOC, T >::allocator_type = ALLOC

Definition at line 112 of file Optional.h.

◆ OptionalType

template<typename ALLOC , typename T >
using zserio::BasicOptional< ALLOC, T >::OptionalType = std::optional<typename detail::optional_element<T>::type>

Definition at line 113 of file Optional.h.

Constructor & Destructor Documentation

◆ BasicOptional() [1/15]

template<typename ALLOC , typename T >
constexpr zserio::BasicOptional< ALLOC, T >::BasicOptional ( )
inlineconstexprnoexcept

Default constructor.

Definition at line 118 of file Optional.h.

◆ BasicOptional() [2/15]

template<typename ALLOC , typename T >
constexpr zserio::BasicOptional< ALLOC, T >::BasicOptional ( std::nullopt_t  ,
const ALLOC &  allocator = {} 
)
inlineconstexprnoexcept

Nullopt constructor (implicit).

Definition at line 125 of file Optional.h.

◆ BasicOptional() [3/15]

template<typename ALLOC , typename T >
constexpr zserio::BasicOptional< ALLOC, T >::BasicOptional ( const T &  value,
const ALLOC &  allocator = {} 
)
inlineconstexpr

Constructor from given value (implicit).

Parameters
valueValue to be initialized with.
allocatorOptional allocator to be used.

Definition at line 135 of file Optional.h.

◆ BasicOptional() [4/15]

template<typename ALLOC , typename T >
constexpr zserio::BasicOptional< ALLOC, T >::BasicOptional ( T &&  value,
const ALLOC &  allocator = {} 
)
inlineconstexpr

Constructor from given value and allocator (implicit).

Parameters
valueValue to be initialized with.
allocatorOptional allocator to be used.

Definition at line 145 of file Optional.h.

◆ BasicOptional() [5/15]

template<typename ALLOC , typename T >
constexpr zserio::BasicOptional< ALLOC, T >::BasicOptional ( const ALLOC &  allocator)
inlineexplicitconstexpr

Constructor from given allocator.

Parameters
allocatorAllocator to be used.

Definition at line 154 of file Optional.h.

◆ BasicOptional() [6/15]

template<typename ALLOC , typename T >
template<typename... ARGS, typename TT = T, std::enable_if_t<!detail::is_optional_heap_allocated_v< TT >> * = nullptr, std::enable_if_t<!is_first_allocator< ARGS... >::value > * = nullptr>
constexpr zserio::BasicOptional< ALLOC, T >::BasicOptional ( std::in_place_t  ,
ARGS &&...  args 
)
inlineexplicitconstexpr

Constructor which constructs the value.

Parameters
in_placetag to request this constructor.
argsArguments to be forwarded for element construction.
Exceptions
canthrow any exception thrown by T.

Definition at line 169 of file Optional.h.

◆ BasicOptional() [7/15]

template<typename ALLOC , typename T >
template<typename... ARGS, typename TT = T, std::enable_if_t<!detail::is_optional_heap_allocated_v< TT >> * = nullptr>
constexpr zserio::BasicOptional< ALLOC, T >::BasicOptional ( std::in_place_t  ,
const ALLOC &  allocator,
ARGS &&...  args 
)
inlineconstexpr

Constructor which constructs the value.

Parameters
in_placetag to request this constructor.
allocatorAllocator to be used.
argsArguments to be forwarded for element construction.
Exceptions
canthrow any exception thrown by T.

Definition at line 184 of file Optional.h.

◆ BasicOptional() [8/15]

template<typename ALLOC , typename T >
template<typename... ARGS, typename TT = T, std::enable_if_t< detail::is_optional_heap_allocated_v< TT >> * = nullptr>
constexpr zserio::BasicOptional< ALLOC, T >::BasicOptional ( std::in_place_t  ,
const ALLOC &  allocator,
ARGS &&...  args 
)
inlineconstexpr

Constructor which constructs the value.

Parameters
in_placetag to request this constructor.
allocatorAllocator to be used.
argsArguments to be forwarded for element construction.
Exceptions
canthrow any exception thrown by T.

Definition at line 200 of file Optional.h.

◆ BasicOptional() [9/15]

template<typename ALLOC , typename T >
template<typename... ARGS, typename TT = T, std::enable_if_t< detail::is_optional_heap_allocated_v< TT >> * = nullptr, std::enable_if_t<!is_first_allocator< ARGS... >::value > * = nullptr>
constexpr zserio::BasicOptional< ALLOC, T >::BasicOptional ( std::in_place_t  ,
ARGS &&...  args 
)
inlineexplicitconstexpr

Constructor which constructs the value.

Parameters
in_placetag to request this constructor.
argsArguments to be forwarded for element construction.
Exceptions
canthrow any exception thrown by T.

Definition at line 216 of file Optional.h.

◆ ~BasicOptional()

template<typename ALLOC , typename T >
zserio::BasicOptional< ALLOC, T >::~BasicOptional ( )
inline

Destructor.

Definition at line 223 of file Optional.h.

◆ BasicOptional() [10/15]

template<typename ALLOC , typename T >
constexpr zserio::BasicOptional< ALLOC, T >::BasicOptional ( const BasicOptional< ALLOC, T > &  other)
inlineconstexpr

Copy constructor.

Parameters
otherOptional to copy.

Definition at line 233 of file Optional.h.

◆ BasicOptional() [11/15]

template<typename ALLOC , typename T >
template<typename U >
constexpr zserio::BasicOptional< ALLOC, T >::BasicOptional ( const BasicOptional< ALLOC, U > &  other)
inlineconstexpr

Conversion copy constructor.

Parameters
otherOptional to copy.

Definition at line 246 of file Optional.h.

◆ BasicOptional() [12/15]

template<typename ALLOC , typename T >
constexpr zserio::BasicOptional< ALLOC, T >::BasicOptional ( const BasicOptional< ALLOC, T > &  other,
const ALLOC &  allocator 
)
inlineconstexpr

Allocator-extended copy constructor.

Parameters
otherOptional to copy.
allocatorAllocator to be used for dynamic memory allocations.
Exceptions
canthrow any exception thrown by the active element in other.

Definition at line 261 of file Optional.h.

◆ BasicOptional() [13/15]

template<typename ALLOC , typename T >
template<typename A , typename U >
zserio::BasicOptional< ALLOC, T >::BasicOptional ( const BasicOptional< A, U > &  other,
const ALLOC &  allocator 
)
inline

Allocator-extended conversion copy constructor.

Parameters
otherOptional to copy.
allocatorAllocator to be used for dynamic memory allocations.
Exceptions
canthrow any exception thrown by the active element in other.

Definition at line 276 of file Optional.h.

◆ BasicOptional() [14/15]

template<typename ALLOC , typename T >
zserio::BasicOptional< ALLOC, T >::BasicOptional ( BasicOptional< ALLOC, T > &&  other)
inline

Move constructor.

Parameters
otherOptional to move from.
Exceptions
canthrow any exception thrown by the value in other.

Definition at line 362 of file Optional.h.

◆ BasicOptional() [15/15]

template<typename ALLOC , typename T >
zserio::BasicOptional< ALLOC, T >::BasicOptional ( BasicOptional< ALLOC, T > &&  other,
const ALLOC &  allocator 
)
inline

Allocator-extended move constructor.

Parameters
otherOptional to move from.
allocatorAllocator to be used for dynamic memory allocations.
Exceptions
canthrow any exception thrown by the value in other.

Definition at line 376 of file Optional.h.

Member Function Documentation

◆ emplace()

template<typename ALLOC , typename T >
template<typename... ARGS>
T& zserio::BasicOptional< ALLOC, T >::emplace ( ARGS &&...  args)
inline

Constructs a value in the Optional.

Parameters
argsArguments to be forwarded for value construction.
Returns
Reference to inserted value.

Definition at line 467 of file Optional.h.

◆ has_value()

template<typename ALLOC , typename T >
constexpr bool zserio::BasicOptional< ALLOC, T >::has_value ( ) const
inlineconstexprnoexcept

Reports if Optional has stored value.

Returns
True if value is set.

Definition at line 409 of file Optional.h.

◆ operator bool()

template<typename ALLOC , typename T >
constexpr zserio::BasicOptional< ALLOC, T >::operator bool ( ) const
inlineexplicitconstexprnoexcept

Reports if Optional has stored value.

Returns
True if value is set.

Definition at line 454 of file Optional.h.

◆ operator*() [1/2]

template<typename ALLOC , typename T >
T& zserio::BasicOptional< ALLOC, T >::operator* ( )
inline

Returns contained value.

Returns
Reference to stored value.
Exceptions
BadOptionalAccessif optional is empty.

Definition at line 534 of file Optional.h.

◆ operator*() [2/2]

template<typename ALLOC , typename T >
const T& zserio::BasicOptional< ALLOC, T >::operator* ( ) const
inline

Returns contained value.

Returns
Reference to stored value.
Exceptions
BadOptionalAccessif optional is empty.

Definition at line 546 of file Optional.h.

◆ operator->() [1/2]

template<typename ALLOC , typename T >
T* zserio::BasicOptional< ALLOC, T >::operator-> ( )
inline

Returns contained value.

Returns
Pointer to stored value.
Exceptions
BadOptionalAccessif optional is empty.

Definition at line 558 of file Optional.h.

◆ operator->() [2/2]

template<typename ALLOC , typename T >
const T* zserio::BasicOptional< ALLOC, T >::operator-> ( ) const
inline

Returns contained value.

Returns
Pointer to stored value.
Exceptions
BadOptionalAccessif optional is empty.

Definition at line 570 of file Optional.h.

◆ operator=() [1/5]

template<typename ALLOC , typename T >
BasicOptional& zserio::BasicOptional< ALLOC, T >::operator= ( BasicOptional< ALLOC, T > &&  other)
inline

Move assignment operator.

Parameters
otherOptional to move from.
Returns
Reference to this.

Definition at line 389 of file Optional.h.

◆ operator=() [2/5]

template<typename ALLOC , typename T >
constexpr BasicOptional& zserio::BasicOptional< ALLOC, T >::operator= ( const BasicOptional< ALLOC, T > &  other)
inlineconstexpr

Copy assignment operator.

Parameters
otherOptional to copy.
Returns
Reference to this.
Exceptions
canthrow any exception thrown by the active element in other.

Definition at line 291 of file Optional.h.

◆ operator=() [3/5]

template<typename ALLOC , typename T >
template<typename A , typename U >
BasicOptional& zserio::BasicOptional< ALLOC, T >::operator= ( const BasicOptional< A, U > &  other)
inline

Conversion copy assignment operator.

Parameters
otherOptional to copy.
Returns
Reference to this.
Exceptions
canthrow any exception thrown by the active element in other.

Definition at line 316 of file Optional.h.

◆ operator=() [4/5]

template<typename ALLOC , typename T >
BasicOptional& zserio::BasicOptional< ALLOC, T >::operator= ( std::nullopt_t  )
inline

Resetting assignment operator.

Returns
Reference to this.

Definition at line 333 of file Optional.h.

◆ operator=() [5/5]

template<typename ALLOC , typename T >
template<typename U = T, std::enable_if_t<!detail::is_optional_v< std::decay_t< U >> &&!(std::is_same_v< T, std::decay_t< U >> &&std::is_scalar_v< U >)> * = nullptr>
BasicOptional& zserio::BasicOptional< ALLOC, T >::operator= ( U &&  value)
inline

Value assignment operator.

Parameters
valueThe value to be emplaced.
Returns
Reference to this.

Definition at line 349 of file Optional.h.

◆ reset()

template<typename ALLOC , typename T >
void zserio::BasicOptional< ALLOC, T >::reset ( )
inline

Resets optional to empty state.

Definition at line 428 of file Optional.h.

◆ swap()

template<typename ALLOC , typename T >
void zserio::BasicOptional< ALLOC, T >::swap ( BasicOptional< ALLOC, T > &  other)
inline

Swaps content with other optional.

Parameters
otherOther optional to swap with.

Definition at line 439 of file Optional.h.

◆ value() [1/2]

template<typename ALLOC , typename T >
constexpr T& zserio::BasicOptional< ALLOC, T >::value ( )
inlineconstexpr

Returns contained value.

Returns
Reference to stored value.
Exceptions
BadOptionalAccessif optional is empty.

Definition at line 488 of file Optional.h.

◆ value() [2/2]

template<typename ALLOC , typename T >
constexpr const T& zserio::BasicOptional< ALLOC, T >::value ( ) const
inlineconstexpr

Returns contained value.

Returns
Reference to stored value.
Exceptions
BadOptionalAccessif optional is empty.

Definition at line 511 of file Optional.h.

◆ value_or() [1/2]

template<typename ALLOC , typename T >
template<typename U >
T zserio::BasicOptional< ALLOC, T >::value_or ( U &&  def)
inline

Returns contained value or default value if optional is empty.

Returns
Stored value or argument if empty.
Parameters
defdefault return value.

Definition at line 583 of file Optional.h.

◆ value_or() [2/2]

template<typename ALLOC , typename T >
template<typename U >
T zserio::BasicOptional< ALLOC, T >::value_or ( U &&  def) const
inline

Returns contained value or default value if optional is empty.

Returns
Stored value or argument if empty.
Parameters
defdefault return value.

Definition at line 600 of file Optional.h.


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