OpenVPN 3 Core Library
Loading...
Searching...
No Matches
openvpn::optional< T > Class Template Reference

designed to represent an optional reference to an object of type T More...

#include <optional_ref.hpp>

Public Member Functions

constexpr optional () noexcept=default
 Default constructor. Creates an empty optional.
 
constexpr optional (std::nullopt_t noOption) noexcept
 Constructs an empty optional.
 
constexpr optional (T r) noexcept
 Constructs an optional containing a reference to the given object.
 
constexpr optional (value_type *r) noexcept
 Constructs an optional containing a reference to the given object.
 
constexpr optional (const optional &) noexcept=default
 Copy constructor.
 
constexpr optional (optional &&) noexcept=default
 Move constructor.
 
optionaloperator= (const optional &) noexcept=default
 Copy assignment operator.
 
optionaloperator= (optional &&) noexcept=default
 Move assignment operator.
 
optionaloperator= (std::nullopt_t noOption) noexcept
 Assigns the optional to be empty.
 
void reset () noexcept
 Resets the optional, making it empty.
 
constexpr bool has_value () const noexcept
 Checks whether the optional contains a value.
 
constexpr operator bool () const noexcept
 Checks whether the optional contains a value.
 
constexpr value_type value () const
 Gets the contained value.
 
constexpr value_type value_or (const value_type &default_value) const noexcept
 Returns the contained value if the optional is not empty, otherwise returns the provided default value.
 
const T operator* () const
 Dereference operator.
 
operator* ()
 Dereference operator.
 
const value_typeoperator-> () const
 Arrow operator.
 
value_typeoperator-> ()
 Arrow operator.
 

Private Types

using value_type = std::remove_reference_t< T >
 

Private Attributes

value_typemRef = nullptr
 

Static Private Attributes

static constexpr char errorMsg [] = "optional<T &>: access error"
 

Detailed Description

template<typename T>
requires std::is_reference_v<T>
class openvpn::optional< T >

designed to represent an optional reference to an object of type T

Template Parameters
Treference type

This code defines a template class called optional<T&> which is designed to represent an optional reference to an object of type T. The purpose of this class is to provide a way to handle situations where a reference might or might not be available, similar to how std::optional works for regular types.

The class doesn't take any direct inputs, but it can be instantiated with either no arguments (creating an empty optional), a reference to an object of type T, or another optional object. It produces an object that can either hold a reference to a T object or be empty.

The class achieves its purpose by using a pointer (T *mRef) internally to store the address of the referenced object. When the optional is empty, this pointer is set to nullptr. The class provides various methods to check if a reference is present, access the referenced object, and assign new references or clear the optional.

The important logic flows in this class include:

  • Constructors and assignment operators that allow creating and modifying the optional object. Assigning from T will write thru, modifying the T that is referred to. Assigning from another optional will change the target of the reference. Assigning std::nullopt will remove the reference target.
  • A boolean conversion operator that checks if the optional contains a valid reference.
  • Dereference operators (* and ->) that provide access to the referenced object, throwing an exception if the optional is empty. For example when calling the * operator, a check is done if the optional contains a valid reference. If it does, it returns the referenced object. If it doesn't, it throws an exception with the message "optional<T &>: access error".
  • The value and value_or members behave similarly except they return a copy of the stored value and in the case of value_or, the default is returned if the reference is not valid. This return by value is a bit idiosyncratic but they are provided for compatibility with the std::optional interface. Hopefully the function names serve as a reminder that the return is a value.

This implementation allows programmers to work with optional references in a way that's similar to how they would work with optional values. It provides a slightly safer alternative to using raw pointers or references when dealing with objects that may or may not be available at runtime. The safety improvement is only that:

  • A segfault is prevented if the optional is de-refed while empty, substituting an exception that can be caught and handled instead.
  • Makes the code a little more self documenting in that a raw pointer is versatile and therefore might be used for many reasons, whereas this class is very specifically for representing an optional reference.

Definition at line 80 of file optional_ref.hpp.

Member Typedef Documentation

◆ value_type

template<typename T >
using openvpn::optional< T >::value_type = std::remove_reference_t<T>
private

Definition at line 82 of file optional_ref.hpp.

Constructor & Destructor Documentation

◆ optional() [1/6]

template<typename T >
constexpr openvpn::optional< T >::optional ( )
constexprdefaultnoexcept

Default constructor. Creates an empty optional.

◆ optional() [2/6]

template<typename T >
requires std::is_reference_v<T>
constexpr openvpn::optional< T >::optional ( std::nullopt_t  noOption)
inlineconstexprnoexcept

Constructs an empty optional.

Parameters
noOptionIndicates that the optional should be empty.

Definition at line 205 of file optional_ref.hpp.

◆ optional() [3/6]

template<typename T >
requires std::is_reference_v<T>
constexpr openvpn::optional< T >::optional ( r)
inlineconstexprnoexcept

