WireDecoder: Add overloads for StringValue and Value.

Change-Id: Idb41fd16fec8efc8b984c1a9b4d225829d16e344
StringValue: Add comparison operators.
This commit is contained in:
Peter Johnson
2015-07-04 23:10:59 -07:00
parent a55b6565b8
commit 6aa32e8752
3 changed files with 36 additions and 3 deletions

View File

@@ -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); }

View File

@@ -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;
}

View File

@@ -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<NT_Value*>(value));
}
bool ReadString(StringValue* str) {
NT_DisposeString(str);
return ReadString(static_cast<NT_String*>(str));
}
WireDecoder(const WireDecoder&) = delete;
WireDecoder& operator=(const WireDecoder&) = delete;