Skip to content

Commit

Permalink
Change PopDataAsValue to return a std::unique_ptr.
Browse files Browse the repository at this point in the history
BUG=581865

Review-Url: https://codereview.chromium.org/2023633002
Cr-Commit-Position: refs/heads/master@{#396686}
  • Loading branch information
zetafunction authored and Commit bot committed May 30, 2016
1 parent ae9107f commit cef0354
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 66 deletions.
2 changes: 1 addition & 1 deletion chromeos/dbus/shill_client_unittest_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ void ShillClientUnittestBase::ExpectDictionaryValueArgument(
case dbus::Message::BOOL:
case dbus::Message::INT32:
case dbus::Message::STRING:
value.reset(dbus::PopDataAsValue(&variant_reader));
value = dbus::PopDataAsValue(&variant_reader);
break;
default:
NOTREACHED();
Expand Down
49 changes: 27 additions & 22 deletions dbus/values_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

#include "dbus/values_util.h"

#include <memory>
#include <utility>

#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "dbus/message.h"

Expand All @@ -24,10 +25,10 @@ bool IsExactlyRepresentableByDouble(T value) {
// Pops values from |reader| and appends them to |list_value|.
bool PopListElements(MessageReader* reader, base::ListValue* list_value) {
while (reader->HasMoreData()) {
base::Value* element_value = PopDataAsValue(reader);
std::unique_ptr<base::Value> element_value = PopDataAsValue(reader);
if (!element_value)
return false;
list_value->Append(element_value);
list_value->Append(std::move(element_value));
}
return true;
}
Expand Down Expand Up @@ -55,10 +56,10 @@ bool PopDictionaryEntries(MessageReader* reader,
base::JSONWriter::Write(*key, &key_string);
}
// Get the value and set the key-value pair.
base::Value* value = PopDataAsValue(&entry_reader);
std::unique_ptr<base::Value> value = PopDataAsValue(&entry_reader);
if (!value)
return false;
dictionary_value->SetWithoutPathExpansion(key_string, value);
dictionary_value->SetWithoutPathExpansion(key_string, std::move(value));
}
return true;
}
Expand Down Expand Up @@ -88,54 +89,57 @@ std::string GetTypeSignature(const base::Value& value) {

} // namespace

base::Value* PopDataAsValue(MessageReader* reader) {
base::Value* result = NULL;
std::unique_ptr<base::Value> PopDataAsValue(MessageReader* reader) {
std::unique_ptr<base::Value> result;
switch (reader->GetDataType()) {
case Message::INVALID_DATA:
// Do nothing.
break;
case Message::BYTE: {
uint8_t value = 0;
if (reader->PopByte(&value))
result = new base::FundamentalValue(value);
result = base::MakeUnique<base::FundamentalValue>(value);
break;
}
case Message::BOOL: {
bool value = false;
if (reader->PopBool(&value))
result = new base::FundamentalValue(value);
result = base::MakeUnique<base::FundamentalValue>(value);
break;
}
case Message::INT16: {
int16_t value = 0;
if (reader->PopInt16(&value))
result = new base::FundamentalValue(value);
result = base::MakeUnique<base::FundamentalValue>(value);
break;
}
case Message::UINT16: {
uint16_t value = 0;
if (reader->PopUint16(&value))
result = new base::FundamentalValue(value);
result = base::MakeUnique<base::FundamentalValue>(value);
break;
}
case Message::INT32: {
int32_t value = 0;
if (reader->PopInt32(&value))
result = new base::FundamentalValue(value);
result = base::MakeUnique<base::FundamentalValue>(value);
break;
}
case Message::UINT32: {
uint32_t value = 0;
if (reader->PopUint32(&value))
result = new base::FundamentalValue(static_cast<double>(value));
if (reader->PopUint32(&value)) {
result = base::MakeUnique<base::FundamentalValue>(
static_cast<double>(value));
}
break;
}
case Message::INT64: {
int64_t value = 0;
if (reader->PopInt64(&value)) {
DLOG_IF(WARNING, !IsExactlyRepresentableByDouble(value)) <<
value << " is not exactly representable by double";
result = new base::FundamentalValue(static_cast<double>(value));
result = base::MakeUnique<base::FundamentalValue>(
static_cast<double>(value));
}
break;
}
Expand All @@ -144,26 +148,27 @@ base::Value* PopDataAsValue(MessageReader* reader) {
if (reader->PopUint64(&value)) {
DLOG_IF(WARNING, !IsExactlyRepresentableByDouble(value)) <<
value << " is not exactly representable by double";
result = new base::FundamentalValue(static_cast<double>(value));
result = base::MakeUnique<base::FundamentalValue>(
static_cast<double>(value));
}
break;
}
case Message::DOUBLE: {
double value = 0;
if (reader->PopDouble(&value))
result = new base::FundamentalValue(value);
result = base::MakeUnique<base::FundamentalValue>(value);
break;
}
case Message::STRING: {
std::string value;
if (reader->PopString(&value))
result = new base::StringValue(value);
result = base::MakeUnique<base::StringValue>(value);
break;
}
case Message::OBJECT_PATH: {
ObjectPath value;
if (reader->PopObjectPath(&value))
result = new base::StringValue(value.value());
result = base::MakeUnique<base::StringValue>(value.value());
break;
}
case Message::UNIX_FD: {
Expand All @@ -180,11 +185,11 @@ base::Value* PopDataAsValue(MessageReader* reader) {
std::unique_ptr<base::DictionaryValue> dictionary_value(
new base::DictionaryValue);
if (PopDictionaryEntries(&sub_reader, dictionary_value.get()))
result = dictionary_value.release();
result = std::move(dictionary_value);
} else {
std::unique_ptr<base::ListValue> list_value(new base::ListValue);
if (PopListElements(&sub_reader, list_value.get()))
result = list_value.release();
result = std::move(list_value);
}
}
break;
Expand All @@ -194,7 +199,7 @@ base::Value* PopDataAsValue(MessageReader* reader) {
if (reader->PopStruct(&sub_reader)) {
std::unique_ptr<base::ListValue> list_value(new base::ListValue);
if (PopListElements(&sub_reader, list_value.get()))
result = list_value.release();
result = std::move(list_value);
}
break;
}
Expand Down
5 changes: 4 additions & 1 deletion dbus/values_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <stdint.h>

#include <memory>

#include "dbus/dbus_export.h"

namespace base {
Expand All @@ -22,7 +24,8 @@ class MessageWriter;
// Returns NULL if an error occurs.
// Note: Integer values larger than int32_t (including uint32_t) are converted
// to double. Non-string dictionary keys are converted to strings.
CHROME_DBUS_EXPORT base::Value* PopDataAsValue(MessageReader* reader);
CHROME_DBUS_EXPORT std::unique_ptr<base::Value> PopDataAsValue(
MessageReader* reader);

// Appends a basic type value to |writer|. Basic types are BOOLEAN, INTEGER,
// DOUBLE, and STRING. Use this function for values that are known to be basic
Expand Down
Loading

0 comments on commit cef0354

Please sign in to comment.