|
| 1 | +/* |
| 2 | + RawSpeed - RAW file decoder. |
| 3 | +
|
| 4 | + Copyright (C) 2024 Roman Lebedev |
| 5 | +
|
| 6 | + This library is free software; you can redistribute it and/or |
| 7 | + modify it under the terms of the GNU Lesser General Public |
| 8 | + License as published by the Free Software Foundation; either |
| 9 | + version 2 of the License, or (at your option) any later version. |
| 10 | +
|
| 11 | + This library is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + Lesser General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU Lesser General Public |
| 17 | + License along with this library; if not, write to the Free Software |
| 18 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#pragma once |
| 22 | + |
| 23 | +#include "rawspeedconfig.h" |
| 24 | +#include "adt/Invariant.h" |
| 25 | +#include <optional> // IWYU pragma: export |
| 26 | + |
| 27 | +namespace rawspeed { |
| 28 | + |
| 29 | +template <typename T> class Optional final { |
| 30 | + std::optional<T> impl; |
| 31 | + |
| 32 | +public: |
| 33 | + Optional() = default; |
| 34 | + |
| 35 | + template <typename U = T> |
| 36 | + requires(!std::same_as<U, Optional<T>> && !std::same_as<U, Optional<T>&> && |
| 37 | + !std::same_as<U, Optional<T> &&> && |
| 38 | + !std::same_as<U, std::optional<T>>) |
| 39 | + // NOLINTNEXTLINE(google-explicit-constructor) |
| 40 | + Optional(U&& value) : impl(std::forward<U>(value)) {} |
| 41 | + |
| 42 | + template <typename U = T> |
| 43 | + requires(std::same_as<U, T>) |
| 44 | + Optional<T>& operator=(U&& value) { |
| 45 | + impl = std::forward<U>(value); |
| 46 | + return *this; |
| 47 | + } |
| 48 | + |
| 49 | + template <typename... Args> T& emplace(Args&&... args) { |
| 50 | + return impl.emplace(std::forward<Args>(args)...); |
| 51 | + } |
| 52 | + |
| 53 | + const T* operator->() const { |
| 54 | + invariant(has_value()); |
| 55 | + return &impl.value(); |
| 56 | + } |
| 57 | + |
| 58 | + T* operator->() { |
| 59 | + invariant(has_value()); |
| 60 | + return &impl.value(); |
| 61 | + } |
| 62 | + |
| 63 | + const T& operator*() const& { |
| 64 | + invariant(has_value()); |
| 65 | + return impl.value(); |
| 66 | + } |
| 67 | + |
| 68 | + T& operator*() & { |
| 69 | + invariant(has_value()); |
| 70 | + return impl.value(); |
| 71 | + } |
| 72 | + |
| 73 | + T&& operator*() && { |
| 74 | + invariant(has_value()); |
| 75 | + return std::move(impl.value()); |
| 76 | + } |
| 77 | + |
| 78 | + const T&& operator*() const&& { |
| 79 | + invariant(has_value()); |
| 80 | + return std::move(impl.value()); |
| 81 | + } |
| 82 | + |
| 83 | + [[nodiscard]] bool has_value() const RAWSPEED_READNONE { |
| 84 | + return impl.has_value(); |
| 85 | + } |
| 86 | + |
| 87 | + explicit operator bool() const { return has_value(); } |
| 88 | + |
| 89 | + void reset() { impl.reset(); } |
| 90 | +}; |
| 91 | + |
| 92 | +template <typename T, typename U> |
| 93 | +bool operator==(const Optional<T>& lhs, const U& rhs) { |
| 94 | + return lhs && *lhs == rhs; |
| 95 | +} |
| 96 | + |
| 97 | +template <typename T, typename U> |
| 98 | +bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) { |
| 99 | + if (lhs.has_value() != rhs.has_value()) |
| 100 | + return false; |
| 101 | + if (!lhs.has_value()) |
| 102 | + return true; |
| 103 | + return *lhs == *rhs; |
| 104 | +} |
| 105 | + |
| 106 | +} // namespace rawspeed |
0 commit comments