The Battle for Wesnoth  1.19.7+dev
Public Member Functions | Private Attributes | List of all members
utils::optional_reference< T > Class Template Reference

A simple wrapper class for optional reference types. More...

#include <optional_reference.hpp>

Public Member Functions

 optional_reference ()=default
 
 optional_reference (T &ref)
 
 optional_reference (utils::nullopt_t)
 
T & value () const
 
optional_reference< T > & operator= (T &new_ref)
 
 operator bool () const
 
T * ptr () const
 Returns a pointer to the referenced object or nullptr if no reference is held. More...
 
T * operator-> () const
 
T & operator* () const
 

Private Attributes

utils::optional< std::reference_wrapper< T > > opt_
 

Detailed Description

template<typename T>
class utils::optional_reference< T >

A simple wrapper class for optional reference types.

Since utils::optional (as of C++17 at least) does not support reference types (see [1]), the only way to use those is utils::optional<std::reference_wrapper<T>>. However, this makes the interace messy, as to access the referenced object you need an extra get() call to access the value stored in the reference wrapper.

This rebinds operator=() as boost::optional does. Assigning a value to this wrapper will simply change the object to which it points instead of assigning a value to the referenced object. To change the value of the referenced object, perform an assignment on value() or operator*.

[1] https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/

Definition at line 38 of file optional_reference.hpp.

Constructor & Destructor Documentation

◆ optional_reference() [1/3]

template<typename T >
utils::optional_reference< T >::optional_reference ( )
default

◆ optional_reference() [2/3]

template<typename T >
utils::optional_reference< T >::optional_reference ( T &  ref)
inline

Definition at line 43 of file optional_reference.hpp.

◆ optional_reference() [3/3]

template<typename T >
utils::optional_reference< T >::optional_reference ( utils::nullopt_t  )
inline

Definition at line 48 of file optional_reference.hpp.

Member Function Documentation

◆ operator bool()

template<typename T >
utils::optional_reference< T >::operator bool ( ) const
inlineexplicit

Definition at line 74 of file optional_reference.hpp.

References utils::optional_reference< T >::opt_.

◆ operator*()

template<typename T >
T& utils::optional_reference< T >::operator* ( ) const
inline

Definition at line 94 of file optional_reference.hpp.

References utils::optional_reference< T >::value().

◆ operator->()

template<typename T >
T* utils::optional_reference< T >::operator-> ( ) const
inline

Definition at line 89 of file optional_reference.hpp.

References utils::optional_reference< T >::value().

◆ operator=()

template<typename T >
optional_reference<T>& utils::optional_reference< T >::operator= ( T &  new_ref)
inline

Definition at line 68 of file optional_reference.hpp.

References utils::optional_reference< T >::opt_.

◆ ptr()

template<typename T >
T* utils::optional_reference< T >::ptr ( ) const
inline

Returns a pointer to the referenced object or nullptr if no reference is held.

Definition at line 80 of file optional_reference.hpp.

References utils::optional_reference< T >::opt_, and utils::optional_reference< T >::value().

◆ value()

template<typename T >
T& utils::optional_reference< T >::value ( ) const
inline

Member Data Documentation

◆ opt_

template<typename T >
utils::optional<std::reference_wrapper<T> > utils::optional_reference< T >::opt_
private

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