From 6aa32e87523f8c783abdca0eb6562fe9d2b9c862 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 4 Jul 2015 23:10:59 -0700 Subject: [PATCH] WireDecoder: Add overloads for StringValue and Value. Change-Id: Idb41fd16fec8efc8b984c1a9b4d225829d16e344 StringValue: Add comparison operators. --- src/Value.h | 24 +++++++++++++++++++++++- src/WireDecoder.cpp | 5 +++-- src/WireDecoder.h | 10 ++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Value.h b/src/Value.h index 7d7f76bd38..1015e36ba5 100644 --- a/src/Value.h +++ b/src/Value.h @@ -22,6 +22,7 @@ class StringValueTest; class Storage; class Value; class ValueTest; +class WireDecoder; /* * C++ wrapper class around NT_String. @@ -29,9 +30,10 @@ class ValueTest; class StringValue : private NT_String { friend class StringValueTest; friend class Value; + friend class WireDecoder; public: StringValue() { NT_InitString(this); } - /*implicit*/ StringValue(llvm::StringRef val); + explicit StringValue(llvm::StringRef val); ~StringValue() { NT_DisposeString(this); } operator llvm::StringRef() const { return llvm::StringRef(str, len); } @@ -58,12 +60,32 @@ class StringValue : private NT_String { } }; +inline bool operator==(const StringValue& lhs, const StringValue& rhs) { + return llvm::StringRef(lhs) == llvm::StringRef(rhs); +} +inline bool operator!=(const StringValue& lhs, const StringValue& rhs) { + return llvm::StringRef(lhs) != llvm::StringRef(rhs); +} +inline bool operator==(llvm::StringRef lhs, const StringValue& rhs) { + return lhs == llvm::StringRef(rhs); +} +inline bool operator!=(llvm::StringRef lhs, const StringValue& rhs) { + return lhs != llvm::StringRef(rhs); +} +inline bool operator==(const StringValue& lhs, llvm::StringRef rhs) { + return llvm::StringRef(lhs) == rhs; +} +inline bool operator!=(const StringValue& lhs, llvm::StringRef rhs) { + return llvm::StringRef(lhs) != rhs; +} + /* * C++ wrapper class around NT_Value. */ class Value : private NT_Value { friend class ValueTest; friend class Storage; + friend class WireDecoder; public: Value() { NT_InitValue(this); } ~Value() { NT_DisposeValue(this); } diff --git a/src/WireDecoder.cpp b/src/WireDecoder.cpp index 55120c9f97..fda46eeaad 100644 --- a/src/WireDecoder.cpp +++ b/src/WireDecoder.cpp @@ -102,6 +102,7 @@ bool WireDecoder::ReadType(NT_Type* type) { *type = NT_RPC; break; default: + *type = NT_UNASSIGNED; m_error = "unrecognized value type"; return false; } @@ -109,8 +110,6 @@ bool WireDecoder::ReadType(NT_Type* type) { } bool WireDecoder::ReadValue(NT_Type type, NT_Value* value) { - value->type = type; - value->last_change = 0; switch (type) { case NT_BOOLEAN: { unsigned int v; @@ -188,6 +187,8 @@ bool WireDecoder::ReadValue(NT_Type type, NT_Value* value) { m_error = "invalid type when trying to read value"; return false; } + value->type = type; + value->last_change = 0; return true; } diff --git a/src/WireDecoder.h b/src/WireDecoder.h index 67c2a9cbb4..43ecff1554 100644 --- a/src/WireDecoder.h +++ b/src/WireDecoder.h @@ -13,6 +13,7 @@ #include "ntcore.h" #include "leb128.h" #include "raw_istream.h" +#include "Value.h" namespace ntimpl { @@ -101,6 +102,15 @@ class WireDecoder { bool ReadValue(NT_Type type, NT_Value* value); bool ReadString(NT_String* str); + bool ReadValue(NT_Type type, Value* value) { + NT_DisposeValue(value); + return ReadValue(type, static_cast(value)); + } + bool ReadString(StringValue* str) { + NT_DisposeString(str); + return ReadString(static_cast(str)); + } + WireDecoder(const WireDecoder&) = delete; WireDecoder& operator=(const WireDecoder&) = delete;