Add a parameter (number of __VA_ARGS__) to REFLECT_MEMBER_START

rapidjson StartArray StartObject do not need the number of fields, but msgpack-c does.
This commit is contained in:
Fangrui Song 2018-01-06 16:22:46 -08:00
parent d22f994d8a
commit fe0313f0b5
10 changed files with 61 additions and 56 deletions

View File

@ -43,7 +43,7 @@ struct RealCacheManager : ICacheManager {
return ReadContent(GetCachePath(path));
}
std::unique_ptr<IndexFile> RawCacheLoad(const std::string& path) {
std::unique_ptr<IndexFile> RawCacheLoad(const std::string& path) override {
if (!config_->enableCacheRead)
return nullptr;
@ -102,7 +102,7 @@ struct FakeCacheManager : ICacheManager {
return nullopt;
}
std::unique_ptr<IndexFile> RawCacheLoad(const std::string& path) {
std::unique_ptr<IndexFile> RawCacheLoad(const std::string& path) override {
for (const FakeCacheEntry& entry : entries_) {
if (entry.path == path) {
return Deserialize(SerializeFormat::Json, path, entry.json, nullopt);

View File

@ -64,8 +64,7 @@ bool operator!=(const Id<T>& a, const Id<T>& b) {
template <typename T>
void Reflect(Reader& visitor, Id<T>& id) {
if (visitor.IsUint64())
id.id = visitor.GetUint64();
id.id = visitor.GetUint64();
}
template <typename T>
void Reflect(Writer& visitor, Id<T>& value) {
@ -239,7 +238,7 @@ template <typename TVisitor,
typename Range>
void Reflect(TVisitor& visitor,
TypeDefDefinitionData<TypeId, FuncId, VarId, Range>& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(12);
REFLECT_MEMBER(short_name);
REFLECT_MEMBER(detailed_name);
REFLECT_MEMBER(kind);
@ -338,7 +337,7 @@ template <typename TVisitor,
void Reflect(
TVisitor& visitor,
FuncDefDefinitionData<TypeId, FuncId, VarId, FuncRef, Range>& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(12);
REFLECT_MEMBER(short_name);
REFLECT_MEMBER(detailed_name);
REFLECT_MEMBER(kind);
@ -452,7 +451,7 @@ template <typename TVisitor,
typename Range>
void Reflect(TVisitor& visitor,
VarDefDefinitionData<TypeId, FuncId, VarId, Range>& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(9);
REFLECT_MEMBER(short_name);
REFLECT_MEMBER(detailed_name);
REFLECT_MEMBER(kind);

View File

@ -194,7 +194,7 @@ void lsResponseError::Write(Writer& visitor) {
auto& value = *this;
int code2 = static_cast<int>(this->code);
visitor.StartObject();
visitor.StartObject(2 + !!data);
REFLECT_MEMBER2("code", code2);
REFLECT_MEMBER(message);
if (data) {

View File

@ -226,7 +226,7 @@ struct lsCommand {
};
template <typename TVisitor, typename T>
void Reflect(TVisitor& visitor, lsCommand<T>& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(3);
REFLECT_MEMBER(title);
REFLECT_MEMBER(command);
REFLECT_MEMBER(arguments);
@ -245,7 +245,7 @@ struct lsCodeLens {
};
template <typename TVisitor, typename TData, typename TCommandArguments>
void Reflect(TVisitor& visitor, lsCodeLens<TData, TCommandArguments>& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(3);
REFLECT_MEMBER(range);
REFLECT_MEMBER(command);
REFLECT_MEMBER(data);
@ -979,7 +979,7 @@ struct Out_TextDocumentPublishDiagnostics
template <typename TVisitor>
void Reflect(TVisitor& visitor, Out_TextDocumentPublishDiagnostics& value) {
std::string method = "textDocument/publishDiagnostics";
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(3);
REFLECT_MEMBER(jsonrpc);
REFLECT_MEMBER2("method", method);
REFLECT_MEMBER(params);
@ -1044,7 +1044,7 @@ struct Out_ShowLogMessage : public lsOutMessage<Out_ShowLogMessage> {
};
template <typename TVisitor>
void Reflect(TVisitor& visitor, Out_ShowLogMessage& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(3);
REFLECT_MEMBER(jsonrpc);
std::string method = value.method();
REFLECT_MEMBER2("method", method);

View File

@ -18,7 +18,7 @@ struct lsCodeLensCommandArguments {
std::vector<lsLocation> locations;
};
void Reflect(Writer& visitor, lsCodeLensCommandArguments& value) {
visitor.StartArray();
visitor.StartArray(3);
Reflect(visitor, value.uri);
Reflect(visitor, value.position);
Reflect(visitor, value.locations);

View File

@ -58,7 +58,7 @@ struct Out_TextDocumentHover : public lsOutMessage<Out_TextDocumentHover> {
};
MAKE_REFLECT_STRUCT(Out_TextDocumentHover::Result, contents, range);
void Reflect(Writer& visitor, Out_TextDocumentHover& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(3);
REFLECT_MEMBER(jsonrpc);
REFLECT_MEMBER(id);
if (value.result)

View File

@ -161,7 +161,7 @@ struct MergeableUpdate {
};
template <typename TVisitor, typename TId, typename TValue>
void Reflect(TVisitor& visitor, MergeableUpdate<TId, TValue>& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(3);
REFLECT_MEMBER(id);
REFLECT_MEMBER(to_add);
REFLECT_MEMBER(to_remove);
@ -177,7 +177,7 @@ struct WithUsr {
};
template <typename TVisitor, typename T>
void Reflect(TVisitor& visitor, WithUsr<T>& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(2);
REFLECT_MEMBER(usr);
REFLECT_MEMBER(value);
REFLECT_MEMBER_END();

View File

@ -2,7 +2,7 @@
// TODO Move Json* to serializers/json.cc
#include "serializers/json.h"
//#include "serializers/msgpack.h"
#include "serializers/msgpack.h"
#include "indexer.h"
@ -31,8 +31,7 @@ void Reflect(Writer& visitor, int16_t& value) {
// int32_t
void Reflect(Reader& visitor, int32_t& value) {
if (visitor.IsInt())
value = visitor.GetInt();
value = visitor.GetInt();
}
void Reflect(Writer& visitor, int32_t& value) {
visitor.Int(value);
@ -40,8 +39,7 @@ void Reflect(Writer& visitor, int32_t& value) {
// int64_t
void Reflect(Reader& visitor, int64_t& value) {
if (visitor.IsInt64())
value = visitor.GetInt64();
value = visitor.GetInt64();
}
void Reflect(Writer& visitor, int64_t& value) {
visitor.Int64(value);
@ -49,8 +47,7 @@ void Reflect(Writer& visitor, int64_t& value) {
// uint64_t
void Reflect(Reader& visitor, uint64_t& value) {
if (visitor.IsUint64())
value = visitor.GetUint64();
value = visitor.GetUint64();
}
void Reflect(Writer& visitor, uint64_t& value) {
visitor.Uint64(value);
@ -58,8 +55,7 @@ void Reflect(Writer& visitor, uint64_t& value) {
// bool
void Reflect(Reader& visitor, bool& value) {
if (visitor.IsBool())
value = visitor.GetBool();
value = visitor.GetBool();
}
void Reflect(Writer& visitor, bool& value) {
visitor.Bool(value);
@ -82,13 +78,13 @@ void ReflectMember(Writer& visitor, const char* name, std::string& value) {
// TODO: Move this to indexer.cc
void Reflect(Reader& visitor, IndexInclude& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(2);
REFLECT_MEMBER(line);
REFLECT_MEMBER(resolved_path);
REFLECT_MEMBER_END();
}
void Reflect(Writer& visitor, IndexInclude& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(2);
REFLECT_MEMBER(line);
if (gTestOutputMode) {
std::string basename = GetBaseName(value.resolved_path);
@ -103,7 +99,7 @@ void Reflect(Writer& visitor, IndexInclude& value) {
template <typename TVisitor>
void Reflect(TVisitor& visitor, IndexType& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(16);
REFLECT_MEMBER2("id", value.id);
REFLECT_MEMBER2("usr", value.usr);
REFLECT_MEMBER2("short_name", value.def.short_name);
@ -125,7 +121,7 @@ void Reflect(TVisitor& visitor, IndexType& value) {
template <typename TVisitor>
void Reflect(TVisitor& visitor, IndexFunc& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(16);
REFLECT_MEMBER2("id", value.id);
REFLECT_MEMBER2("is_operator", value.def.is_operator);
REFLECT_MEMBER2("usr", value.usr);
@ -147,7 +143,7 @@ void Reflect(TVisitor& visitor, IndexFunc& value) {
template <typename TVisitor>
void Reflect(TVisitor& visitor, IndexVar& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(13);
REFLECT_MEMBER2("id", value.id);
REFLECT_MEMBER2("usr", value.usr);
REFLECT_MEMBER2("short_name", value.def.short_name);
@ -165,7 +161,7 @@ void Reflect(TVisitor& visitor, IndexVar& value) {
}
// IndexFile
bool ReflectMemberStart(Writer& visitor, IndexFile& value) {
bool ReflectMemberStart(Writer& visitor, IndexFile& value, size_t n) {
auto it = value.id_cache.usr_to_type_id.find("");
if (it != value.id_cache.usr_to_type_id.end()) {
value.Resolve(it->second)->def.short_name = "<fundamental>";
@ -173,12 +169,12 @@ bool ReflectMemberStart(Writer& visitor, IndexFile& value) {
}
value.version = IndexFile::kCurrentVersion;
DefaultReflectMemberStart(visitor);
DefaultReflectMemberStart(visitor, n);
return true;
}
template <typename TVisitor>
void Reflect(TVisitor& visitor, IndexFile& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER_START(5 + (gTestOutputMode ? 0 : 6));
if (!gTestOutputMode) {
REFLECT_MEMBER(version);
REFLECT_MEMBER(last_modification_time);

View File

@ -13,13 +13,14 @@ enum class SerializeFormat { Json, MessagePack };
class Reader {
public:
virtual ~Reader() {}
virtual SerializeFormat Format() const = 0;
virtual bool IsBool() = 0;
//virtual bool IsBool() = 0;
virtual bool IsNull() = 0;
virtual bool IsArray() = 0;
virtual bool IsInt() = 0;
virtual bool IsInt64() = 0;
virtual bool IsUint64() = 0;
//virtual bool IsInt64() = 0;
//virtual bool IsUint64() = 0;
virtual bool IsString() = 0;
virtual bool GetBool() = 0;
@ -38,6 +39,7 @@ class Reader {
class Writer {
public:
virtual ~Writer() {}
virtual SerializeFormat Format() const = 0;
virtual void Null() = 0;
virtual void Bool(bool x) = 0;
@ -46,17 +48,17 @@ class Writer {
virtual void Uint64(uint64_t x) = 0;
virtual void String(const char* x) = 0;
virtual void String(const char* x, size_t len) = 0;
virtual void StartArray() = 0;
virtual void StartArray(size_t) = 0;
virtual void EndArray() = 0;
virtual void StartObject() = 0;
virtual void StartObject(size_t) = 0;
virtual void EndObject() = 0;
virtual void Key(const char* name) = 0;
};
struct IndexFile;
#define REFLECT_MEMBER_START() \
if (!ReflectMemberStart(visitor, value)) \
#define REFLECT_MEMBER_START(n) \
if (!ReflectMemberStart(visitor, value, n)) \
return
#define REFLECT_MEMBER_START1(value) \
if (!ReflectMemberStart(visitor, value)) \
@ -74,19 +76,25 @@ struct IndexFile;
value = static_cast<type>(value0); \
}
// clang-format off
// Config has many fields, we need to support at least its number of fields.
#define NUM_VA_ARGS_IMPL(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,N,...) N
#define NUM_VA_ARGS(...) NUM_VA_ARGS_IMPL(__VA_ARGS__,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
// clang-format on
#define _MAPPABLE_REFLECT_MEMBER(name) REFLECT_MEMBER(name);
#define MAKE_REFLECT_EMPTY_STRUCT(type, ...) \
template <typename TVisitor> \
void Reflect(TVisitor& visitor, type& value) { \
REFLECT_MEMBER_START(); \
REFLECT_MEMBER_START(0); \
REFLECT_MEMBER_END(); \
}
#define MAKE_REFLECT_STRUCT(type, ...) \
template <typename TVisitor> \
void Reflect(TVisitor& visitor, type& value) { \
REFLECT_MEMBER_START(); \
REFLECT_MEMBER_START(NUM_VA_ARGS(__VA_ARGS__)); \
MACRO_MAP(_MAPPABLE_REFLECT_MEMBER, __VA_ARGS__) \
REFLECT_MEMBER_END(); \
}
@ -97,7 +105,7 @@ struct IndexFile;
// This currently only supports writers.
#define MAKE_REFLECT_STRUCT_WRITER_AS_ARRAY(type, ...) \
inline void Reflect(Writer& visitor, type& value) { \
visitor.StartArray(); \
visitor.StartArray(NUM_VA_ARGS(__VA_ARGS__)); \
MACRO_MAP(_MAPPABLE_REFLECT_ARRAY, __VA_ARGS__) \
visitor.EndArray(); \
}
@ -180,7 +188,7 @@ void Reflect(Reader& visitor, std::vector<T>& values) {
}
template <typename T>
void Reflect(Writer& visitor, std::vector<T>& values) {
visitor.StartArray();
visitor.StartArray(values.size());
for (auto& value : values)
Reflect(visitor, value);
visitor.EndArray();
@ -188,12 +196,12 @@ void Reflect(Writer& visitor, std::vector<T>& values) {
// Writer:
inline void DefaultReflectMemberStart(Writer& visitor) {
visitor.StartObject();
inline void DefaultReflectMemberStart(Writer& visitor, size_t n) {
visitor.StartObject(n);
}
template <typename T>
bool ReflectMemberStart(Writer& visitor, T& value) {
visitor.StartObject();
bool ReflectMemberStart(Writer& visitor, T& value, size_t n) {
visitor.StartObject(n);
return true;
}
template <typename T>
@ -208,7 +216,7 @@ void ReflectMember(Writer& visitor, const char* name, T& value) {
template <typename T>
void ReflectMember(Writer& visitor, const char* name, std::vector<T>& values) {
visitor.Key(name);
visitor.StartArray();
visitor.StartArray(values.size());
for (auto& value : values)
Reflect(visitor, value);
visitor.EndArray();
@ -225,9 +233,9 @@ void ReflectMember(Writer& visitor, const char* name, std::string& value);
// Reader:
inline void DefaultReflectMemberStart(Reader& visitor) {}
inline void DefaultReflectMemberStart(Reader& visitor, size_t n) {}
template <typename T>
bool ReflectMemberStart(Reader& visitor, T& value) {
bool ReflectMemberStart(Reader& visitor, T& value, size_t n) {
return true;
}
template <typename T>

View File

@ -10,13 +10,14 @@ class JsonReader : public Reader {
public:
JsonReader(rapidjson::GenericValue<rapidjson::UTF8<>>* m) : m_(m) {}
SerializeFormat Format() const override { return SerializeFormat::Json; }
bool IsBool() override { return m_->IsBool(); }
//bool IsBool() override { return m_->IsBool(); }
bool IsNull() override { return m_->IsNull(); }
bool IsArray() override { return m_->IsArray(); }
bool IsInt() override { return m_->IsInt(); }
bool IsInt64() override { return m_->IsInt64(); }
bool IsUint64() override { return m_->IsUint64(); }
//bool IsInt64() override { return m_->IsInt64(); }
//bool IsUint64() override { return m_->IsUint64(); }
bool IsString() override { return m_->IsString(); }
bool GetBool() override { return m_->GetBool(); }
@ -52,6 +53,7 @@ class JsonWriter : public Writer {
public:
JsonWriter(rapidjson::Writer<rapidjson::StringBuffer>* m) : m_(m) {}
SerializeFormat Format() const override { return SerializeFormat::Json; }
void Null() override { m_->Null(); }
void Bool(bool x) override { m_->Bool(x); }
@ -60,9 +62,9 @@ class JsonWriter : public Writer {
void Uint64(uint64_t x) override { m_->Uint64(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() override { m_->StartArray(); }
void StartArray(size_t) override { m_->StartArray(); }
void EndArray() override { m_->EndArray(); }
void StartObject() override { m_->StartObject(); }
void StartObject(size_t) override { m_->StartObject(); }
void EndObject() override { m_->EndObject(); }
void Key(const char* name) override { m_->Key(name); }
};