Reflect std::variant and change lsRequestId to variant<monostate,double,string> (#279)

This commit is contained in:
Fangrui Song 2018-01-09 22:34:58 -08:00 committed by GitHub
parent a99fd7c444
commit 503127e0da
8 changed files with 50 additions and 31 deletions

View File

@ -5,22 +5,17 @@
#include <doctest/doctest.h>
#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) {
if (visitor.IsInt())
Reflect(visitor, id.id0);
else if (visitor.IsString())
Reflect(visitor, id.id1);
else
if (visitor.IsInt()) {
int v;
Reflect(visitor, v);
id = v;
}
else if (visitor.IsString()) {
std::string v;
Reflect(visitor, v);
id = v;
} else
LOG_S(WARNING) << "Unable to deserialize id";
}

View File

@ -24,11 +24,7 @@
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
struct lsRequestId {
optional<int> id0;
optional<std::string> id1;
};
void Reflect(Writer& visitor, lsRequestId& value);
using lsRequestId = std::variant<std::monostate, double, std::string>;
void Reflect(Reader& visitor, lsRequestId& id);
/////////////////////////////////////////////////////////////////////////////

View File

@ -209,7 +209,6 @@ void EmitSemanticHighlighting(QueryDatabase* db,
std::vector<uint8_t> deleted(id, 0);
int top = 0;
for (size_t i = 0; i < events.size(); i++) {
// |start| is used as liveness tag here.
while (top && deleted[events[top - 1].id])
top--;
// Order [a, b0) after [a, b1) if b0 < b1. The range comes later overrides

View File

@ -10,16 +10,14 @@ REGISTER_IPC_MESSAGE(Ipc_Shutdown);
struct Out_Shutdown
: public lsOutMessage<Out_Shutdown> {
lsRequestId id;
std::monostate result;
lsRequestId id; // defaults to std::monostate (null)
std::monostate result; // null
};
MAKE_REFLECT_STRUCT(Out_Shutdown, jsonrpc, id, result);
struct ShutdownHandler : BaseMessageHandler<Ipc_Shutdown> {
void Run(Ipc_Shutdown* request) override {
Out_Shutdown out;
// FIXME lsRequestId should be number | string | null
out.id.id0 = 0;
QueueManager::WriteStdout(IpcId::TextDocumentDefinition, out);
}
};

View File

@ -60,6 +60,14 @@ void Reflect(Writer& visitor, uint64_t& value) {
visitor.Uint64(value);
}
// double
void Reflect(Reader& visitor, double& value) {
value = visitor.GetDouble();
}
void Reflect(Writer& visitor, double& value) {
visitor.Double(value);
}
// bool
void Reflect(Reader& visitor, bool& value) {
value = visitor.GetBool();

View File

@ -28,6 +28,7 @@ class Reader {
virtual int GetInt() = 0;
virtual int64_t GetInt64() = 0;
virtual uint64_t GetUint64() = 0;
virtual double GetDouble() = 0;
virtual std::string GetString() = 0;
virtual bool HasMember(const char* x) = 0;
@ -47,6 +48,7 @@ class Writer {
virtual void Int(int x) = 0;
virtual void Int64(int64_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, size_t len) = 0;
virtual void StartArray(size_t) = 0;
@ -147,6 +149,9 @@ void Reflect(Writer& visitor, int64_t& value);
// uint64_t
void Reflect(Reader& 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
void Reflect(Reader& visitor, bool& value);
void Reflect(Writer& visitor, bool& value);
@ -176,13 +181,27 @@ void Reflect(Writer& visitor, optional<T>& value) {
visitor.Null();
}
// std::variant (Writer only)
template <typename T0, typename T1>
void Reflect(Writer& visitor, std::variant<T0, T1>& value) {
if (value.index() == 0)
template <size_t N, typename... Ts>
struct ReflectVariant {
void operator()(Writer& visitor, std::variant<Ts...>& value) {
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));
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

View File

@ -25,6 +25,7 @@ class JsonReader : public Reader {
int GetInt() override { return m_->GetInt(); }
int64_t GetInt64() override { return m_->GetInt64(); }
uint64_t GetUint64() override { return m_->GetUint64(); }
double GetDouble() override { return m_->GetDouble(); }
std::string GetString() override { return m_->GetString(); }
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 Int64(int64_t x) override { m_->Int64(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, size_t len) override { m_->String(x, len); }
void StartArray(size_t) override { m_->StartArray(); }

View File

@ -32,6 +32,7 @@ class MessagePackReader : public Reader {
int GetInt() override { return Get<int>(); }
int64_t GetInt64() override { return Get<int64_t>(); }
uint64_t GetUint64() override { return Get<uint64_t>(); }
double GetDouble() override { return Get<double>(); }
std::string GetString() override { return Get<std::string>(); }
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 Int64(int64_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); }
// TODO Remove std::string
void String(const char* x, size_t len) override { m_->pack(std::string(x, len)); }