1#ifndef SOURCEMETA_CORE_MEMORY_OWNED_OR_REFERENCE_H_
2#define SOURCEMETA_CORE_MEMORY_OWNED_OR_REFERENCE_H_
11namespace sourcemeta::core {
16concept Ownable = std::is_object_v<T> && std::move_constructible<T>;
85 [[nodiscard]]
auto has_value() const noexcept ->
bool {
86 return this->referenced_ !=
nullptr || this->owned_.has_value();
90 [[nodiscard]]
auto value() const -> const T & {
92 return this->referenced_ !=
nullptr ? *this->referenced_
93 : this->owned_.value();
97 [[nodiscard]]
auto operator*() const -> const T & {
return this->
value(); }
100 [[nodiscard]]
auto operator->() const -> const T * {
101 return std::addressof(this->
value());
107 requires std::copy_constructible<T>
110 if (this->referenced_ !=
nullptr) {
111 return *this->referenced_;
114 return std::move(this->owned_).value();
118 std::optional<T> owned_;
119 const T *referenced_{
nullptr};
auto has_value() const noexcept -> bool
Whether there is anything to read.
Definition memory_owned_or_reference.h:85
auto value() const -> const T &
Definition memory_owned_or_reference.h:90
OwnedOrReference(std::optional< T > &&value)
Take ownership of a value that may or may not be there.
Definition memory_owned_or_reference.h:59
OwnedOrReference(T &&value)
Take ownership of a value.
Definition memory_owned_or_reference.h:63
OwnedOrReference(OwnedOrReference &&)=default
Move.
OwnedOrReference(const std::nullopt_t value)
Hold nothing.
Definition memory_owned_or_reference.h:55
auto to_owned() &&-> T
Definition memory_owned_or_reference.h:106
OwnedOrReference()=default
Hold nothing.
OwnedOrReference(const T &value)
Definition memory_owned_or_reference.h:68