Commit 93ca2893 authored by Daniel Bratell's avatar Daniel Bratell Committed by Commit Bot

Retire ToValueImpl and rely on ToValue for headless devtools

The previous ToValue -> ToValueImpl implementation caused
template resolution complexities and prevented jumbo support
by having a clone of the same template in every domain_types_cc
file.

Bug: 883727
Change-Id: Iec49d7bde93515fe180f26073cea59db3cf4be82
Reviewed-on: https://chromium-review.googlesource.com/1236355Reviewed-by: default avatarSami Kyöstilä <skyostil@chromium.org>
Reviewed-by: default avatarAndrey Kosyakov <caseq@chromium.org>
Commit-Queue: Daniel Bratell <bratell@opera.com>
Cr-Commit-Position: refs/heads/master@{#593104}
parent a5bcea03
...@@ -34,8 +34,8 @@ struct FromValue<{{namespace}}::{{type.id}}> { ...@@ -34,8 +34,8 @@ struct FromValue<{{namespace}}::{{type.id}}> {
} }
}; };
template <typename T> template <>
std::unique_ptr<base::Value> ToValueImpl(const {{namespace}}::{{type.id}}& value, T*) { inline std::unique_ptr<base::Value> ToValue(const {{namespace}}::{{type.id}}& value) {
switch (value) { switch (value) {
{% for literal in type.enum %} {% for literal in type.enum %}
case {{namespace}}::{{type.id}}::{{literal | sanitize_literal | dash_to_camelcase | camelcase_to_hacker_style | upper }}: case {{namespace}}::{{type.id}}::{{literal | sanitize_literal | dash_to_camelcase | camelcase_to_hacker_style | upper }}:
...@@ -56,8 +56,8 @@ struct FromValue<{{namespace}}::{{type.id}}> { ...@@ -56,8 +56,8 @@ struct FromValue<{{namespace}}::{{type.id}}> {
} }
}; };
template <typename T> template <>
std::unique_ptr<base::Value> ToValueImpl(const {{namespace}}::{{type.id}}& value, T*) { inline std::unique_ptr<base::Value> ToValue(const {{namespace}}::{{type.id}}& value) {
return value.Serialize(); return value.Serialize();
} }
......
...@@ -15,15 +15,6 @@ ...@@ -15,15 +15,6 @@
namespace headless { namespace headless {
namespace internal {
template <typename T>
std::unique_ptr<base::Value> ToValue(const T& value) {
return ToValueImpl(value, static_cast<T*>(nullptr));
}
} // namespace internal
namespace {{domain.domain | camelcase_to_hacker_style}} { namespace {{domain.domain | camelcase_to_hacker_style}} {
{% for type in domain.types %} {% for type in domain.types %}
{% if not (type.type == "object") or not ("properties" in type) %}{% continue %}{% endif %} {% if not (type.type == "object") or not ("properties" in type) %}{% continue %}{% endif %}
......
...@@ -12,8 +12,8 @@ ...@@ -12,8 +12,8 @@
namespace headless { namespace headless {
namespace internal { namespace internal {
// Generic conversion from a type to a base::Value. Implemented in // Generic conversion from a type to a base::Value. Implemented below
// types_DOMAIN.cc after all type-specific ToValueImpls have been defined. // (for composite and low level types) and and in types_DOMAIN.cc.
template <typename T> template <typename T>
std::unique_ptr<base::Value> ToValue(const T& value); std::unique_ptr<base::Value> ToValue(const T& value);
...@@ -26,49 +26,52 @@ struct FromValue { ...@@ -26,49 +26,52 @@ struct FromValue {
ErrorReporter* errors); ErrorReporter* errors);
}; };
// ToValueImpl is a helper used by the ToValue template for dispatching into template <>
// type-specific serializers. It uses a dummy |T*| argument as a way to inline std::unique_ptr<base::Value> ToValue(const int& value) {
// partially specialize vector types.
template <typename T>
std::unique_ptr<base::Value> ToValueImpl(int value, T*) {
return std::make_unique<base::Value>(value); return std::make_unique<base::Value>(value);
} }
template <typename T> template <>
std::unique_ptr<base::Value> ToValueImpl(double value, T*) { inline std::unique_ptr<base::Value> ToValue(const double& value) {
return std::make_unique<base::Value>(value); return std::make_unique<base::Value>(value);
} }
template <typename T> template <>
std::unique_ptr<base::Value> ToValueImpl(bool value, T*) { inline std::unique_ptr<base::Value> ToValue(const bool& value) {
return std::make_unique<base::Value>(value); return std::make_unique<base::Value>(value);
} }
template <typename T> template <>
std::unique_ptr<base::Value> ToValueImpl(const std::string& value, T*) { inline std::unique_ptr<base::Value> ToValue(const std::string& value) {
return std::make_unique<base::Value>(value); return std::make_unique<base::Value>(value);
} }
template <typename T> template <>
std::unique_ptr<base::Value> ToValueImpl(const base::Value& value, T*) { inline std::unique_ptr<base::Value> ToValue(const base::Value& value) {
return value.CreateDeepCopy(); return value.CreateDeepCopy();
} }
template <typename T> template <>
std::unique_ptr<base::Value> ToValueImpl(const std::vector<T>& vector, inline std::unique_ptr<base::Value> ToValue(
const std::vector<T>*) { const base::DictionaryValue& value) {
std::unique_ptr<base::ListValue> result(new base::ListValue()); return ToValue(static_cast<const base::Value&>(value));
for (const auto& it : vector)
result->Append(ToValue(it));
return std::move(result);
} }
// Note: Order of the two templates below is important to handle
// vectors of unique_ptr.
template <typename T> template <typename T>
std::unique_ptr<base::Value> ToValueImpl(const std::unique_ptr<T>& value, std::unique_ptr<base::Value> ToValue(const std::unique_ptr<T>& value) {
std::unique_ptr<T>*) {
return ToValue(*value); return ToValue(*value);
} }
template <typename T>
std::unique_ptr<base::Value> ToValue(const std::vector<T>& vector_of_values) {
std::unique_ptr<base::ListValue> result(new base::ListValue());
for (const T& value : vector_of_values)
result->Append(ToValue(value));
return std::move(result);
}
// FromValue specializations for basic types. // FromValue specializations for basic types.
template <> template <>
struct FromValue<bool> { struct FromValue<bool> {
......
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