|
| 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.
|
| |
| optional & | operator= (const optional &) noexcept=default |
| | Copy assignment operator.
|
| |
| optional & | operator= (optional &&) noexcept=default |
| | Move assignment operator.
|
| |
| optional & | operator= (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.
|
| |
| T | operator* () |
| | Dereference operator.
|
| |
| const value_type * | operator-> () const |
| | Arrow operator.
|
| |
| value_type * | operator-> () |
| | Arrow operator.
|
| |
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
-
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.