MessageReader: Use rvalue refs and std::move for handlers.

Change-Id: Iedba512d64ef2674c18a88b9fb720b2a6346b902
This commit is contained in:
Peter Johnson
2015-07-05 16:27:27 -07:00
parent f4673f3123
commit 071b278b71
2 changed files with 21 additions and 20 deletions

View File

@@ -34,14 +34,13 @@ bool MessageReader::Run() {
case NT_MSG_CLIENT_HELLO: {
unsigned int proto_rev;
if (!Read16(&proto_rev)) return false;
NT_String self_id;
NT_InitString(&self_id);
StringValue self_id;
// This intentionally uses the provided proto_rev instead of
// m_proto_rev.
if (proto_rev >= 0x0300u) {
if (!ReadString(&self_id)) return false;
}
m_handler.GotClientHello(proto_rev, self_id);
m_handler.GotClientHello(proto_rev, std::move(self_id));
break;
}
case NT_MSG_PROTO_UNSUP: {
@@ -65,24 +64,26 @@ bool MessageReader::Run() {
m_handler.GotClientHelloDone();
break;
case NT_MSG_ENTRY_ASSIGN: {
NT_String name;
StringValue name;
if (!ReadString(&name)) return false;
NT_Type type;
if (!ReadType(&type)) return false;
unsigned int id, seq_num;
if (!Read16(&id)) return false;
if (!Read16(&seq_num)) return false;
unsigned int flags = 0;
NT_Value value;
if (!ReadType(&type) || !Read16(&id) || !Read16(&seq_num) ||
(m_proto_rev >= 0x0300u && !Read8(&flags)) ||
!ReadValue(type, &value)) {
NT_DisposeString(&name);
return false;
if (m_proto_rev >= 0x0300u) {
if (!Read8(&flags)) return false;
}
m_handler.GotEntryAssign(name, id, seq_num, value, flags);
Value value;
if (!ReadValue(type, &value)) return false;
m_handler.GotEntryAssign(std::move(name), id, seq_num, std::move(value),
flags);
break;
}
case NT_MSG_ENTRY_UPDATE: {
unsigned int id, seq_num;
NT_Value value;
Value value;
if (!Read16(&id)) return false;
if (!Read16(&seq_num)) return false;
NT_Type type;
@@ -93,7 +94,7 @@ bool MessageReader::Run() {
} else
type = m_handler.GetEntryType(id);
if (!ReadValue(type, &value)) return false;
m_handler.GotEntryUpdate(id, seq_num, value);
m_handler.GotEntryUpdate(id, seq_num, std::move(value));
break;
}
case NT_MSG_FLAGS_UPDATE: {

View File

@@ -8,6 +8,7 @@
#ifndef NT_MESSAGEREADER_H_
#define NT_MESSAGEREADER_H_
#include "Value.h"
#include "WireDecoder.h"
namespace ntimpl {
@@ -19,20 +20,19 @@ class MessageHandler {
// Needed for protocol rev 2.0 ENTRY_UPDATE messages.
virtual NT_Type GetEntryType(unsigned int id) = 0;
// All of these functions are expected to take ownership of passed
// strings/values.
virtual ~MessageHandler() {}
virtual void GotKeepAlive() = 0;
virtual void GotClientHello(unsigned int proto_rev, NT_String& self_id) = 0;
virtual void GotClientHello(unsigned int proto_rev,
StringValue&& self_id) = 0;
virtual void GotProtoUnsup(unsigned int proto_rev) = 0;
virtual void GotServerHelloDone() = 0;
virtual void GotServerHello(unsigned int flags, NT_String& self_id) = 0;
virtual void GotServerHello(unsigned int flags, StringValue&& self_id) = 0;
virtual void GotClientHelloDone() = 0;
virtual void GotEntryAssign(NT_String& name, unsigned int id,
unsigned int seq_num, NT_Value& value,
virtual void GotEntryAssign(StringValue&& name, unsigned int id,
unsigned int seq_num, Value&& value,
unsigned int flags) = 0;
virtual void GotEntryUpdate(unsigned int id, unsigned int seq_num,
NT_Value& value) = 0;
Value&& value) = 0;
virtual void GotFlagsUpdate(unsigned int id, unsigned int flags) = 0;
virtual void GotEntryDelete(unsigned int id) = 0;
virtual void GotClearEntries() = 0;