19#ifndef MTCORE_OPTIONAL_HPP
20#define MTCORE_OPTIONAL_HPP
68 return other.v ==
nullptr;
70 if (other.v ==
nullptr) {
74 ensure(v && other.v,
"EXISTS CHECK FAILED");
81 [[nodiscard]]
bool has_value() const noexcept {
return v !=
nullptr; }
87 [[nodiscard]] T
value_or(
const T &def)
const noexcept {
94 [[nodiscard]] T &
value() noexcept {
99 [[nodiscard]]
const T &
value() const noexcept {
237 std::variant<std::monostate, T> v;
267 return std::get<T>(v) == std::get<T>(other.v);
277 return std::get<T>(v) == other;
284 [[nodiscard]] T
value_or(
const T &def)
const noexcept {
294 [[nodiscard]]
bool has_value() const noexcept {
return std::holds_alternative<T>(v); }
299 [[nodiscard]]
bool empty() const noexcept {
return std::holds_alternative<std::monostate>(v); }
303 return std::get<T>(v);
306 [[nodiscard]]
const T &
value() const noexcept {
308 return std::get<T>(v);
329 out = std::get<T>(v);
354 out = std::move(std::get<T>(v));
355 v = std::monostate{};
366 return std::get<T>(v);
374 return &std::get<T>(v);
382 return std::get<T>(v);
390 return &std::get<T>(v);
constexpr auto nullopt
Placeholder value for an empty Optional.
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
#define mtdefer
Defer statement that will mtdefer execution until the scope is left, at which point the code will run...
Core library for C++ with Zig-related functionality.
Placeholder value for any empty Optional (similar to std::nullopt)
const T & value() const noexcept
bool copy_if_present(T *&out) const noexcept
Copies a value to a reference output destination if a value is present Designed to be used by simple ...
const T * operator->() const
Dereferences the internal value.
T value_or(const T &def) const noexcept
Returns the value in the optional, or a default value if the optional is empty.
Optional()
Creates an empty Optional.
bool has_value() const noexcept
bool operator==(const Optional &other) const noexcept
Checks if value is equal to another Optional.
Optional(T *value)
Creates an Optional from a value.
T * operator->()
Dereferences the internal value.
const T & operator*() const
Dereferences the internal value.
bool move_if_present(std::remove_const_t< T > *&out) noexcept
Moves a value to a reference output destination if a value is present.
T & operator*()
Dereferences the internal value.
bool copy_if_present(std::remove_const_t< T > &out) const noexcept
Copies a value to a reference output destination if a value is present Designed to be used by simple ...
bool move_if_present(std::remove_const_t< T > &out) noexcept
Moves a value to a reference output destination if a value is present.
Represents a value that may or may not exist (an "Optional" value) Similar concept to std::optional,...
bool operator==(const Optional &other) const noexcept
Checks if value is equal to another Optional.
Optional(const T &value)
Creates an Optional from a value.
const T * operator->() const
Dereferences the internal value.
bool has_value() const noexcept
T * operator->()
Dereferences the internal value.
Optional(T &&value)
Creates an Optional from a value.
bool operator==(const T &other) const noexcept
Checks if value is equal to a non-Optional value.
const T & value() const noexcept
const T & operator*() const
Dereferences the internal value.
Optional()
Creates an empty Optional.
T & operator*()
Dereferences the internal value.
T value_or(const T &def) const noexcept
Returns the value in the optional, or a default value if the optional is empty.
bool empty() const noexcept
bool move_if_present(T &out) noexcept
Moves a value to a reference output destination if a value is present.
bool copy_if_present(std::remove_const_t< T > &out) const noexcept
Copies a value to a reference output destination if a value is present Designed to be used by simple ...