ccls/src/maybe.h

59 lines
1.8 KiB
C
Raw Normal View History

2018-08-21 05:27:52 +00:00
/* Copyright 2017-2018 ccls Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#pragma once
2018-03-31 03:16:33 +00:00
#include <optional>
#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 {
T storage;
2018-08-09 17:08:14 +00:00
public:
constexpr Maybe() = default;
2018-08-09 17:08:14 +00:00
Maybe(const Maybe &) = default;
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-08-09 17:08:14 +00:00
Maybe &operator=(const Maybe &) = default;
Maybe &operator=(const T &x) {
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; }
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 {
if (Valid())
return storage;
2018-03-31 03:16:33 +00:00
return std::nullopt;
}
2018-08-09 17:08:14 +00:00
void operator=(std::optional<T> &&o) { storage = o ? *o : T(); }
// 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); }
};