Merge maybe.hh into utils.hh

This commit is contained in:
Fangrui Song 2018-11-26 23:07:28 -08:00
parent 5a5165faa8
commit 72ee893d26
5 changed files with 40 additions and 63 deletions

View File

@ -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"

View File

@ -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 <optional>
#include <utility>
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 <typename T> 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<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; }
bool Valid() const { return storage.Valid(); }
explicit operator bool() const { return Valid(); }
operator std::optional<T>() const {
if (Valid())
return storage;
return std::nullopt;
}
void operator=(std::optional<T> &&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

View File

@ -15,7 +15,6 @@ limitations under the License.
#pragma once
#include "maybe.hh"
#include "utils.hh"
#include <stdint.h>

View File

@ -15,7 +15,7 @@ limitations under the License.
#pragma once
#include "maybe.hh"
#include "utils.hh"
#include <llvm/Support/Compiler.h>

View File

@ -21,6 +21,7 @@ limitations under the License.
#include <iterator>
#include <memory>
#include <string>
#include <utility>
#include <vector>
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 <typename T> 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<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; }
bool Valid() const { return storage.Valid(); }
explicit operator bool() const { return Valid(); }
operator std::optional<T>() const {
if (Valid())
return storage;
return std::nullopt;
}
void operator=(std::optional<T> &&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