• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// This file is generated by Imported_h.template.
2
3// Copyright (c) 2016 The Chromium Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6
7#ifndef {{"_".join(config.protocol.namespace)}}_{{domain.domain}}_imported_h
8#define {{"_".join(config.protocol.namespace)}}_{{domain.domain}}_imported_h
9
10#include {{format_include(config.protocol.package, "Protocol")}}
11{% if config.imported.header %}
12#include {{format_include(config.imported.header)}}
13{% else %}
14#include {{format_include(config.imported.package, domain.domain)}}
15{% endif %}
16
17{% for namespace in config.protocol.namespace %}
18namespace {{namespace}} {
19{% endfor %}
20
21using Exported = {{"::".join(config.imported.namespace)}}::Exported;
22
23#ifndef {{"_".join(config.protocol.namespace)}}_imported_imported_h
24#define {{"_".join(config.protocol.namespace)}}_imported_imported_h
25
26class {{config.lib.export_macro}} ImportedValue : public Value {
27public:
28    static std::unique_ptr<ImportedValue> fromExported(const Exported* value) {
29        return std::unique_ptr<ImportedValue>(new ImportedValue(value));
30    }
31
32    void writeJSON(StringBuilder* output) const override {
33        auto json = m_exported->toJSONString();
34        String local_json = ({{config.imported.from_imported_string % "std::move(json)"}});
35        StringUtil::builderAppend(*output, local_json);
36    }
37    void writeBinary(std::vector<uint8_t>* output) const override {
38        m_exported->writeBinary(output);
39    }
40    std::unique_ptr<Value> clone() const override {
41        return std::unique_ptr<Value>(new ImportedValue(m_exported));
42    }
43
44private:
45    explicit ImportedValue(const Exported* exported) : Value(TypeImported), m_exported(exported) { }
46    const Exported* m_exported;
47};
48
49#endif // !defined({{"_".join(config.protocol.namespace)}}_imported_imported_h)
50
51  {% for type in domain.types %}
52    {% if not (type.type == "object") or not ("properties" in type) or not protocol.is_imported(domain.domain, type.id) %}{% continue %}{% endif %}
53
54template<>
55struct ValueConversions<{{"::".join(config.imported.namespace)}}::{{domain.domain}}::API::{{type.id}}> {
56    static std::unique_ptr<{{"::".join(config.imported.namespace)}}::{{domain.domain}}::API::{{type.id}}> fromValue(protocol::Value* value, ErrorSupport* errors)
57    {
58        if (!value) {
59            errors->addError("value expected");
60            return nullptr;
61        }
62
63        std::vector<uint8_t> binary;
64        value->writeBinary(&binary);
65        auto result = {{"::".join(config.imported.namespace)}}::{{domain.domain}}::API::{{type.id}}::fromBinary(binary.data(), binary.size());
66        if (!result)
67            errors->addError("cannot parse");
68        return result;
69    }
70
71    static std::unique_ptr<protocol::Value> toValue(const {{"::".join(config.imported.namespace)}}::{{domain.domain}}::API::{{type.id}}* exported)
72    {
73        return ImportedValue::fromExported(exported);
74    }
75
76    static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<{{"::".join(config.imported.namespace)}}::{{domain.domain}}::API::{{type.id}}>& value)
77    {
78        return toValue(value.get());
79    }
80};
81
82  {% endfor %}
83
84{% for namespace in config.protocol.namespace %}
85} // namespace {{namespace}}
86{% endfor %}
87
88#endif // !defined({{"_".join(config.protocol.namespace)}}_{{domain.domain}}_imported_h)
89