Zserio C++17 runtime library  1.1.0
Built for Zserio 2.18.1
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 >
constexpr 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)
 
template<typename U >
constexpr BasicOptional (BasicOptional< ALLOC, U > &&other)
 
template<typename U >
constexpr BasicOptional (BasicOptional< ALLOC, U > &&other, const ALLOC &allocator)
 
template<typename U >
BasicOptionaloperator= (BasicOptional< ALLOC, U > &&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 T && value () &&
 
constexpr const T & value () const &
 
constexpr const T && value () const &&
 
T & operator* () &
 
T && operator* () &&
 
const T & operator* () const &
 
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
 

Friends

template<typename A , typename U >
class BasicOptional
 

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 107 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 114 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 115 of file Optional.h.

Constructor & Destructor Documentation

◆ BasicOptional() [1/17]

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

Default constructor.

Definition at line 120 of file Optional.h.

◆ BasicOptional() [2/17]

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

Nullopt constructor (implicit).

Definition at line 127 of file Optional.h.

◆ BasicOptional() [3/17]

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 137 of file Optional.h.

◆ BasicOptional() [4/17]

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 147 of file Optional.h.

◆ BasicOptional() [5/17]

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 156 of file Optional.h.

◆ BasicOptional() [6/17]

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 171 of file Optional.h.

◆ BasicOptional() [7/17]

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 186 of file Optional.h.

◆ BasicOptional() [8/17]

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 202 of file Optional.h.

◆ BasicOptional() [9/17]

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 218 of file Optional.h.

◆ ~BasicOptional()

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

Destructor.

Definition at line 225 of file Optional.h.

◆ BasicOptional() [10/17]

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 235 of file Optional.h.

◆ BasicOptional() [11/17]

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 248 of file Optional.h.

◆ BasicOptional() [12/17]

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 263 of file Optional.h.

◆ BasicOptional() [13/17]

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

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 278 of file Optional.h.

◆ BasicOptional() [14/17]

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 364 of file Optional.h.

◆ BasicOptional() [15/17]

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 378 of file Optional.h.

◆ BasicOptional() [16/17]

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

Conversion move constructor.

Parameters
otherOptional to move.

Definition at line 412 of file Optional.h.

◆ BasicOptional() [17/17]

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

Allocator-extended conversion move constructor.

Parameters
otherOptional to move.
allocatorAllocator to be used for dynamic memory allocations.

Definition at line 425 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 515 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 457 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 502 of file Optional.h.

◆ operator*() [1/4]

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 606 of file Optional.h.

◆ operator*() [2/4]

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

Returns contained value (overload when called on temporary values).

Returns
The stored value.
Exceptions
BadOptionalAccessif optional is empty.

Definition at line 618 of file Optional.h.

◆ operator*() [3/4]

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 630 of file Optional.h.

◆ operator*() [4/4]

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

Returns contained value (overload when called on temporary values).

Returns
The stored value.
Exceptions
BadOptionalAccessif optional is empty.

Definition at line 642 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 654 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 666 of file Optional.h.

◆ operator=() [1/6]

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 391 of file Optional.h.

◆ operator=() [2/6]

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

Conversion assignment operator.

Parameters
otherOptional to move from.
Returns
Reference to this.

Definition at line 439 of file Optional.h.

◆ operator=() [3/6]

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 293 of file Optional.h.

◆ operator=() [4/6]

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 318 of file Optional.h.

◆ operator=() [5/6]

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 335 of file Optional.h.

◆ operator=() [6/6]

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 351 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 476 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 487 of file Optional.h.

◆ value() [1/4]

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 536 of file Optional.h.

◆ value() [2/4]

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

Returns contained value (overload when called on temporary values).

Returns
The stored value.
Exceptions
BadOptionalAccessif optional is empty.

Definition at line 559 of file Optional.h.

◆ value() [3/4]

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 571 of file Optional.h.

◆ value() [4/4]

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

Returns contained value (overload when called on temporary values).

Returns
The stored value.
Exceptions
BadOptionalAccessif optional is empty.

Definition at line 594 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 679 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 696 of file Optional.h.

Friends And Related Function Documentation

◆ BasicOptional

template<typename ALLOC , typename T >
template<typename A , typename U >
friend class BasicOptional
friend

Definition at line 708 of file Optional.h.


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