#ifndef CORE_FXCRT_MAYBE_OWNED_H_
#define CORE_FXCRT_MAYBE_OWNED_H_
#include <memory>
#include <utility>
#include "core/fxcrt/unowned_ptr.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
namespace fxcrt {
template <typename T, typename D = std::default_delete<T>>
class MaybeOwned {
public:
using OwnedType = std::unique_ptr<T, D>;
using UnownedType = UnownedPtr<T>;
MaybeOwned() = default;
explicit MaybeOwned(T* ptr) : … { … }
explicit MaybeOwned(const UnownedType& ptr) : … { … }
explicit MaybeOwned(OwnedType ptr) : … { … }
MaybeOwned(const MaybeOwned& that) = delete;
MaybeOwned(MaybeOwned&& that) noexcept = default;
MaybeOwned& operator=(const MaybeOwned& that) = delete;
MaybeOwned& operator=(MaybeOwned&& that) noexcept = default;
~MaybeOwned() = default;
void Reset(T* ptr = nullptr) { … }
void Reset(OwnedType ptr) { … }
bool IsOwned() const { … }
void ResetIfUnowned() { … }
T* Get() const& { … }
T* Get() && { … }
OwnedType Release() { … }
OwnedType ReleaseAndClear() { … }
MaybeOwned& operator=(T* ptr) { … }
MaybeOwned& operator=(const UnownedType& ptr) { … }
MaybeOwned& operator=(OwnedType ptr) { … }
bool operator==(const MaybeOwned& that) const { … }
bool operator==(const OwnedType& ptr) const { … }
bool operator==(T* ptr) const { … }
bool operator!=(const MaybeOwned& that) const { … }
bool operator!=(const OwnedType ptr) const { … }
bool operator!=(T* ptr) const { … }
explicit operator bool() const { return !!Get(); }
T& operator*() const { … }
T* operator->() const { … }
private:
absl::variant<UnownedType, OwnedType> ptr_;
};
}
MaybeOwned;
#endif