Line data Source code
1 : #pragma once 2 : 3 : #include <optional> 4 : 5 : namespace elsa 6 : { 7 : /// @brief A very tiny wrapper around std::optional to give it a little nicer name for this 8 : /// specific use case 9 : template <class T> 10 : class MaybeUninitialized 11 : { 12 : public: 13 8 : MaybeUninitialized() = default; 14 : 15 : /// Uninitialized can be constructed using T 16 10 : explicit MaybeUninitialized(const T& x) : value_(x) {} 17 2 : explicit MaybeUninitialized(T&& x) : value_(std::move(x)) {} 18 : 19 : /// Uninitialized can be assigned using T 20 : MaybeUninitialized& operator=(const T& x) 21 : { 22 : value_ = x; 23 : return *this; 24 : } 25 : 26 : MaybeUninitialized& operator=(T&& x) 27 10 : { 28 10 : value_ = std::move(x); 29 10 : return *this; 30 10 : } 31 : 32 74 : bool isInitialized() const { return value_.has_value(); } 33 : 34 14 : T& operator*() { return *value_; } 35 20 : const T& operator*() const { return *value_; } 36 : 37 : private: 38 : std::optional<T> value_ = std::nullopt; 39 : }; 40 : 41 : template <class T> 42 : bool operator==(const MaybeUninitialized<T>& lhs, const MaybeUninitialized<T>& rhs) 43 20 : { 44 20 : if (!lhs.isInitialized() && !rhs.isInitialized()) { 45 4 : return true; 46 16 : } else if (lhs.isInitialized() && rhs.isInitialized()) { 47 8 : return *lhs == *rhs; 48 8 : } else { 49 8 : return false; 50 8 : } 51 20 : } 52 : 53 : template <class T> 54 : bool operator!=(const MaybeUninitialized<T>& lhs, const MaybeUninitialized<T>& rhs) 55 8 : { 56 8 : return !(lhs == rhs); 57 8 : } 58 : } // namespace elsa