From 503127e0da5b40645301f112c914a8b01c65b021 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 9 Jan 2018 22:34:58 -0800 Subject: [PATCH] Reflect std::variant and change lsRequestId to variant (#279) --- src/language_server_api.cc | 25 ++++++++++--------------- src/language_server_api.h | 6 +----- src/message_handler.cc | 1 - src/messages/shutdown.cc | 6 ++---- src/serializer.cc | 8 ++++++++ src/serializer.h | 31 +++++++++++++++++++++++++------ src/serializers/json.h | 2 ++ src/serializers/msgpack.h | 2 ++ 8 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/language_server_api.cc b/src/language_server_api.cc index ff8918cd..9c1acf4a 100644 --- a/src/language_server_api.cc +++ b/src/language_server_api.cc @@ -5,22 +5,17 @@ #include #include -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"; } diff --git a/src/language_server_api.h b/src/language_server_api.h index a798b2ad..120207de 100644 --- a/src/language_server_api.h +++ b/src/language_server_api.h @@ -24,11 +24,7 @@ ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// -struct lsRequestId { - optional id0; - optional id1; -}; -void Reflect(Writer& visitor, lsRequestId& value); +using lsRequestId = std::variant; void Reflect(Reader& visitor, lsRequestId& id); ///////////////////////////////////////////////////////////////////////////// diff --git a/src/message_handler.cc b/src/message_handler.cc index c1c5d6aa..b54cfc68 100644 --- a/src/message_handler.cc +++ b/src/message_handler.cc @@ -209,7 +209,6 @@ void EmitSemanticHighlighting(QueryDatabase* db, std::vector 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 diff --git a/src/messages/shutdown.cc b/src/messages/shutdown.cc index b8ea5eaa..f5b0096e 100644 --- a/src/messages/shutdown.cc +++ b/src/messages/shutdown.cc @@ -10,16 +10,14 @@ REGISTER_IPC_MESSAGE(Ipc_Shutdown); struct Out_Shutdown : public lsOutMessage { - 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 { 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); } }; diff --git a/src/serializer.cc b/src/serializer.cc index 0fff4eae..fc42adbb 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -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(); diff --git a/src/serializer.h b/src/serializer.h index b62dd39a..a1b7fb42 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -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& value) { visitor.Null(); } -// std::variant (Writer only) -template -void Reflect(Writer& visitor, std::variant& value) { - if (value.index() == 0) +template +struct ReflectVariant { + void operator()(Writer& visitor, std::variant& value) { + if (value.index() == N - 1) + Reflect(visitor, std::get(value)); + else + ReflectVariant()(visitor, value); + } +}; + +template +struct ReflectVariant<1, Ts...> { + void operator()(Writer& visitor, std::variant& value) { Reflect(visitor, std::get<0>(value)); - else - Reflect(visitor, std::get<1>(value)); + } +}; + +// std::variant (Writer only) +template +void Reflect(Writer& visitor, std::variant& value) { + ReflectVariant()(visitor, value); } // std::vector diff --git a/src/serializers/json.h b/src/serializers/json.h index 38b2cf65..c304a322 100644 --- a/src/serializers/json.h +++ b/src/serializers/json.h @@ -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(); } diff --git a/src/serializers/msgpack.h b/src/serializers/msgpack.h index 9d5a57e2..e0ed84a1 100644 --- a/src/serializers/msgpack.h +++ b/src/serializers/msgpack.h @@ -32,6 +32,7 @@ class MessagePackReader : public Reader { int GetInt() override { return Get(); } int64_t GetInt64() override { return Get(); } uint64_t GetUint64() override { return Get(); } + double GetDouble() override { return Get(); } std::string GetString() override { return Get(); } 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)); }