[DevTools] Unify toValue and FromValue into ValueConversions.

BUG=none
TBR=pfeldman

Review-Url: https://codereview.chromium.org/2164353002
Cr-Original-Commit-Position: refs/heads/master@{#407027}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: dc5d0632171363ab683aa84754db3b35d6ed39a8
diff --git a/Array.h b/Array.h
index d92af3a..7201822 100644
--- a/Array.h
+++ b/Array.h
@@ -17,67 +17,6 @@
 namespace protocol {
 
 template<typename T>
-class ArrayBase {
-public:
-    static std::unique_ptr<Array<T>> create()
-    {
-        return wrapUnique(new Array<T>());
-    }
-
-    static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport* errors)
-    {
-        protocol::ListValue* array = ListValue::cast(value);
-        if (!array) {
-            errors->addError("array expected");
-            return nullptr;
-        }
-        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));
-            T item = FromValue<T>::parse(array->at(i), errors);
-            result->m_vector.push_back(item);
-        }
-        errors->pop();
-        if (errors->hasErrors())
-            return nullptr;
-        return result;
-    }
-
-    void addItem(const T& value)
-    {
-        m_vector.push_back(value);
-    }
-
-    size_t length()
-    {
-        return m_vector.size();
-    }
-
-    T get(size_t index)
-    {
-        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));
-        return result;
-    }
-
-private:
-    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> {};
-
-template<typename T>
 class Array {
 public:
     static std::unique_ptr<Array<T>> create()
@@ -96,7 +35,7 @@
         errors->push();
         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);
+            std::unique_ptr<T> item = ValueConversions<T>::parse(array->at(i), errors);
             result->m_vector.push_back(std::move(item));
         }
         errors->pop();
@@ -124,7 +63,7 @@
     {
         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;
     }
 
@@ -132,6 +71,67 @@
     std::vector<std::unique_ptr<T>> m_vector;
 };
 
+template<typename T>
+class ArrayBase {
+public:
+    static std::unique_ptr<Array<T>> create()
+    {
+        return wrapUnique(new Array<T>());
+    }
+
+    static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport* errors)
+    {
+        protocol::ListValue* array = ListValue::cast(value);
+        if (!array) {
+            errors->addError("array expected");
+            return nullptr;
+        }
+        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));
+            T item = ValueConversions<T>::parse(array->at(i), errors);
+            result->m_vector.push_back(item);
+        }
+        errors->pop();
+        if (errors->hasErrors())
+            return nullptr;
+        return result;
+    }
+
+    void addItem(const T& value)
+    {
+        m_vector.push_back(value);
+    }
+
+    size_t length()
+    {
+        return m_vector.size();
+    }
+
+    T get(size_t index)
+    {
+        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(ValueConversions<T>::serialize(item));
+        return result;
+    }
+
+private:
+    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
 
diff --git a/TypeBuilder_cpp.template b/TypeBuilder_cpp.template
index 909fea4..f1a252c 100644
--- a/TypeBuilder_cpp.template
+++ b/TypeBuilder_cpp.template
@@ -49,11 +49,11 @@
       {% 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 @@
     {% 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 @@
     {% 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 @@
           {% 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 @@
     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 @@
       {% 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 %}
     }
diff --git a/ValueConversions.cpp b/ValueConversions.cpp
deleted file mode 100644
index 98b8e7f..0000000
--- a/ValueConversions.cpp
+++ /dev/null
@@ -1,51 +0,0 @@
-// 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
diff --git a/ValueConversions.h b/ValueConversions.h
index dd7b2e7..ba1d080 100644
--- a/ValueConversions.h
+++ b/ValueConversions.h
@@ -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 @@
             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 @@
             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 @@
             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 @@
             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 @@
             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 @@
         }
         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 @@
             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 @@
             errors->addError("list expected");
         return ListValue::cast(value->clone());
     }
-};
 
-template<typename T> class Array;
-
-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(ListValue* value)
     {
-        return protocol::Array<T>::parse(value, errors);
+        return value->clone();
+    }
+
+    static std::unique_ptr<protocol::Value> serialize(const std::unique_ptr<ListValue>& value)
+    {
+        return value->clone();
     }
 };