diff --git a/src/indexer.hh b/src/indexer.hh index 55bc1ffb..b0ea2de1 100644 --- a/src/indexer.hh +++ b/src/indexer.hh @@ -16,7 +16,6 @@ limitations under the License. #pragma once #include "lsp.hh" -#include "maybe.hh" #include "position.hh" #include "serializer.hh" #include "utils.hh" diff --git a/src/maybe.hh b/src/maybe.hh deleted file mode 100644 index e62f2792..00000000 --- a/src/maybe.hh +++ /dev/null @@ -1,60 +0,0 @@ -/* 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 - -#include - -#include - -namespace ccls { -// Like std::optional, but the stored data is responsible for containing the -// empty state. T should define a function `bool T::Valid()`. -template class Maybe { - T storage; - -public: - constexpr Maybe() = default; - Maybe(const Maybe &) = default; - Maybe(std::nullopt_t) {} - Maybe(const T &x) : storage(x) {} - Maybe(T &&x) : storage(std::forward(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; } - - bool Valid() const { return storage.Valid(); } - explicit operator bool() const { return Valid(); } - operator std::optional() const { - if (Valid()) - return storage; - return std::nullopt; - } - - void operator=(std::optional &&o) { storage = o ? *o : T(); } - - // Does not test if has_value() - bool operator==(const Maybe &o) const { return storage == o.storage; } - bool operator!=(const Maybe &o) const { return !(*this == o); } -}; -} // namespace ccls diff --git a/src/position.hh b/src/position.hh index e595e1ed..11075e90 100644 --- a/src/position.hh +++ b/src/position.hh @@ -15,7 +15,6 @@ limitations under the License. #pragma once -#include "maybe.hh" #include "utils.hh" #include diff --git a/src/serializer.hh b/src/serializer.hh index b57dbfae..dc469d30 100644 --- a/src/serializer.hh +++ b/src/serializer.hh @@ -15,7 +15,7 @@ limitations under the License. #pragma once -#include "maybe.hh" +#include "utils.hh" #include diff --git a/src/utils.hh b/src/utils.hh index d6d07966..fcda9d8e 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -21,6 +21,7 @@ limitations under the License. #include #include #include +#include #include namespace llvm { @@ -99,4 +100,42 @@ inline void hash_combine(std::size_t &seed, const T &v, Rest... rest) { } std::string GetDefaultResourceDirectory(); + +// Like std::optional, but the stored data is responsible for containing the +// empty state. T should define a function `bool T::Valid()`. +template class Maybe { + T storage; + +public: + constexpr Maybe() = default; + Maybe(const Maybe &) = default; + Maybe(std::nullopt_t) {} + Maybe(const T &x) : storage(x) {} + Maybe(T &&x) : storage(std::forward(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; } + + bool Valid() const { return storage.Valid(); } + explicit operator bool() const { return Valid(); } + operator std::optional() const { + if (Valid()) + return storage; + return std::nullopt; + } + + void operator=(std::optional &&o) { storage = o ? *o : T(); } + + // Does not test if has_value() + bool operator==(const Maybe &o) const { return storage == o.storage; } + bool operator!=(const Maybe &o) const { return !(*this == o); } +}; } // namespace ccls