11cb0ef41Sopenharmony_ci// This file is generated by ValueConversions_h.template.
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Copyright 2016 The Chromium Authors. All rights reserved.
41cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
51cb0ef41Sopenharmony_ci// found in the LICENSE file.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#ifndef {{"_".join(config.protocol.namespace)}}_ValueConversions_h
81cb0ef41Sopenharmony_ci#define {{"_".join(config.protocol.namespace)}}_ValueConversions_h
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci//#include "ErrorSupport.h"
111cb0ef41Sopenharmony_ci//#include "Forward.h"
121cb0ef41Sopenharmony_ci//#include "Values.h"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %}
151cb0ef41Sopenharmony_cinamespace {{namespace}} {
161cb0ef41Sopenharmony_ci{% endfor %}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_citemplate<typename T>
191cb0ef41Sopenharmony_cistruct ValueConversions {
201cb0ef41Sopenharmony_ci    static std::unique_ptr<T> fromValue(protocol::Value* value, ErrorSupport* errors)
211cb0ef41Sopenharmony_ci    {
221cb0ef41Sopenharmony_ci        return T::fromValue(value, errors);
231cb0ef41Sopenharmony_ci    }
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci    static std::unique_ptr<protocol::Value> toValue(T* value)
261cb0ef41Sopenharmony_ci    {
271cb0ef41Sopenharmony_ci        return value->toValue();
281cb0ef41Sopenharmony_ci    }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci    static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<T>& value)
311cb0ef41Sopenharmony_ci    {
321cb0ef41Sopenharmony_ci        return value->toValue();
331cb0ef41Sopenharmony_ci    }
341cb0ef41Sopenharmony_ci};
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_citemplate<>
371cb0ef41Sopenharmony_cistruct ValueConversions<bool> {
381cb0ef41Sopenharmony_ci    static bool fromValue(protocol::Value* value, ErrorSupport* errors)
391cb0ef41Sopenharmony_ci    {
401cb0ef41Sopenharmony_ci        bool result = false;
411cb0ef41Sopenharmony_ci        bool success = value ? value->asBoolean(&result) : false;
421cb0ef41Sopenharmony_ci        if (!success)
431cb0ef41Sopenharmony_ci            errors->addError("boolean value expected");
441cb0ef41Sopenharmony_ci        return result;
451cb0ef41Sopenharmony_ci    }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci    static std::unique_ptr<protocol::Value> toValue(bool value)
481cb0ef41Sopenharmony_ci    {
491cb0ef41Sopenharmony_ci        return FundamentalValue::create(value);
501cb0ef41Sopenharmony_ci    }
511cb0ef41Sopenharmony_ci};
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_citemplate<>
541cb0ef41Sopenharmony_cistruct ValueConversions<int> {
551cb0ef41Sopenharmony_ci    static int fromValue(protocol::Value* value, ErrorSupport* errors)
561cb0ef41Sopenharmony_ci    {
571cb0ef41Sopenharmony_ci        int result = 0;
581cb0ef41Sopenharmony_ci        bool success = value ? value->asInteger(&result) : false;
591cb0ef41Sopenharmony_ci        if (!success)
601cb0ef41Sopenharmony_ci            errors->addError("integer value expected");
611cb0ef41Sopenharmony_ci        return result;
621cb0ef41Sopenharmony_ci    }
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    static std::unique_ptr<protocol::Value> toValue(int value)
651cb0ef41Sopenharmony_ci    {
661cb0ef41Sopenharmony_ci        return FundamentalValue::create(value);
671cb0ef41Sopenharmony_ci    }
681cb0ef41Sopenharmony_ci};
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_citemplate<>
711cb0ef41Sopenharmony_cistruct ValueConversions<double> {
721cb0ef41Sopenharmony_ci    static double fromValue(protocol::Value* value, ErrorSupport* errors)
731cb0ef41Sopenharmony_ci    {
741cb0ef41Sopenharmony_ci        double result = 0;
751cb0ef41Sopenharmony_ci        bool success = value ? value->asDouble(&result) : false;
761cb0ef41Sopenharmony_ci        if (!success)
771cb0ef41Sopenharmony_ci            errors->addError("double value expected");
781cb0ef41Sopenharmony_ci        return result;
791cb0ef41Sopenharmony_ci    }
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci    static std::unique_ptr<protocol::Value> toValue(double value)
821cb0ef41Sopenharmony_ci    {
831cb0ef41Sopenharmony_ci        return FundamentalValue::create(value);
841cb0ef41Sopenharmony_ci    }
851cb0ef41Sopenharmony_ci};
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_citemplate<>
881cb0ef41Sopenharmony_cistruct ValueConversions<String> {
891cb0ef41Sopenharmony_ci    static String fromValue(protocol::Value* value, ErrorSupport* errors)
901cb0ef41Sopenharmony_ci    {
911cb0ef41Sopenharmony_ci        String result;
921cb0ef41Sopenharmony_ci        bool success = value ? value->asString(&result) : false;
931cb0ef41Sopenharmony_ci        if (!success)
941cb0ef41Sopenharmony_ci            errors->addError("string value expected");
951cb0ef41Sopenharmony_ci        return result;
961cb0ef41Sopenharmony_ci    }
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci    static std::unique_ptr<protocol::Value> toValue(const String& value)
991cb0ef41Sopenharmony_ci    {
1001cb0ef41Sopenharmony_ci        return StringValue::create(value);
1011cb0ef41Sopenharmony_ci    }
1021cb0ef41Sopenharmony_ci};
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_citemplate<>
1051cb0ef41Sopenharmony_cistruct ValueConversions<Binary> {
1061cb0ef41Sopenharmony_ci    static Binary fromValue(protocol::Value* value, ErrorSupport* errors)
1071cb0ef41Sopenharmony_ci    {
1081cb0ef41Sopenharmony_ci        if (!value ||
1091cb0ef41Sopenharmony_ci            (value->type() != Value::TypeBinary && value->type() != Value::TypeString)) {
1101cb0ef41Sopenharmony_ci            errors->addError("Either string base64 or binary value expected");
1111cb0ef41Sopenharmony_ci            return Binary();
1121cb0ef41Sopenharmony_ci        }
1131cb0ef41Sopenharmony_ci        Binary binary;
1141cb0ef41Sopenharmony_ci        if (value->asBinary(&binary))
1151cb0ef41Sopenharmony_ci            return binary;
1161cb0ef41Sopenharmony_ci        String result;
1171cb0ef41Sopenharmony_ci        value->asString(&result);
1181cb0ef41Sopenharmony_ci        bool success;
1191cb0ef41Sopenharmony_ci        Binary out = Binary::fromBase64(result, &success);
1201cb0ef41Sopenharmony_ci        if (!success)
1211cb0ef41Sopenharmony_ci          errors->addError("base64 decoding error");
1221cb0ef41Sopenharmony_ci        return out;
1231cb0ef41Sopenharmony_ci    }
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci    static std::unique_ptr<protocol::Value> toValue(const Binary& value)
1261cb0ef41Sopenharmony_ci    {
1271cb0ef41Sopenharmony_ci        return BinaryValue::create(value);
1281cb0ef41Sopenharmony_ci    }
1291cb0ef41Sopenharmony_ci};
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_citemplate<>
1321cb0ef41Sopenharmony_cistruct ValueConversions<Value> {
1331cb0ef41Sopenharmony_ci    static std::unique_ptr<Value> fromValue(protocol::Value* value, ErrorSupport* errors)
1341cb0ef41Sopenharmony_ci    {
1351cb0ef41Sopenharmony_ci        bool success = !!value;
1361cb0ef41Sopenharmony_ci        if (!success) {
1371cb0ef41Sopenharmony_ci            errors->addError("value expected");
1381cb0ef41Sopenharmony_ci            return nullptr;
1391cb0ef41Sopenharmony_ci        }
1401cb0ef41Sopenharmony_ci        return value->clone();
1411cb0ef41Sopenharmony_ci    }
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci    static std::unique_ptr<protocol::Value> toValue(Value* value)
1441cb0ef41Sopenharmony_ci    {
1451cb0ef41Sopenharmony_ci        return value->clone();
1461cb0ef41Sopenharmony_ci    }
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci    static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<Value>& value)
1491cb0ef41Sopenharmony_ci    {
1501cb0ef41Sopenharmony_ci        return value->clone();
1511cb0ef41Sopenharmony_ci    }
1521cb0ef41Sopenharmony_ci};
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_citemplate<>
1551cb0ef41Sopenharmony_cistruct ValueConversions<DictionaryValue> {
1561cb0ef41Sopenharmony_ci    static std::unique_ptr<DictionaryValue> fromValue(protocol::Value* value, ErrorSupport* errors)
1571cb0ef41Sopenharmony_ci    {
1581cb0ef41Sopenharmony_ci        bool success = value && value->type() == protocol::Value::TypeObject;
1591cb0ef41Sopenharmony_ci        if (!success)
1601cb0ef41Sopenharmony_ci            errors->addError("object expected");
1611cb0ef41Sopenharmony_ci        return DictionaryValue::cast(value->clone());
1621cb0ef41Sopenharmony_ci    }
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci    static std::unique_ptr<protocol::Value> toValue(DictionaryValue* value)
1651cb0ef41Sopenharmony_ci    {
1661cb0ef41Sopenharmony_ci        return value->clone();
1671cb0ef41Sopenharmony_ci    }
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci    static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<DictionaryValue>& value)
1701cb0ef41Sopenharmony_ci    {
1711cb0ef41Sopenharmony_ci        return value->clone();
1721cb0ef41Sopenharmony_ci    }
1731cb0ef41Sopenharmony_ci};
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_citemplate<>
1761cb0ef41Sopenharmony_cistruct ValueConversions<ListValue> {
1771cb0ef41Sopenharmony_ci    static std::unique_ptr<ListValue> fromValue(protocol::Value* value, ErrorSupport* errors)
1781cb0ef41Sopenharmony_ci    {
1791cb0ef41Sopenharmony_ci        bool success = value && value->type() == protocol::Value::TypeArray;
1801cb0ef41Sopenharmony_ci        if (!success)
1811cb0ef41Sopenharmony_ci            errors->addError("list expected");
1821cb0ef41Sopenharmony_ci        return ListValue::cast(value->clone());
1831cb0ef41Sopenharmony_ci    }
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ci    static std::unique_ptr<protocol::Value> toValue(ListValue* value)
1861cb0ef41Sopenharmony_ci    {
1871cb0ef41Sopenharmony_ci        return value->clone();
1881cb0ef41Sopenharmony_ci    }
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci    static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<ListValue>& value)
1911cb0ef41Sopenharmony_ci    {
1921cb0ef41Sopenharmony_ci        return value->clone();
1931cb0ef41Sopenharmony_ci    }
1941cb0ef41Sopenharmony_ci};
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %}
1971cb0ef41Sopenharmony_ci} // namespace {{namespace}}
1981cb0ef41Sopenharmony_ci{% endfor %}
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci#endif // !defined({{"_".join(config.protocol.namespace)}}_ValueConversions_h)
201