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)
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)
{
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)
{
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)
{
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)
......
......@@ -857,7 +857,6 @@
'inspector_protocol/String16WTF.h',
'inspector_protocol/Values.cpp',
'inspector_protocol/Values.h',
'inspector_protocol/ValueConversions.cpp',
'inspector_protocol/ValueConversions.h',
'mac/BlockExceptions.h',
'mac/BlockExceptions.mm',
......
......@@ -17,7 +17,7 @@ namespace blink {
namespace protocol {
template<typename T>
class ArrayBase {
class Array {
public:
static std::unique_ptr<Array<T>> create()
{
......@@ -31,12 +31,12 @@ public:
errors->addError("array expected");
return nullptr;
}
errors->push();
std::unique_ptr<Array<T>> result(new Array<T>());
errors->push();
for (size_t i = 0; i < array->size(); ++i) {
errors->setName(String16::fromInteger(i));
T item = FromValue<T>::parse(array->at(i), errors);
result->m_vector.push_back(item);
std::unique_ptr<T> item = ValueConversions<T>::parse(array->at(i), errors);
result->m_vector.push_back(std::move(item));
}
errors->pop();
if (errors->hasErrors())
......@@ -44,9 +44,9 @@ public:
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()
......@@ -54,31 +54,25 @@ public:
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> result = ListValue::create();
for (auto& item : m_vector)
result->pushValue(toValue(item));
result->pushValue(ValueConversions<T>::serialize(item));
return result;
}
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>
class Array {
class ArrayBase {
public:
static std::unique_ptr<Array<T>> create()
{
......@@ -92,12 +86,12 @@ public:
errors->addError("array expected");
return nullptr;
}
std::unique_ptr<Array<T>> result(new Array<T>());
errors->push();
std::unique_ptr<Array<T>> result(new Array<T>());
for (size_t i = 0; i < array->size(); ++i) {
errors->setName(String16::fromInteger(i));
std::unique_ptr<T> item = FromValue<T>::parse(array->at(i), errors);
result->m_vector.push_back(std::move(item));
T item = ValueConversions<T>::parse(array->at(i), errors);
result->m_vector.push_back(item);
}
errors->pop();
if (errors->hasErrors())
......@@ -105,9 +99,9 @@ public:
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()
......@@ -115,23 +109,29 @@ public:
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> result = ListValue::create();
for (auto& item : m_vector)
result->pushValue(toValue(item));
result->pushValue(ValueConversions<T>::serialize(item));
return result;
}
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 blink
......
......@@ -49,11 +49,11 @@ std::unique_ptr<{{type.id}}> {{type.id}}::parse(protocol::Value* value, ErrorSup
{% if property.optional %}
if ({{property.name}}Value) {
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 %}
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 %}
{% endfor %}
errors->pop();
......@@ -68,9 +68,9 @@ std::unique_ptr<protocol::DictionaryValue> {{type.id}}::serialize() const
{% for property in type.properties %}
{% if property.optional %}
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 %}
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 %}
{% endfor %}
return result;
......@@ -119,9 +119,9 @@ void Frontend::{{event.name}}(
{% for parameter in event.parameters %}
{% if "optional" in parameter %}
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 %}
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 %}
{% endfor %}
jsonMessage->setObject("params", std::move(paramsObject));
......@@ -196,9 +196,9 @@ public:
{% for parameter in command.returns %}
{% if "optional" in parameter %}
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 %}
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 %}
{% endfor %}
sendIfActive(std::move(resultObject), ErrorString());
......@@ -225,11 +225,11 @@ void DispatcherImpl::{{command.name}}(int callId, std::unique_ptr<DictionaryValu
Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}};
if ({{property.name}}Value) {
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 %}
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 %}
{% endfor %}
errors->pop();
......@@ -274,9 +274,9 @@ void DispatcherImpl::{{command.name}}(int callId, std::unique_ptr<DictionaryValu
{% for parameter in command.returns %}
{% if "optional" in parameter %}
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 %}
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 %}
{% 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 @@
namespace blink {
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>
struct FromValue {
struct ValueConversions {
static std::unique_ptr<T> parse(protocol::Value* value, ErrorSupport* 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<>
struct FromValue<bool> {
struct ValueConversions<bool> {
static bool parse(protocol::Value* value, ErrorSupport* errors)
{
bool result = false;
......@@ -57,10 +41,15 @@ struct FromValue<bool> {
errors->addError("boolean value expected");
return result;
}
static std::unique_ptr<protocol::Value> serialize(bool value)
{
return FundamentalValue::create(value);
}
};
template<>
struct FromValue<int> {
struct ValueConversions<int> {
static int parse(protocol::Value* value, ErrorSupport* errors)
{
int result = 0;
......@@ -69,10 +58,15 @@ struct FromValue<int> {
errors->addError("integer value expected");
return result;
}
static std::unique_ptr<protocol::Value> serialize(int value)
{
return FundamentalValue::create(value);
}
};
template<>
struct FromValue<double> {
struct ValueConversions<double> {
static double parse(protocol::Value* value, ErrorSupport* errors)
{
double result = 0;
......@@ -81,10 +75,15 @@ struct FromValue<double> {
errors->addError("double value expected");
return result;
}
static std::unique_ptr<protocol::Value> serialize(double value)
{
return FundamentalValue::create(value);
}
};
template<>
struct FromValue<String> {
struct ValueConversions<String> {
static String parse(protocol::Value* value, ErrorSupport* errors)
{
String16 result;
......@@ -93,10 +92,15 @@ struct FromValue<String> {
errors->addError("string value expected");
return result;
}
static std::unique_ptr<protocol::Value> serialize(const String& value)
{
return StringValue::create(value);
}
};
template<>
struct FromValue<String16> {
struct ValueConversions<String16> {
static String16 parse(protocol::Value* value, ErrorSupport* errors)
{
String16 result;
......@@ -105,10 +109,15 @@ struct FromValue<String16> {
errors->addError("string value expected");
return result;
}
static std::unique_ptr<protocol::Value> serialize(const String16& value)
{
return StringValue::create(value);
}
};
template<>
struct FromValue<Value> {
struct ValueConversions<Value> {
static std::unique_ptr<Value> parse(protocol::Value* value, ErrorSupport* errors)
{
bool success = !!value;
......@@ -118,10 +127,20 @@ struct FromValue<Value> {
}
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<>
struct FromValue<DictionaryValue> {
struct ValueConversions<DictionaryValue> {
static std::unique_ptr<DictionaryValue> parse(protocol::Value* value, ErrorSupport* errors)
{
bool success = value && value->type() == protocol::Value::TypeObject;
......@@ -129,10 +148,20 @@ struct FromValue<DictionaryValue> {
errors->addError("object expected");
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<>
struct FromValue<ListValue> {
struct ValueConversions<ListValue> {
static std::unique_ptr<ListValue> parse(protocol::Value* value, ErrorSupport* errors)
{
bool success = value && value->type() == protocol::Value::TypeArray;
......@@ -140,15 +169,15 @@ struct FromValue<ListValue> {
errors->addError("list expected");
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>
struct FromValue<protocol::Array<T>> {
static std::unique_ptr<protocol::Array<T>> parse(protocol::Value* value, ErrorSupport* errors)
static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<ListValue>& value)
{
return protocol::Array<T>::parse(value, errors);
return value->clone();
}
};
......
......@@ -177,7 +177,6 @@
'../inspector_protocol/String16STL.h',
'../inspector_protocol/Values.cpp',
'../inspector_protocol/Values.h',
'../inspector_protocol/ValueConversions.cpp',
'../inspector_protocol/ValueConversions.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