| 122 | // See issue #3330 for more details. |
| 123 | template <typename T> |
| 124 | class ReferenceSensitiveOptional { |
| 125 | public: |
| 126 | using value_type = T; |
| 127 | |
| 128 | ReferenceSensitiveOptional() = default; |
| 129 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 130 | ReferenceSensitiveOptional(const T &value) : storage{value} {} |
| 131 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 132 | ReferenceSensitiveOptional(T &&value) : storage{std::move(value)} {} |
| 133 | ReferenceSensitiveOptional &operator=(const T &value) { |
| 134 | storage = {value}; |
| 135 | return *this; |
| 136 | } |
| 137 | ReferenceSensitiveOptional &operator=(T &&value) { |
| 138 | storage = {std::move(value)}; |
| 139 | return *this; |
| 140 | } |
| 141 | |
| 142 | template <typename... Args> |
| 143 | T &emplace(Args &&...args) { |
| 144 | storage.clear(); |
| 145 | storage.emplace_back(std::forward<Args>(args)...); |
| 146 | return storage.back(); |
| 147 | } |
| 148 | |
| 149 | const T &value() const noexcept { |
| 150 | assert(!storage.empty()); |
| 151 | return storage[0]; |
| 152 | } |
| 153 | |
| 154 | const T &operator*() const noexcept { return value(); } |
| 155 | |
| 156 | const T *operator->() const noexcept { return &value(); } |
| 157 | |
| 158 | explicit operator bool() const noexcept { return !storage.empty(); } |
| 159 | |
| 160 | private: |
| 161 | std::vector<T> storage; |
| 162 | }; |
| 163 | |
| 164 | namespace PYBIND11_NAMESPACE { |
| 165 | namespace detail { |
nothing calls this directly
no test coverage detected