mirror of
https://github.com/MaskRay/ccls.git
synced 2025-01-31 18:00:26 +00:00
Reflect std::variant and change lsRequestId to variant<monostate,double,string> (#279)
This commit is contained in:
parent
a99fd7c444
commit
503127e0da
@ -5,22 +5,17 @@
|
|||||||
#include <doctest/doctest.h>
|
#include <doctest/doctest.h>
|
||||||
#include <loguru.hpp>
|
#include <loguru.hpp>
|
||||||
|
|
||||||
void Reflect(Writer& visitor, lsRequestId& value) {
|
|
||||||
assert(value.id0.has_value() || value.id1.has_value());
|
|
||||||
|
|
||||||
if (value.id0) {
|
|
||||||
Reflect(visitor, value.id0.value());
|
|
||||||
} else {
|
|
||||||
Reflect(visitor, value.id1.value());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Reflect(Reader& visitor, lsRequestId& id) {
|
void Reflect(Reader& visitor, lsRequestId& id) {
|
||||||
if (visitor.IsInt())
|
if (visitor.IsInt()) {
|
||||||
Reflect(visitor, id.id0);
|
int v;
|
||||||
else if (visitor.IsString())
|
Reflect(visitor, v);
|
||||||
Reflect(visitor, id.id1);
|
id = v;
|
||||||
else
|
}
|
||||||
|
else if (visitor.IsString()) {
|
||||||
|
std::string v;
|
||||||
|
Reflect(visitor, v);
|
||||||
|
id = v;
|
||||||
|
} else
|
||||||
LOG_S(WARNING) << "Unable to deserialize id";
|
LOG_S(WARNING) << "Unable to deserialize id";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,11 +24,7 @@
|
|||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
struct lsRequestId {
|
using lsRequestId = std::variant<std::monostate, double, std::string>;
|
||||||
optional<int> id0;
|
|
||||||
optional<std::string> id1;
|
|
||||||
};
|
|
||||||
void Reflect(Writer& visitor, lsRequestId& value);
|
|
||||||
void Reflect(Reader& visitor, lsRequestId& id);
|
void Reflect(Reader& visitor, lsRequestId& id);
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -209,7 +209,6 @@ void EmitSemanticHighlighting(QueryDatabase* db,
|
|||||||
std::vector<uint8_t> deleted(id, 0);
|
std::vector<uint8_t> deleted(id, 0);
|
||||||
int top = 0;
|
int top = 0;
|
||||||
for (size_t i = 0; i < events.size(); i++) {
|
for (size_t i = 0; i < events.size(); i++) {
|
||||||
// |start| is used as liveness tag here.
|
|
||||||
while (top && deleted[events[top - 1].id])
|
while (top && deleted[events[top - 1].id])
|
||||||
top--;
|
top--;
|
||||||
// Order [a, b0) after [a, b1) if b0 < b1. The range comes later overrides
|
// Order [a, b0) after [a, b1) if b0 < b1. The range comes later overrides
|
||||||
|
@ -10,16 +10,14 @@ REGISTER_IPC_MESSAGE(Ipc_Shutdown);
|
|||||||
|
|
||||||
struct Out_Shutdown
|
struct Out_Shutdown
|
||||||
: public lsOutMessage<Out_Shutdown> {
|
: public lsOutMessage<Out_Shutdown> {
|
||||||
lsRequestId id;
|
lsRequestId id; // defaults to std::monostate (null)
|
||||||
std::monostate result;
|
std::monostate result; // null
|
||||||
};
|
};
|
||||||
MAKE_REFLECT_STRUCT(Out_Shutdown, jsonrpc, id, result);
|
MAKE_REFLECT_STRUCT(Out_Shutdown, jsonrpc, id, result);
|
||||||
|
|
||||||
struct ShutdownHandler : BaseMessageHandler<Ipc_Shutdown> {
|
struct ShutdownHandler : BaseMessageHandler<Ipc_Shutdown> {
|
||||||
void Run(Ipc_Shutdown* request) override {
|
void Run(Ipc_Shutdown* request) override {
|
||||||
Out_Shutdown out;
|
Out_Shutdown out;
|
||||||
// FIXME lsRequestId should be number | string | null
|
|
||||||
out.id.id0 = 0;
|
|
||||||
QueueManager::WriteStdout(IpcId::TextDocumentDefinition, out);
|
QueueManager::WriteStdout(IpcId::TextDocumentDefinition, out);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -60,6 +60,14 @@ void Reflect(Writer& visitor, uint64_t& value) {
|
|||||||
visitor.Uint64(value);
|
visitor.Uint64(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// double
|
||||||
|
void Reflect(Reader& visitor, double& value) {
|
||||||
|
value = visitor.GetDouble();
|
||||||
|
}
|
||||||
|
void Reflect(Writer& visitor, double& value) {
|
||||||
|
visitor.Double(value);
|
||||||
|
}
|
||||||
|
|
||||||
// bool
|
// bool
|
||||||
void Reflect(Reader& visitor, bool& value) {
|
void Reflect(Reader& visitor, bool& value) {
|
||||||
value = visitor.GetBool();
|
value = visitor.GetBool();
|
||||||
|
@ -28,6 +28,7 @@ class Reader {
|
|||||||
virtual int GetInt() = 0;
|
virtual int GetInt() = 0;
|
||||||
virtual int64_t GetInt64() = 0;
|
virtual int64_t GetInt64() = 0;
|
||||||
virtual uint64_t GetUint64() = 0;
|
virtual uint64_t GetUint64() = 0;
|
||||||
|
virtual double GetDouble() = 0;
|
||||||
virtual std::string GetString() = 0;
|
virtual std::string GetString() = 0;
|
||||||
|
|
||||||
virtual bool HasMember(const char* x) = 0;
|
virtual bool HasMember(const char* x) = 0;
|
||||||
@ -47,6 +48,7 @@ class Writer {
|
|||||||
virtual void Int(int x) = 0;
|
virtual void Int(int x) = 0;
|
||||||
virtual void Int64(int64_t x) = 0;
|
virtual void Int64(int64_t x) = 0;
|
||||||
virtual void Uint64(uint64_t x) = 0;
|
virtual void Uint64(uint64_t x) = 0;
|
||||||
|
virtual void Double(double x) = 0;
|
||||||
virtual void String(const char* x) = 0;
|
virtual void String(const char* x) = 0;
|
||||||
virtual void String(const char* x, size_t len) = 0;
|
virtual void String(const char* x, size_t len) = 0;
|
||||||
virtual void StartArray(size_t) = 0;
|
virtual void StartArray(size_t) = 0;
|
||||||
@ -147,6 +149,9 @@ void Reflect(Writer& visitor, int64_t& value);
|
|||||||
// uint64_t
|
// uint64_t
|
||||||
void Reflect(Reader& visitor, uint64_t& value);
|
void Reflect(Reader& visitor, uint64_t& value);
|
||||||
void Reflect(Writer& visitor, uint64_t& value);
|
void Reflect(Writer& visitor, uint64_t& value);
|
||||||
|
// double
|
||||||
|
void Reflect(Reader& visitor, double& value);
|
||||||
|
void Reflect(Writer& visitor, double& value);
|
||||||
// bool
|
// bool
|
||||||
void Reflect(Reader& visitor, bool& value);
|
void Reflect(Reader& visitor, bool& value);
|
||||||
void Reflect(Writer& visitor, bool& value);
|
void Reflect(Writer& visitor, bool& value);
|
||||||
@ -176,13 +181,27 @@ void Reflect(Writer& visitor, optional<T>& value) {
|
|||||||
visitor.Null();
|
visitor.Null();
|
||||||
}
|
}
|
||||||
|
|
||||||
// std::variant (Writer only)
|
template <size_t N, typename... Ts>
|
||||||
template <typename T0, typename T1>
|
struct ReflectVariant {
|
||||||
void Reflect(Writer& visitor, std::variant<T0, T1>& value) {
|
void operator()(Writer& visitor, std::variant<Ts...>& value) {
|
||||||
if (value.index() == 0)
|
if (value.index() == N - 1)
|
||||||
|
Reflect(visitor, std::get<N - 1>(value));
|
||||||
|
else
|
||||||
|
ReflectVariant<N - 1, Ts...>()(visitor, value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... Ts>
|
||||||
|
struct ReflectVariant<1, Ts...> {
|
||||||
|
void operator()(Writer& visitor, std::variant<Ts...>& value) {
|
||||||
Reflect(visitor, std::get<0>(value));
|
Reflect(visitor, std::get<0>(value));
|
||||||
else
|
}
|
||||||
Reflect(visitor, std::get<1>(value));
|
};
|
||||||
|
|
||||||
|
// std::variant (Writer only)
|
||||||
|
template <typename... Ts>
|
||||||
|
void Reflect(Writer& visitor, std::variant<Ts...>& value) {
|
||||||
|
ReflectVariant<sizeof...(Ts), Ts...>()(visitor, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// std::vector
|
// std::vector
|
||||||
|
@ -25,6 +25,7 @@ class JsonReader : public Reader {
|
|||||||
int GetInt() override { return m_->GetInt(); }
|
int GetInt() override { return m_->GetInt(); }
|
||||||
int64_t GetInt64() override { return m_->GetInt64(); }
|
int64_t GetInt64() override { return m_->GetInt64(); }
|
||||||
uint64_t GetUint64() override { return m_->GetUint64(); }
|
uint64_t GetUint64() override { return m_->GetUint64(); }
|
||||||
|
double GetDouble() override { return m_->GetDouble(); }
|
||||||
std::string GetString() override { return m_->GetString(); }
|
std::string GetString() override { return m_->GetString(); }
|
||||||
|
|
||||||
bool HasMember(const char* x) override { return m_->HasMember(x); }
|
bool HasMember(const char* x) override { return m_->HasMember(x); }
|
||||||
@ -64,6 +65,7 @@ class JsonWriter : public Writer {
|
|||||||
void Int(int x) override { m_->Int(x); }
|
void Int(int x) override { m_->Int(x); }
|
||||||
void Int64(int64_t x) override { m_->Int64(x); }
|
void Int64(int64_t x) override { m_->Int64(x); }
|
||||||
void Uint64(uint64_t x) override { m_->Uint64(x); }
|
void Uint64(uint64_t x) override { m_->Uint64(x); }
|
||||||
|
void Double(double x) override { m_->Double(x); }
|
||||||
void String(const char* x) override { m_->String(x); }
|
void String(const char* x) override { m_->String(x); }
|
||||||
void String(const char* x, size_t len) override { m_->String(x, len); }
|
void String(const char* x, size_t len) override { m_->String(x, len); }
|
||||||
void StartArray(size_t) override { m_->StartArray(); }
|
void StartArray(size_t) override { m_->StartArray(); }
|
||||||
|
@ -32,6 +32,7 @@ class MessagePackReader : public Reader {
|
|||||||
int GetInt() override { return Get<int>(); }
|
int GetInt() override { return Get<int>(); }
|
||||||
int64_t GetInt64() override { return Get<int64_t>(); }
|
int64_t GetInt64() override { return Get<int64_t>(); }
|
||||||
uint64_t GetUint64() override { return Get<uint64_t>(); }
|
uint64_t GetUint64() override { return Get<uint64_t>(); }
|
||||||
|
double GetDouble() override { return Get<double>(); }
|
||||||
std::string GetString() override { return Get<std::string>(); }
|
std::string GetString() override { return Get<std::string>(); }
|
||||||
|
|
||||||
bool HasMember(const char* x) override { return true; }
|
bool HasMember(const char* x) override { return true; }
|
||||||
@ -62,6 +63,7 @@ class MessagePackWriter : public Writer {
|
|||||||
void Int(int x) override { m_->pack(x); }
|
void Int(int x) override { m_->pack(x); }
|
||||||
void Int64(int64_t x) override { m_->pack(x); }
|
void Int64(int64_t x) override { m_->pack(x); }
|
||||||
void Uint64(uint64_t x) override { m_->pack(x); }
|
void Uint64(uint64_t x) override { m_->pack(x); }
|
||||||
|
void Double(double x) override { m_->pack(x); }
|
||||||
void String(const char* x) override { m_->pack(x); }
|
void String(const char* x) override { m_->pack(x); }
|
||||||
// TODO Remove std::string
|
// TODO Remove std::string
|
||||||
void String(const char* x, size_t len) override { m_->pack(std::string(x, len)); }
|
void String(const char* x, size_t len) override { m_->pack(std::string(x, len)); }
|
||||||
|
Loading…
Reference in New Issue
Block a user