2018-02-02 05:31:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
#include <optional>
|
2018-02-02 05:31:56 +00:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
// Like std::optional, but the stored data is responsible for containing the
|
|
|
|
// empty state. T should define a function `bool T::Valid()`.
|
|
|
|
template <typename T> class Maybe {
|
2018-02-02 05:31:56 +00:00
|
|
|
T storage;
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
public:
|
2018-02-02 05:31:56 +00:00
|
|
|
constexpr Maybe() = default;
|
2018-08-09 17:08:14 +00:00
|
|
|
Maybe(const Maybe &) = default;
|
2018-02-02 07:10:37 +00:00
|
|
|
Maybe(std::nullopt_t) {}
|
2018-08-09 17:08:14 +00:00
|
|
|
Maybe(const T &x) : storage(x) {}
|
|
|
|
Maybe(T &&x) : storage(std::forward<T>(x)) {}
|
2018-02-02 05:31:56 +00:00
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
Maybe &operator=(const Maybe &) = default;
|
|
|
|
Maybe &operator=(const T &x) {
|
2018-02-02 05:31:56 +00:00
|
|
|
storage = x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
const T *operator->() const { return &storage; }
|
|
|
|
T *operator->() { return &storage; }
|
|
|
|
const T &operator*() const { return storage; }
|
|
|
|
T &operator*() { return storage; }
|
2018-02-02 05:31:56 +00:00
|
|
|
|
2018-04-08 06:32:35 +00:00
|
|
|
bool Valid() const { return storage.Valid(); }
|
|
|
|
explicit operator bool() const { return Valid(); }
|
2018-03-31 03:16:33 +00:00
|
|
|
operator std::optional<T>() const {
|
2018-04-08 06:32:35 +00:00
|
|
|
if (Valid())
|
2018-02-02 05:31:56 +00:00
|
|
|
return storage;
|
2018-03-31 03:16:33 +00:00
|
|
|
return std::nullopt;
|
2018-02-02 05:31:56 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
void operator=(std::optional<T> &&o) { storage = o ? *o : T(); }
|
2018-02-02 05:31:56 +00:00
|
|
|
|
|
|
|
// Does not test if has_value()
|
2018-08-09 17:08:14 +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
|
|
|
};
|