2018-02-02 05:31:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <optional.h>
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Maybe {
|
|
|
|
T storage;
|
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr Maybe() = default;
|
|
|
|
Maybe(const Maybe&) = default;
|
2018-02-02 07:10:37 +00:00
|
|
|
Maybe(std::nullopt_t) {}
|
2018-02-02 05:31:56 +00:00
|
|
|
Maybe(const T& x) : storage(x) {}
|
|
|
|
Maybe(T&& x) : storage(std::forward<T>(x)) {}
|
|
|
|
|
|
|
|
Maybe& operator=(const Maybe&) = default;
|
|
|
|
Maybe& operator=(const T& x) {
|
|
|
|
storage = x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T *operator->() const { return &storage; }
|
|
|
|
T *operator->() { return &storage; }
|
|
|
|
const T& operator*() const { return storage; }
|
|
|
|
T& operator*() { return storage; }
|
|
|
|
|
2018-02-02 06:31:43 +00:00
|
|
|
bool has_value() const {
|
|
|
|
return storage.HasValue();
|
|
|
|
}
|
2018-02-02 05:31:56 +00:00
|
|
|
explicit operator bool() const { return has_value(); }
|
|
|
|
operator optional<T>() const {
|
|
|
|
if (has_value())
|
|
|
|
return storage;
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator=(optional<T>&& o) {
|
|
|
|
storage = o ? *o : T();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does not test if has_value()
|
|
|
|
bool operator==(const Maybe& o) const {
|
|
|
|
return storage == o.storage;
|
|
|
|
}
|
|
|
|
};
|