Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
storm::OptionalRef< T > Class Template Reference

Helper class that optionally holds a reference to an object of type T. More...

#include <OptionalRef.h>

Public Types

using type = T
 

Public Member Functions

 OptionalRef ()
 Creates a non-initialized reference.
 
 OptionalRef (NullRefType)
 Creates a non-initialized reference.
 
template<class U , class = decltype(optionalref_detail::FUN<T>(std::declval<U>()), std::enable_if_t<!std::is_same_v<OptionalRef, std::remove_cv_t<std::remove_reference_t<U>>>>())>
constexpr OptionalRef (U &&u) noexcept(noexcept(optionalref_detail::FUN< T >(std::forward< U >(u))))
 Creates a reference to the provided object.
 
 OptionalRef (OptionalRef< T > const &other)=default
 Creates a copy of the given OptionalRef.
 
 OptionalRef (OptionalRef< T > &&other)=default
 Move constructs this OptionalRef from another one.
 
OptionalRefoperator= (OptionalRef const &other)=delete
 Deleted assignment operator (see class description)
 
 operator bool () const
 Yields true iff this contains a reference.
 
bool has_value () const
 Yields true iff this contains a reference.
 
T & operator* ()
 Accesses the contained reference (if any)
 
T const & operator* () const
 Accesses the contained reference (if any)
 
T & value ()
 Accesses the contained reference (if any)
 
T const & value () const
 Accesses the contained reference (if any)
 
T & value_or (T &defaultValue)
 Returns the contained reference (if any).
 
T const & value_or (T const &defaultValue) const
 Returns the contained reference (if any).
 
T * operator-> ()
 Yields a pointer to the referenced object (if any)
 
T const * operator-> () const
 Yields a pointer to the referenced object (if any)
 
void reset ()
 Unsets the reference.
 
void reset (NullRefType const &)
 Unsets the reference.
 
void reset (T &t)
 Rebinds the reference.
 

Detailed Description

template<class T>
class storm::OptionalRef< T >

Helper class that optionally holds a reference to an object of type T.

This mimics the interface of std::optional, except that an OptionalRef never takes ownership of an object.

Warning
An OptionalRef becomes invalid if the lifetime of the referenced object ends prematurely.
Note
A possible use case is the implementation of optional function arguments, where std::optional would trigger a deep copy of the object. For example, foo(storm::OptionalRef<T const> bar = storm::NullRef) instead of foo(T const& bar)
This class does not provide an operator= as this is prone to errors: should this re-bind the reference or call T::operator= ? Instead, the reset method can be used.
Template Parameters
TThe type of the referenced object

Definition at line 48 of file OptionalRef.h.

Member Typedef Documentation

◆ type

template<class T >
using storm::OptionalRef< T >::type = T

Definition at line 52 of file OptionalRef.h.

Constructor & Destructor Documentation

◆ OptionalRef() [1/5]

template<class T >
storm::OptionalRef< T >::OptionalRef ( )
inline

Creates a non-initialized reference.

Definition at line 57 of file OptionalRef.h.

◆ OptionalRef() [2/5]

template<class T >
storm::OptionalRef< T >::OptionalRef ( NullRefType  )
inline

Creates a non-initialized reference.

Definition at line 62 of file OptionalRef.h.

◆ OptionalRef() [3/5]

template<class T >
template<class U , class = decltype(optionalref_detail::FUN<T>(std::declval<U>()), std::enable_if_t<!std::is_same_v<OptionalRef, std::remove_cv_t<std::remove_reference_t<U>>>>())>
constexpr storm::OptionalRef< T >::OptionalRef ( U &&  u)
inlineconstexprnoexcept

Creates a reference to the provided object.

Parameters
objthe object this will be a reference to
Note
Exploits template argument deduction so that the class template can be derived from expressions like OptionalRef(foo) (even if foo is of type e.g. T&)

Definition at line 72 of file OptionalRef.h.

◆ OptionalRef() [4/5]

template<class T >
storm::OptionalRef< T >::OptionalRef ( OptionalRef< T > const &  other)
default

Creates a copy of the given OptionalRef.

this and other will both reference the same object

◆ OptionalRef() [5/5]

template<class T >
storm::OptionalRef< T >::OptionalRef ( OptionalRef< T > &&  other)
default

Move constructs this OptionalRef from another one.

Member Function Documentation

◆ has_value()

template<class T >
bool storm::OptionalRef< T >::has_value ( ) const
inline

Yields true iff this contains a reference.

Definition at line 100 of file OptionalRef.h.

◆ operator bool()

template<class T >
storm::OptionalRef< T >::operator bool ( ) const
inline

Yields true iff this contains a reference.

Definition at line 93 of file OptionalRef.h.

◆ operator*() [1/2]

template<class T >
T & storm::OptionalRef< T >::operator* ( )
inline

Accesses the contained reference (if any)

Precondition
this must contain a reference.

Definition at line 108 of file OptionalRef.h.

◆ operator*() [2/2]

template<class T >
T const & storm::OptionalRef< T >::operator* ( ) const
inline

Accesses the contained reference (if any)

Precondition
this must contain a reference.

Definition at line 117 of file OptionalRef.h.

◆ operator->() [1/2]

template<class T >
T * storm::OptionalRef< T >::operator-> ( )
inline

Yields a pointer to the referenced object (if any)

Precondition
this must contain a reference.

Definition at line 158 of file OptionalRef.h.

◆ operator->() [2/2]

template<class T >
T const * storm::OptionalRef< T >::operator-> ( ) const
inline

Yields a pointer to the referenced object (if any)

Precondition
this must contain a reference.

Definition at line 167 of file OptionalRef.h.

◆ operator=()

template<class T >
OptionalRef & storm::OptionalRef< T >::operator= ( OptionalRef< T > const &  other)
delete

Deleted assignment operator (see class description)

◆ reset() [1/3]

template<class T >
void storm::OptionalRef< T >::reset ( )
inline

Unsets the reference.

has_value() yields false after calling this.

Definition at line 175 of file OptionalRef.h.

◆ reset() [2/3]

template<class T >
void storm::OptionalRef< T >::reset ( NullRefType const &  )
inline

Unsets the reference.

has_value() yields false after calling this.

Definition at line 182 of file OptionalRef.h.

◆ reset() [3/3]

template<class T >
void storm::OptionalRef< T >::reset ( T &  t)
inline

Rebinds the reference.

has_value()’ yields true after calling this.

Definition at line 189 of file OptionalRef.h.

◆ value() [1/2]

template<class T >
T & storm::OptionalRef< T >::value ( )
inline

Accesses the contained reference (if any)

Precondition
this must contain a reference.

Definition at line 126 of file OptionalRef.h.

◆ value() [2/2]

template<class T >
T const & storm::OptionalRef< T >::value ( ) const
inline

Accesses the contained reference (if any)

Precondition
this must contain a reference.

Definition at line 135 of file OptionalRef.h.

◆ value_or() [1/2]

template<class T >
T & storm::OptionalRef< T >::value_or ( T &  defaultValue)
inline

Returns the contained reference (if any).

Otherwise, the provided default value is returned.

Definition at line 143 of file OptionalRef.h.

◆ value_or() [2/2]

template<class T >
T const & storm::OptionalRef< T >::value_or ( T const &  defaultValue) const
inline

Returns the contained reference (if any).

Otherwise, the provided default value is returned.

Definition at line 150 of file OptionalRef.h.


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