Constructs an optional containing a reference to the given object.

Parameters
rReference to the object to be stored.

Definition at line 212 of file optional_ref.hpp.

◆ optional() [4/6]

template<typename T >
requires std::is_reference_v<T>
constexpr openvpn::optional< T >::optional ( value_type r)
inlineconstexprnoexcept

Constructs an optional containing a reference to the given object.

Parameters
rpointer to the object to be stored.

Definition at line 219 of file optional_ref.hpp.

◆ optional() [5/6]

template<typename T >
constexpr openvpn::optional< T >::optional ( const optional< T > &  )
constexprdefaultnoexcept

Copy constructor.

◆ optional() [6/6]

template<typename T >
constexpr openvpn::optional< T >::optional ( optional< T > &&  )
constexprdefaultnoexcept

Move constructor.

Member Function Documentation

◆ has_value()

template<typename T >
requires std::is_reference_v<T>
constexpr bool openvpn::optional< T >::has_value ( ) const
inlineconstexprnoexcept

Checks whether the optional contains a value.

Returns
true if the optional contains a value, false otherwise.

Definition at line 241 of file optional_ref.hpp.

Here is the caller graph for this function:

◆ operator bool()

template<typename T >
requires std::is_reference_v<T>
constexpr openvpn::optional< T >::operator bool ( ) const
inlineexplicitconstexprnoexcept

Checks whether the optional contains a value.

Returns
true if the optional contains a value, false otherwise.

Definition at line 248 of file optional_ref.hpp.

◆ operator*() [1/2]

template<typename T >
requires std::is_reference_v<T>
T openvpn::optional< T >::operator* ( )
inline

Dereference operator.

Returns
Reference to the contained value.
Exceptions
std::runtime_errorif the optional is empty.

Definition at line 280 of file optional_ref.hpp.

◆ operator*() [2/2]

template<typename T >
requires std::is_reference_v<T>
const T openvpn::optional< T >::operator* ( ) const
inline

Dereference operator.

Returns
Const reference to the contained value.
Exceptions
std::runtime_errorif the optional is empty.

Definition at line 271 of file optional_ref.hpp.

◆ operator->() [1/2]

template<typename T >
requires std::is_reference_v<T>
auto openvpn::optional< T >::operator-> ( )
inline

Arrow operator.

Returns
Pointer to the contained value.
Exceptions
std::runtime_errorif the optional is empty.

Definition at line 296 of file optional_ref.hpp.

◆ operator->() [2/2]

template<typename T >
requires std::is_reference_v<T>
auto openvpn::optional< T >::operator-> ( ) const
inline

Arrow operator.

Returns
Const pointer to the contained value.
Exceptions
std::runtime_errorif the optional is empty.

Definition at line 287 of file optional_ref.hpp.

◆ operator=() [1/3]

template<typename T >
optional & openvpn::optional< T >::operator= ( const optional< T > &  )
defaultnoexcept

Copy assignment operator.

Returns
Reference to this object.

◆ operator=() [2/3]

template<typename T >
optional & openvpn::optional< T >::operator= ( optional< T > &&  )
defaultnoexcept

Move assignment operator.

Returns
Reference to this object.

◆ operator=() [3/3]

template<typename T >
requires std::is_reference_v<T>
optional< T > & openvpn::optional< T >::operator= ( std::nullopt_t  noOption)
inlinenoexcept

Assigns the optional to be empty.

Parameters
noOptionIndicates that the optional should be empty.
Returns
Reference to this object.

Definition at line 226 of file optional_ref.hpp.

◆ reset()

template<typename T >
requires std::is_reference_v<T>
void openvpn::optional< T >::reset ( )
inlinenoexcept

Resets the optional, making it empty.

Definition at line 234 of file optional_ref.hpp.

Here is the caller graph for this function:

◆ value()

template<typename T >
requires std::is_reference_v<T>
constexpr auto openvpn::optional< T >::value ( ) const
inlineconstexpr

Gets the contained value.

Returns
Copy of the contained value.
Exceptions
std::runtime_errorif the optional is empty.

Definition at line 255 of file optional_ref.hpp.

◆ value_or()

template<typename T >
requires std::is_reference_v<T>
constexpr auto openvpn::optional< T >::value_or ( const value_type default_value) const
inlineconstexprnoexcept

Returns the contained value if the optional is not empty, otherwise returns the provided default value.

Parameters
default_valueThe value to return if the optional is empty.
Returns
Copy of the contained value if the optional is not empty, otherwise a copy of the default value.

Definition at line 264 of file optional_ref.hpp.

Member Data Documentation

◆ errorMsg

template<typename T >
constexpr char openvpn::optional< T >::errorMsg[] = "optional<T &>: access error"
staticconstexprprivate

Definition at line 84 of file optional_ref.hpp.

◆ mRef

template<typename T >
value_type* openvpn::optional< T >::mRef = nullptr
private

Definition at line 83 of file optional_ref.hpp.


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