The Battle for Wesnoth  1.15.10+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 (std::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

std::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 std::optional (as of C++17 at least) does not support reference types (see [1]), the only way to use those is std::optional<std::reference_wrapper>. 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 does not rebind operator=() as std::optional does. Instead, assigning a value to this object will simply change the object to which it points. To change the value of the referred to object, use value() or one of the other operators.

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

Definition at line 36 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 41 of file optional_reference.hpp.

◆ optional_reference() [3/3]

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

Definition at line 46 of file optional_reference.hpp.

Member Function Documentation

◆ operator bool()

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

Definition at line 72 of file optional_reference.hpp.

◆ operator*()

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

Definition at line 92 of file optional_reference.hpp.

◆ operator->()

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

Definition at line 87 of file optional_reference.hpp.

◆ operator=()

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

Definition at line 66 of file optional_reference.hpp.

◆ 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 78 of file optional_reference.hpp.

◆ value()

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

Member Data Documentation

◆ opt_

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

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