|
| 1 | +/*! |
| 2 | + * \file MFEMMGIS/Utilities/OptionalReference.hxx |
| 3 | + * \brief This header declares the OptionalReference class |
| 4 | + * \author Thomas Helfer |
| 5 | + * \date 19/02/2026 |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef LIB_MGIS_UTILITIES_OPTIONALREFERENCE_HXX |
| 9 | +#define LIB_MGIS_UTILITIES_OPTIONALREFERENCE_HXX |
| 10 | + |
| 11 | +#include <memory> |
| 12 | +#include <cstddef> |
| 13 | +#include <utility> |
| 14 | +#include <cassert> |
| 15 | +#include <type_traits> |
| 16 | +#include "MGIS/Config.hxx" |
| 17 | +#include "MGIS/InvalidResult.hxx" |
| 18 | + |
| 19 | +namespace mgis { |
| 20 | + |
| 21 | + /*! |
| 22 | + * \brief a class that may contain a reference to a class |
| 23 | + * |
| 24 | + * \note this class was introduced by of the lack of optional |
| 25 | + * references befor to C++-26. |
| 26 | + * \note the design of the class is loosely modeled by the |
| 27 | + * std::experimental::observer_ptr proposal |
| 28 | + */ |
| 29 | + template <typename ValueType> |
| 30 | + struct [[nodiscard]] OptionalReference { |
| 31 | + // |
| 32 | + using element_type = ValueType; |
| 33 | + using pointer = ValueType*; |
| 34 | + using reference = ValueType&; |
| 35 | + // |
| 36 | + constexpr OptionalReference() noexcept : ptr(nullptr) {} |
| 37 | + |
| 38 | + constexpr OptionalReference(std::nullptr_t) noexcept : ptr(nullptr) {} |
| 39 | + |
| 40 | + constexpr OptionalReference(pointer p) noexcept : ptr(p) {} |
| 41 | + |
| 42 | + template <typename ValueType2> |
| 43 | + requires(std::is_convertible<ValueType2*, ValueType*>::value) // |
| 44 | + constexpr OptionalReference( |
| 45 | + OptionalReference<ValueType2> other) noexcept |
| 46 | + : ptr(other.get()) {} |
| 47 | + |
| 48 | + template <typename ValueType2> |
| 49 | + requires(std::is_convertible<ValueType2*, ValueType*>::value) // |
| 50 | + constexpr OptionalReference( |
| 51 | + std::unique_ptr<ValueType2> const& other) noexcept |
| 52 | + : ptr(other.get()) {} |
| 53 | + |
| 54 | + template <typename ValueType2> |
| 55 | + requires(std::is_convertible<ValueType2*, ValueType*>::value) // |
| 56 | + constexpr OptionalReference( |
| 57 | + std::shared_ptr<ValueType2> const& other) noexcept |
| 58 | + : ptr(other.get()) {} |
| 59 | + |
| 60 | + [[nodiscard]] constexpr pointer get() const noexcept { return this->ptr; } |
| 61 | + |
| 62 | + [[nodiscard]] constexpr reference operator*() const { |
| 63 | + assert(ptr != nullptr); |
| 64 | + return *ptr; |
| 65 | + } |
| 66 | + |
| 67 | + constexpr pointer operator->() const noexcept { return this->ptr; } |
| 68 | + |
| 69 | + [[nodiscard]] constexpr operator bool() const noexcept { |
| 70 | + return this->ptr != nullptr; |
| 71 | + } |
| 72 | + |
| 73 | + constexpr operator pointer() const noexcept { return this->ptr; } |
| 74 | + |
| 75 | + constexpr pointer release() noexcept { |
| 76 | + pointer p(ptr); |
| 77 | + this->reset(); |
| 78 | + return p; |
| 79 | + } |
| 80 | + |
| 81 | + constexpr void reset(pointer p = nullptr) noexcept { this->ptr = p; } |
| 82 | + |
| 83 | + constexpr void swap(OptionalReference& other) noexcept { |
| 84 | + std::swap(ptr, other.ptr); |
| 85 | + } |
| 86 | + |
| 87 | + private: |
| 88 | + pointer ptr; |
| 89 | + }; |
| 90 | + |
| 91 | + template <typename ValueType> |
| 92 | + void swap(OptionalReference<ValueType>& p1, |
| 93 | + OptionalReference<ValueType>& p2) noexcept { |
| 94 | + p1.swap(p2); |
| 95 | + } |
| 96 | + |
| 97 | + template <typename ValueType> |
| 98 | + [[nodiscard]] constexpr OptionalReference<ValueType> make_optional_reference( |
| 99 | + ValueType& p) noexcept { |
| 100 | + return OptionalReference<ValueType>(&p); |
| 101 | + } |
| 102 | + |
| 103 | + template <typename ValueType> |
| 104 | + [[nodiscard]] constexpr OptionalReference<ValueType> make_optional_reference( |
| 105 | + ValueType* p) noexcept { |
| 106 | + return OptionalReference<ValueType>(p); |
| 107 | + } |
| 108 | + |
| 109 | + template <typename ValueType1, typename ValueType2> |
| 110 | + [[nodiscard]] constexpr bool operator==(OptionalReference<ValueType1> p1, |
| 111 | + OptionalReference<ValueType2> p2) { |
| 112 | + return p1.get() == p2.get(); |
| 113 | + } |
| 114 | + |
| 115 | + template <typename ValueType1, typename ValueType2> |
| 116 | + [[nodiscard]] constexpr bool operator!=(OptionalReference<ValueType1> p1, |
| 117 | + OptionalReference<ValueType2> p2) { |
| 118 | + return !(p1 == p2); |
| 119 | + } |
| 120 | + |
| 121 | + template <typename ValueType> |
| 122 | + [[nodiscard]] constexpr bool operator==(OptionalReference<ValueType> p, |
| 123 | + std::nullptr_t) noexcept { |
| 124 | + return !p; |
| 125 | + } |
| 126 | + |
| 127 | + template <typename ValueType> |
| 128 | + [[nodiscard]] constexpr bool operator==( |
| 129 | + std::nullptr_t, OptionalReference<ValueType> p) noexcept { |
| 130 | + return !p; |
| 131 | + } |
| 132 | + |
| 133 | + template <typename ValueType> |
| 134 | + [[nodiscard]] constexpr bool operator!=(OptionalReference<ValueType> p, |
| 135 | + std::nullptr_t) noexcept { |
| 136 | + return static_cast<bool>(p); |
| 137 | + } |
| 138 | + |
| 139 | + template <typename ValueType> |
| 140 | + [[nodiscard]] constexpr bool operator!=( |
| 141 | + std::nullptr_t, OptionalReference<ValueType> p) noexcept { |
| 142 | + return static_cast<bool>(p); |
| 143 | + } |
| 144 | + |
| 145 | + template <typename ValueType> |
| 146 | + [[nodiscard]] constexpr bool isInvalid( |
| 147 | + const OptionalReference<ValueType>& p) noexcept { |
| 148 | + return p == nullptr; |
| 149 | + } // end of isInvalid |
| 150 | + |
| 151 | +} // end of namespace mgis |
| 152 | + |
| 153 | +namespace std { |
| 154 | + |
| 155 | + template <typename ValueType> |
| 156 | + struct hash<::mgis::OptionalReference<ValueType>> { |
| 157 | + [[nodiscard]] size_t operator()( |
| 158 | + ::mgis::OptionalReference<ValueType> p) const noexcept { |
| 159 | + return hash<ValueType*>()(p.get()); |
| 160 | + } |
| 161 | + }; |
| 162 | + |
| 163 | +} // namespace std |
| 164 | + |
| 165 | +namespace mgis::internal { |
| 166 | + |
| 167 | + //! \brief partial specialisation for optional references |
| 168 | + template <typename ValueType> |
| 169 | + struct InvalidValueTraits<mgis::OptionalReference<ValueType>> { |
| 170 | + static constexpr bool isSpecialized = true; |
| 171 | + static mgis::OptionalReference<ValueType> getValue() noexcept { |
| 172 | + return {}; |
| 173 | + } |
| 174 | + }; |
| 175 | + |
| 176 | +} // end of namespace mgis::internal |
| 177 | + |
| 178 | +#endif /* LIB_MGIS_UTILITIES_OPTIONALREFERENCE_HXX */ |
0 commit comments