mirror of
https://github.com/MaskRay/ccls.git
synced 2024-11-22 07:35:08 +00:00
Merge maybe.hh into utils.hh
This commit is contained in:
parent
880c515d64
commit
65d856cf8a
@ -4,7 +4,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "lsp.hh"
|
||||
#include "maybe.hh"
|
||||
#include "position.hh"
|
||||
#include "serializer.hh"
|
||||
#include "utils.hh"
|
||||
|
48
src/maybe.hh
48
src/maybe.hh
@ -1,48 +0,0 @@
|
||||
// Copyright 2017-2018 ccls Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#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
|
@ -3,7 +3,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "maybe.hh"
|
||||
#include "utils.hh"
|
||||
|
||||
#include <stdint.h>
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "maybe.hh"
|
||||
#include "utils.hh"
|
||||
|
||||
#include <llvm/Support/Compiler.h>
|
||||
|
||||
|
39
src/utils.hh
39
src/utils.hh
@ -9,6 +9,7 @@
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace llvm {
|
||||
@ -87,4 +88,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
|
||||
|
Loading…
Reference in New Issue
Block a user