Commit dc5d0632 authored by dgozman's avatar dgozman Committed by Commit bot

[DevTools] Unify toValue and FromValue into ValueConversions.

BUG=none
TBR=pfeldman

Review-Url: https://codereview.chromium.org/2164353002
Cr-Commit-Position: refs/heads/master@{#407027}
parent d805a40e
...@@ -69,22 +69,22 @@ std::unique_ptr<AXProperty> createProperty(IgnoredReason reason) ...@@ -69,22 +69,22 @@ std::unique_ptr<AXProperty> createProperty(IgnoredReason reason)
std::unique_ptr<AXValue> createValue(const String& value, const String& type) std::unique_ptr<AXValue> createValue(const String& value, const String& type)
{ {
return AXValue::create().setType(type).setValue(protocol::toValue(value)).build(); return AXValue::create().setType(type).setValue(protocol::ValueConversions<String>::serialize(value)).build();
} }
std::unique_ptr<AXValue> createValue(int value, const String& type) std::unique_ptr<AXValue> createValue(int value, const String& type)
{ {
return AXValue::create().setType(type).setValue(protocol::toValue(value)).build(); return AXValue::create().setType(type).setValue(protocol::ValueConversions<int>::serialize(value)).build();
} }
std::unique_ptr<AXValue> createValue(float value, const String& type) std::unique_ptr<AXValue> createValue(float value, const String& type)
{ {
return AXValue::create().setType(type).setValue(protocol::toValue(value)).build(); return AXValue::create().setType(type).setValue(protocol::ValueConversions<double>::serialize(value)).build();
} }
std::unique_ptr<AXValue> createBooleanValue(bool value, const String& type) std::unique_ptr<AXValue> createBooleanValue(bool value, const String& type)
{ {
return AXValue::create().setType(type).setValue(protocol::toValue(value)).build(); return AXValue::create().setType(type).setValue(protocol::ValueConversions<bool>::serialize(value)).build();
} }
std::unique_ptr<AXRelatedNode> relatedNodeForAXObject(const AXObject* axObject, String* name = nullptr) std::unique_ptr<AXRelatedNode> relatedNodeForAXObject(const AXObject* axObject, String* name = nullptr)
......
...@@ -857,7 +857,6 @@ ...@@ -857,7 +857,6 @@
'inspector_protocol/String16WTF.h', 'inspector_protocol/String16WTF.h',
'inspector_protocol/Values.cpp', 'inspector_protocol/Values.cpp',
'inspector_protocol/Values.h', 'inspector_protocol/Values.h',
'inspector_protocol/ValueConversions.cpp',
'inspector_protocol/ValueConversions.h', 'inspector_protocol/ValueConversions.h',
'mac/BlockExceptions.h', 'mac/BlockExceptions.h',
'mac/BlockExceptions.mm', 'mac/BlockExceptions.mm',
......
...@@ -17,7 +17,7 @@ namespace blink { ...@@ -17,7 +17,7 @@ namespace blink {
namespace protocol { namespace protocol {
template<typename T> template<typename T>
class ArrayBase { class Array {
public: public:
static std::unique_ptr<Array<T>> create() static std::unique_ptr<Array<T>> create()
{ {
...@@ -31,12 +31,12 @@ public: ...@@ -31,12 +31,12 @@ public:
errors->addError("array expected"); errors->addError("array expected");
return nullptr; return nullptr;
} }
errors->push();
std::unique_ptr<Array<T>> result(new Array<T>()); std::unique_ptr<Array<T>> result(new Array<T>());
errors->push();
for (size_t i = 0; i < array->size(); ++i) { for (size_t i = 0; i < array->size(); ++i) {
errors->setName(String16::fromInteger(i)); errors->setName(String16::fromInteger(i));
T item = FromValue<T>::parse(array->at(i), errors); std::unique_ptr<T> item = ValueConversions<T>::parse(array->at(i), errors);
result->m_vector.push_back(item); result->m_vector.push_back(std::move(item));
} }
errors->pop(); errors->pop();
if (errors->hasErrors()) if (errors->hasErrors())
...@@ -44,9 +44,9 @@ public: ...@@ -44,9 +44,9 @@ public:
return result; return result;
} }
void addItem(const T& value) void addItem(std::unique_ptr<T> value)
{ {
m_vector.push_back(value); m_vector.push_back(std::move(value));
} }
size_t length() size_t length()
...@@ -54,31 +54,25 @@ public: ...@@ -54,31 +54,25 @@ public:
return m_vector.size(); return m_vector.size();
} }
T get(size_t index) T* get(size_t index)
{ {
return m_vector[index]; return m_vector[index].get();
} }
std::unique_ptr<protocol::ListValue> serialize() std::unique_ptr<protocol::ListValue> serialize()
{ {
std::unique_ptr<protocol::ListValue> result = ListValue::create(); std::unique_ptr<protocol::ListValue> result = ListValue::create();
for (auto& item : m_vector) for (auto& item : m_vector)
result->pushValue(toValue(item)); result->pushValue(ValueConversions<T>::serialize(item));
return result; return result;
} }
private: private:
std::vector<T> m_vector; std::vector<std::unique_ptr<T>> m_vector;
}; };
template<> class Array<String> : public ArrayBase<String> {};
template<> class Array<String16> : public ArrayBase<String16> {};
template<> class Array<int> : public ArrayBase<int> {};
template<> class Array<double> : public ArrayBase<double> {};
template<> class Array<bool> : public ArrayBase<bool> {};
template<typename T> template<typename T>
class Array { class ArrayBase {
public: public:
static std::unique_ptr<Array<T>> create() static std::unique_ptr<Array<T>> create()
{ {
...@@ -92,12 +86,12 @@ public: ...@@ -92,12 +86,12 @@ public:
errors->addError("array expected"); errors->addError("array expected");
return nullptr; return nullptr;
} }
std::unique_ptr<Array<T>> result(new Array<T>());
errors->push(); errors->push();
std::unique_ptr<Array<T>> result(new Array<T>());
for (size_t i = 0; i < array->size(); ++i) { for (size_t i = 0; i < array->size(); ++i) {
errors->setName(String16::fromInteger(i)); errors->setName(String16::fromInteger(i));
std::unique_ptr<T> item = FromValue<T>::parse(array->at(i), errors); T item = ValueConversions<T>::parse(array->at(i), errors);
result->m_vector.push_back(std::move(item)); result->m_vector.push_back(item);
} }
errors->pop(); errors->pop();
if (errors->hasErrors()) if (errors->hasErrors())
...@@ -105,9 +99,9 @@ public: ...@@ -105,9 +99,9 @@ public:
return result; return result;
} }
void addItem(std::unique_ptr<T> value) void addItem(const T& value)
{ {
m_vector.push_back(std::move(value)); m_vector.push_back(value);
} }
size_t length() size_t length()
...@@ -115,23 +109,29 @@ public: ...@@ -115,23 +109,29 @@ public:
return m_vector.size(); return m_vector.size();
} }
T* get(size_t index) T get(size_t index)
{ {
return m_vector[index].get(); return m_vector[index];
} }
std::unique_ptr<protocol::ListValue> serialize() std::unique_ptr<protocol::ListValue> serialize()
{ {
std::unique_ptr<protocol::ListValue> result = ListValue::create(); std::unique_ptr<protocol::ListValue> result = ListValue::create();
for (auto& item : m_vector) for (auto& item : m_vector)
result->pushValue(toValue(item)); result->pushValue(ValueConversions<T>::serialize(item));
return result; return result;
} }
private: private:
std::vector<std::unique_ptr<T>> m_vector; std::vector<T> m_vector;
}; };
template<> class Array<String> : public ArrayBase<String> {};
template<> class Array<String16> : public ArrayBase<String16> {};
template<> class Array<int> : public ArrayBase<int> {};
template<> class Array<double> : public ArrayBase<double> {};
template<> class Array<bool> : public ArrayBase<bool> {};
} // namespace platform } // namespace platform
} // namespace blink } // namespace blink
......
...@@ -49,11 +49,11 @@ std::unique_ptr<{{type.id}}> {{type.id}}::parse(protocol::Value* value, ErrorSup ...@@ -49,11 +49,11 @@ std::unique_ptr<{{type.id}}> {{type.id}}::parse(protocol::Value* value, ErrorSup
{% if property.optional %} {% if property.optional %}
if ({{property.name}}Value) { if ({{property.name}}Value) {
errors->setName("{{property.name}}"); errors->setName("{{property.name}}");
result->m_{{property.name}} = FromValue<{{resolve_type(property).raw_type}}>::parse({{property.name}}Value, errors); result->m_{{property.name}} = ValueConversions<{{resolve_type(property).raw_type}}>::parse({{property.name}}Value, errors);
} }
{% else %} {% else %}
errors->setName("{{property.name}}"); errors->setName("{{property.name}}");
result->m_{{property.name}} = FromValue<{{resolve_type(property).raw_type}}>::parse({{property.name}}Value, errors); result->m_{{property.name}} = ValueConversions<{{resolve_type(property).raw_type}}>::parse({{property.name}}Value, errors);
{% endif %} {% endif %}
{% endfor %} {% endfor %}
errors->pop(); errors->pop();
...@@ -68,9 +68,9 @@ std::unique_ptr<protocol::DictionaryValue> {{type.id}}::serialize() const ...@@ -68,9 +68,9 @@ std::unique_ptr<protocol::DictionaryValue> {{type.id}}::serialize() const
{% for property in type.properties %} {% for property in type.properties %}
{% if property.optional %} {% if property.optional %}
if (m_{{property.name}}.isJust()) if (m_{{property.name}}.isJust())
result->setValue("{{property.name}}", toValue(m_{{property.name}}.fromJust())); result->setValue("{{property.name}}", ValueConversions<{{resolve_type(property).raw_type}}>::serialize(m_{{property.name}}.fromJust()));
{% else %} {% else %}
result->setValue("{{property.name}}", toValue({{resolve_type(property).to_raw_type % ("m_" + property.name)}})); result->setValue("{{property.name}}", ValueConversions<{{resolve_type(property).raw_type}}>::serialize({{resolve_type(property).to_raw_type % ("m_" + property.name)}}));
{% endif %} {% endif %}
{% endfor %} {% endfor %}
return result; return result;
...@@ -119,9 +119,9 @@ void Frontend::{{event.name}}( ...@@ -119,9 +119,9 @@ void Frontend::{{event.name}}(
{% for parameter in event.parameters %} {% for parameter in event.parameters %}
{% if "optional" in parameter %} {% if "optional" in parameter %}
if ({{parameter.name}}.isJust()) if ({{parameter.name}}.isJust())
paramsObject->setValue("{{parameter.name}}", toValue({{parameter.name}}.fromJust())); paramsObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_type(parameter).raw_type}}>::serialize({{parameter.name}}.fromJust()));
{% else %} {% else %}
paramsObject->setValue("{{parameter.name}}", toValue({{resolve_type(parameter).to_raw_type % parameter.name}})); paramsObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_type(parameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % parameter.name}}));
{% endif %} {% endif %}
{% endfor %} {% endfor %}
jsonMessage->setObject("params", std::move(paramsObject)); jsonMessage->setObject("params", std::move(paramsObject));
...@@ -196,9 +196,9 @@ public: ...@@ -196,9 +196,9 @@ public:
{% for parameter in command.returns %} {% for parameter in command.returns %}
{% if "optional" in parameter %} {% if "optional" in parameter %}
if ({{parameter.name}}.isJust()) if ({{parameter.name}}.isJust())
resultObject->setValue("{{parameter.name}}", toValue({{parameter.name}}.fromJust())); resultObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_type(parameter).raw_type}}>::serialize({{parameter.name}}.fromJust()));
{% else %} {% else %}
resultObject->setValue("{{parameter.name}}", toValue({{resolve_type(parameter).to_raw_type % parameter.name}})); resultObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_type(parameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % parameter.name}}));
{% endif %} {% endif %}
{% endfor %} {% endfor %}
sendIfActive(std::move(resultObject), ErrorString()); sendIfActive(std::move(resultObject), ErrorString());
...@@ -225,11 +225,11 @@ void DispatcherImpl::{{command.name}}(int callId, std::unique_ptr<DictionaryValu ...@@ -225,11 +225,11 @@ void DispatcherImpl::{{command.name}}(int callId, std::unique_ptr<DictionaryValu
Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}}; Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}};
if ({{property.name}}Value) { if ({{property.name}}Value) {
errors->setName("{{property.name}}"); errors->setName("{{property.name}}");
in_{{property.name}} = FromValue<{{resolve_type(property).raw_type}}>::parse({{property.name}}Value, errors); in_{{property.name}} = ValueConversions<{{resolve_type(property).raw_type}}>::parse({{property.name}}Value, errors);
} }
{% else %} {% else %}
errors->setName("{{property.name}}"); errors->setName("{{property.name}}");
{{resolve_type(property).type}} in_{{property.name}} = FromValue<{{resolve_type(property).raw_type}}>::parse({{property.name}}Value, errors); {{resolve_type(property).type}} in_{{property.name}} = ValueConversions<{{resolve_type(property).raw_type}}>::parse({{property.name}}Value, errors);
{% endif %} {% endif %}
{% endfor %} {% endfor %}
errors->pop(); errors->pop();
...@@ -274,9 +274,9 @@ void DispatcherImpl::{{command.name}}(int callId, std::unique_ptr<DictionaryValu ...@@ -274,9 +274,9 @@ void DispatcherImpl::{{command.name}}(int callId, std::unique_ptr<DictionaryValu
{% for parameter in command.returns %} {% for parameter in command.returns %}
{% if "optional" in parameter %} {% if "optional" in parameter %}
if (out_{{parameter.name}}.isJust()) if (out_{{parameter.name}}.isJust())
result->setValue("{{parameter.name}}", toValue(out_{{parameter.name}}.fromJust())); result->setValue("{{parameter.name}}", ValueConversions<{{resolve_type(parameter).raw_type}}>::serialize(out_{{parameter.name}}.fromJust()));
{% else %} {% else %}
result->setValue("{{parameter.name}}", toValue({{resolve_type(parameter).to_raw_type % ("out_" + parameter.name)}})); result->setValue("{{parameter.name}}", ValueConversions<{{resolve_type(parameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % ("out_" + parameter.name)}}));
{% endif %} {% endif %}
{% endfor %} {% endfor %}
} }
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "platform/inspector_protocol/ValueConversions.h"
namespace blink {
namespace protocol {
std::unique_ptr<protocol::Value> toValue(int value)
{
return FundamentalValue::create(value);
}
std::unique_ptr<protocol::Value> toValue(double value)
{
return FundamentalValue::create(value);
}
std::unique_ptr<protocol::Value> toValue(bool value)
{
return FundamentalValue::create(value);
}
std::unique_ptr<protocol::Value> toValue(const String16& param)
{
return StringValue::create(param);
}
std::unique_ptr<protocol::Value> toValue(const String& param)
{
return StringValue::create(param);
}
std::unique_ptr<protocol::Value> toValue(Value* param)
{
return param->clone();
}
std::unique_ptr<protocol::Value> toValue(DictionaryValue* param)
{
return param->clone();
}
std::unique_ptr<protocol::Value> toValue(ListValue* param)
{
return param->clone();
}
} // namespace protocol
} // namespace blink
...@@ -13,42 +13,26 @@ ...@@ -13,42 +13,26 @@
namespace blink { namespace blink {
namespace protocol { namespace protocol {
PLATFORM_EXPORT std::unique_ptr<protocol::Value> toValue(int value);
PLATFORM_EXPORT std::unique_ptr<protocol::Value> toValue(double value);
PLATFORM_EXPORT std::unique_ptr<protocol::Value> toValue(bool value);
PLATFORM_EXPORT std::unique_ptr<protocol::Value> toValue(const String16& param);
PLATFORM_EXPORT std::unique_ptr<protocol::Value> toValue(const String& param);
PLATFORM_EXPORT std::unique_ptr<protocol::Value> toValue(protocol::Value* param);
PLATFORM_EXPORT std::unique_ptr<protocol::Value> toValue(protocol::DictionaryValue* param);
PLATFORM_EXPORT std::unique_ptr<protocol::Value> toValue(protocol::ListValue* param);
template<typename T> std::unique_ptr<protocol::Value> toValue(T* param)
{
return param->serialize();
}
template<typename T> std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<T>& param)
{
return toValue(param.get());
}
template<typename T> template<typename T>
struct FromValue { struct ValueConversions {
static std::unique_ptr<T> parse(protocol::Value* value, ErrorSupport* errors) static std::unique_ptr<T> parse(protocol::Value* value, ErrorSupport* errors)
{ {
return T::parse(value, errors); return T::parse(value, errors);
} }
static std::unique_ptr<protocol::Value> serialize(T* value)
{
return value->serialize();
}
static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<T>& value)
{
return value->serialize();
}
}; };
template<> template<>
struct FromValue<bool> { struct ValueConversions<bool> {
static bool parse(protocol::Value* value, ErrorSupport* errors) static bool parse(protocol::Value* value, ErrorSupport* errors)
{ {
bool result = false; bool result = false;
...@@ -57,10 +41,15 @@ struct FromValue<bool> { ...@@ -57,10 +41,15 @@ struct FromValue<bool> {
errors->addError("boolean value expected"); errors->addError("boolean value expected");
return result; return result;
} }
static std::unique_ptr<protocol::Value> serialize(bool value)
{
return FundamentalValue::create(value);
}
}; };
template<> template<>
struct FromValue<int> { struct ValueConversions<int> {
static int parse(protocol::Value* value, ErrorSupport* errors) static int parse(protocol::Value* value, ErrorSupport* errors)
{ {
int result = 0; int result = 0;
...@@ -69,10 +58,15 @@ struct FromValue<int> { ...@@ -69,10 +58,15 @@ struct FromValue<int> {
errors->addError("integer value expected"); errors->addError("integer value expected");
return result; return result;
} }
static std::unique_ptr<protocol::Value> serialize(int value)
{
return FundamentalValue::create(value);
}
}; };
template<> template<>
struct FromValue<double> { struct ValueConversions<double> {
static double parse(protocol::Value* value, ErrorSupport* errors) static double parse(protocol::Value* value, ErrorSupport* errors)
{ {
double result = 0; double result = 0;
...@@ -81,10 +75,15 @@ struct FromValue<double> { ...@@ -81,10 +75,15 @@ struct FromValue<double> {
errors->addError("double value expected"); errors->addError("double value expected");
return result; return result;
} }
static std::unique_ptr<protocol::Value> serialize(double value)
{
return FundamentalValue::create(value);
}
}; };
template<> template<>
struct FromValue<String> { struct ValueConversions<String> {
static String parse(protocol::Value* value, ErrorSupport* errors) static String parse(protocol::Value* value, ErrorSupport* errors)
{ {
String16 result; String16 result;
...@@ -93,10 +92,15 @@ struct FromValue<String> { ...@@ -93,10 +92,15 @@ struct FromValue<String> {
errors->addError("string value expected"); errors->addError("string value expected");
return result; return result;
} }
static std::unique_ptr<protocol::Value> serialize(const String& value)
{
return StringValue::create(value);
}
}; };
template<> template<>
struct FromValue<String16> { struct ValueConversions<String16> {
static String16 parse(protocol::Value* value, ErrorSupport* errors) static String16 parse(protocol::Value* value, ErrorSupport* errors)
{ {
String16 result; String16 result;
...@@ -105,10 +109,15 @@ struct FromValue<String16> { ...@@ -105,10 +109,15 @@ struct FromValue<String16> {
errors->addError("string value expected"); errors->addError("string value expected");
return result; return result;
} }
static std::unique_ptr<protocol::Value> serialize(const String16& value)
{
return StringValue::create(value);
}
}; };
template<> template<>
struct FromValue<Value> { struct ValueConversions<Value> {
static std::unique_ptr<Value> parse(protocol::Value* value, ErrorSupport* errors) static std::unique_ptr<Value> parse(protocol::Value* value, ErrorSupport* errors)
{ {
bool success = !!value; bool success = !!value;
...@@ -118,10 +127,20 @@ struct FromValue<Value> { ...@@ -118,10 +127,20 @@ struct FromValue<Value> {
} }
return value->clone(); return value->clone();
} }
static std::unique_ptr<protocol::Value> serialize(Value* value)
{
return value->clone();
}
static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<Value>& value)
{
return value->clone();
}
}; };
template<> template<>
struct FromValue<DictionaryValue> { struct ValueConversions<DictionaryValue> {
static std::unique_ptr<DictionaryValue> parse(protocol::Value* value, ErrorSupport* errors) static std::unique_ptr<DictionaryValue> parse(protocol::Value* value, ErrorSupport* errors)
{ {
bool success = value && value->type() == protocol::Value::TypeObject; bool success = value && value->type() == protocol::Value::TypeObject;
...@@ -129,10 +148,20 @@ struct FromValue<DictionaryValue> { ...@@ -129,10 +148,20 @@ struct FromValue<DictionaryValue> {
errors->addError("object expected"); errors->addError("object expected");
return DictionaryValue::cast(value->clone()); return DictionaryValue::cast(value->clone());
} }
static std::unique_ptr<protocol::Value> serialize(DictionaryValue* value)
{
return value->clone();
}
static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<DictionaryValue>& value)
{
return value->clone();
}
}; };
template<> template<>
struct FromValue<ListValue> { struct ValueConversions<ListValue> {
static std::unique_ptr<ListValue> parse(protocol::Value* value, ErrorSupport* errors) static std::unique_ptr<ListValue> parse(protocol::Value* value, ErrorSupport* errors)
{ {
bool success = value && value->type() == protocol::Value::TypeArray; bool success = value && value->type() == protocol::Value::TypeArray;
...@@ -140,15 +169,15 @@ struct FromValue<ListValue> { ...@@ -140,15 +169,15 @@ struct FromValue<ListValue> {
errors->addError("list expected"); errors->addError("list expected");
return ListValue::cast(value->clone()); return ListValue::cast(value->clone());
} }
};
template<typename T> class Array; static std::unique_ptr<protocol::Value> serialize(ListValue* value)
{
return value->clone();
}
template<typename T> static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<ListValue>& value)
struct FromValue<protocol::Array<T>> {
static std::unique_ptr<protocol::Array<T>> parse(protocol::Value* value, ErrorSupport* errors)
{ {
return protocol::Array<T>::parse(value, errors); return value->clone();
} }
}; };
......
...@@ -177,7 +177,6 @@ ...@@ -177,7 +177,6 @@
'../inspector_protocol/String16STL.h', '../inspector_protocol/String16STL.h',
'../inspector_protocol/Values.cpp', '../inspector_protocol/Values.cpp',
'../inspector_protocol/Values.h', '../inspector_protocol/Values.h',
'../inspector_protocol/ValueConversions.cpp',
'../inspector_protocol/ValueConversions.h', '../inspector_protocol/ValueConversions.h',
'Atomics.h', 'Atomics.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment