Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::OptionalRef< T > Class Template Reference

A class that simulates an optional reference to a constant object of type T. More...

#include "donner/base/OptionalRef.h"

Public Types

using Type = const std::remove_cvref_t<T>
 Type of the referenced object.
 

Public Member Functions

constexpr OptionalRef () noexcept=default
 Constructs an empty OptionalRef.
 
 OptionalRef (std::nullopt_t nullopt) noexcept
 Constructs an empty OptionalRef from a std::nullopt.
 
constexpr OptionalRef (const T &ref) noexcept
 Constructs an OptionalRef that contains a reference to the given object.
 
 ~OptionalRef () noexcept=default
 Destructor.
 
constexpr OptionalRef (const OptionalRef &other) noexcept=default
 Copy constructor.
 
constexpr OptionalRef (OptionalRef &&other) noexcept=default
 Move constructor.
 
OptionalRefoperator= (const OptionalRef &other) noexcept=default
 Copy assignment operator.
 
OptionalRefoperator= (OptionalRef &&other) noexcept=default
 Move assignment operator.
 
OptionalRefoperator= (const T &ref) noexcept
 Assigns a reference to the given object.
 
void reset () noexcept
 Resets the OptionalRef to be empty.
 
constexpr operator bool () const noexcept
 Returns true if the OptionalRef contains a reference.
 
constexpr bool hasValue () const noexcept
 Returns true if the OptionalRef contains a reference.
 
constexpr const T & value () const
 Returns a const reference to the referenced object.
 
constexpr const T & operator* () const
 Returns a const reference to the referenced object.
 
constexpr const Typeoperator-> () const noexcept
 Returns a pointer to the referenced object.
 
constexpr operator std::optional< T > () const
 Implicit conversion to a std::optional<T>.
 

Friends

constexpr friend bool operator== (const OptionalRef &lhs, const OptionalRef &rhs)
 Equality operator to another OptionalRef.
 
constexpr friend bool operator== (const OptionalRef &lhs, std::nullopt_t)
 Equality operator to a std::nullopt.
 
constexpr friend bool operator== (const OptionalRef &lhs, const T &rhs)
 Equality operator for a set value.
 
std::ostream & operator<< (std::ostream &os, const OptionalRef &opt)
 Stream output operator.
 

Detailed Description

template<typename T>
class donner::OptionalRef< T >

A class that simulates an optional reference to a constant object of type T.

This class behaves similarly to std::optional<const T&>, which is proposed by the upcoming P2988 std::optional<T&> proposal, providing a way to store an optional reference to a constant object. Since std::optional cannot be used with reference types, OptionalRef provides a workaround.

Note
The referenced object must outlive the OptionalRef. It is the responsibility of the user to ensure that the reference remains valid.
Template Parameters
TThe type of the object to reference.

Constructor & Destructor Documentation

◆ OptionalRef() [1/4]

template<typename T >
donner::OptionalRef< T >::OptionalRef ( std::nullopt_t nullopt)
inlinenoexcept

Constructs an empty OptionalRef from a std::nullopt.

Parameters
nulloptAccepts std::nullopt to construct an empty OptionalRef.

◆ OptionalRef() [2/4]

template<typename T >
constexpr donner::OptionalRef< T >::OptionalRef ( const T & ref)
inlineconstexprnoexcept

Constructs an OptionalRef that contains a reference to the given object.

Parameters
refThe object to reference.

◆ OptionalRef() [3/4]

template<typename T >
constexpr donner::OptionalRef< T >::OptionalRef ( const OptionalRef< T > & other)
constexprdefaultnoexcept

Copy constructor.

Parameters
otherThe other OptionalRef to copy from.

◆ OptionalRef() [4/4]

template<typename T >
constexpr donner::OptionalRef< T >::OptionalRef ( OptionalRef< T > && other)
constexprdefaultnoexcept

Move constructor.

Parameters
otherThe other OptionalRef to move from.

Member Function Documentation

◆ hasValue()

template<typename T >
constexpr bool donner::OptionalRef< T >::hasValue ( ) const
inlineconstexprnoexcept

Returns true if the OptionalRef contains a reference.

Returns
true if the OptionalRef is not empty.

◆ operator bool()

template<typename T >
constexpr donner::OptionalRef< T >::operator bool ( ) const
inlineexplicitconstexprnoexcept

Returns true if the OptionalRef contains a reference.

Returns
true if the OptionalRef is not empty.

◆ operator*()

template<typename T >
constexpr const T & donner::OptionalRef< T >::operator* ( ) const
inlineconstexpr

Returns a const reference to the referenced object.

Returns
Const reference to the object.
Note
Asserts that the OptionalRef is not empty.

◆ operator->()

template<typename T >
constexpr const Type * donner::OptionalRef< T >::operator-> ( ) const
inlineconstexprnoexcept

Returns a pointer to the referenced object.

Returns
Pointer to the object.
Note
Asserts that the OptionalRef is not empty.

◆ operator=() [1/3]

template<typename T >
OptionalRef & donner::OptionalRef< T >::operator= ( const OptionalRef< T > & other)
defaultnoexcept

Copy assignment operator.

Parameters
otherThe other OptionalRef to copy from.
Returns
Reference to this OptionalRef.

◆ operator=() [2/3]

template<typename T >
OptionalRef & donner::OptionalRef< T >::operator= ( const T & ref)
inlinenoexcept

Assigns a reference to the given object.

Parameters
refThe object to reference.
Returns
Reference to this OptionalRef.

◆ operator=() [3/3]

template<typename T >
OptionalRef & donner::OptionalRef< T >::operator= ( OptionalRef< T > && other)
defaultnoexcept

Move assignment operator.

Parameters
otherThe other OptionalRef to move from.
Returns
Reference to this OptionalRef.

◆ value()

template<typename T >
constexpr const T & donner::OptionalRef< T >::value ( ) const
inlineconstexpr

Returns a const reference to the referenced object.

Returns
Const reference to the object.
Note
Asserts that the OptionalRef is not empty.

Friends And Related Symbol Documentation

◆ operator<<

template<typename T >
std::ostream & operator<< ( std::ostream & os,
const OptionalRef< T > & opt )
friend

Stream output operator.

Parameters
osOutput stream.
optThe OptionalRef to output.
Returns
Reference to the output stream.
Note
Outputs the referenced object if not empty, otherwise outputs "nullopt".

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