2018-02-02 05:31:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <optional.h>
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
2018-02-11 21:49:44 +00:00
|
|
|
// Like optional, but the stored data is responsible for containing the empty
|
|
|
|
// state. T should define a function `bool T::HasValueForMaybe_()`.
|
2018-02-02 05:31:56 +00:00
|
|
|
template <typename T>
|
|
|
|
class Maybe {
|
|
|
|
T storage;
|
|
|
|
|
2018-02-22 07:34:32 +00:00
|
|
|
public:
|
2018-02-02 05:31:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-22 07:34:32 +00:00
|
|
|
const T* operator->() const { return &storage; }
|
|
|
|
T* operator->() { return &storage; }
|
2018-02-02 05:31:56 +00:00
|
|
|
const T& operator*() const { return storage; }
|
|
|
|
T& operator*() { return storage; }
|
|
|
|
|
2018-02-22 07:34:32 +00:00
|
|
|
bool HasValue() const { return storage.HasValueForMaybe_(); }
|
2018-02-11 21:56:34 +00:00
|
|
|
explicit operator bool() const { return HasValue(); }
|
2018-02-02 05:31:56 +00:00
|
|
|
operator optional<T>() const {
|
2018-02-11 21:56:34 +00:00
|
|
|
if (HasValue())
|
2018-02-02 05:31:56 +00:00
|
|
|
return storage;
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
|
2018-02-22 07:34:32 +00:00
|
|
|
void operator=(optional<T>&& o) { storage = o ? *o : T(); }
|
2018-02-02 05:31:56 +00:00
|
|
|
|
|
|
|
// Does not test if has_value()
|
2018-02-22 07:34:32 +00:00
|
|
|
bool operator==(const Maybe& o) const { return storage == o.storage; }
|
|
|
|
bool operator!=(const Maybe& o) const { return !(*this == o); }
|
2018-02-02 05:31:56 +00:00
|
|
|
};
|