summaryrefslogtreecommitdiff
path: root/chromium/headless/lib/browser/devtools_api/domain_type_conversions_h.template
blob: 15796eb756d3212d5b6e03f2f8aba77b909b982d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// This file is generated

// 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.

#ifndef HEADLESS_PUBLIC_DEVTOOLS_INTERNAL_TYPE_CONVERSIONS_{{domain.domain | camelcase_to_hacker_style | upper}}_H_
#define HEADLESS_PUBLIC_DEVTOOLS_INTERNAL_TYPE_CONVERSIONS_{{domain.domain | camelcase_to_hacker_style | upper}}_H_

#include "headless/public/devtools/domains/types_{{domain.domain | camelcase_to_hacker_style}}.h"
#include "headless/public/internal/value_conversions.h"

namespace headless {
namespace internal {

{% for type in domain.types %}
  {% set namespace = domain.domain | camelcase_to_hacker_style %}
  {% if "enum" in type %}
template <>
struct FromValue<{{namespace}}::{{type.id}}> {
  static {{namespace}}::{{type.id}} Parse(const base::Value& value, ErrorReporter* errors) {
    {% set default = namespace + '::' + type.id + '::' + type.enum[0] | sanitize_literal | dash_to_camelcase | camelcase_to_hacker_style | upper %}
    std::string string_value;
    if (!value.GetAsString(&string_value)) {
      errors->AddError("string enum value expected");
      {# Return an arbitrary enum member -- the caller will just ignore it. #}
      return {{default}};
    }
    {% for literal in type.enum %}
    if (string_value == "{{literal}}")
      return {{namespace}}::{{type.id}}::{{literal | sanitize_literal | dash_to_camelcase | camelcase_to_hacker_style | upper }};
    {% endfor %}
    errors->AddError("invalid enum value");
    return {{default}};
  }
};

template <typename T>
std::unique_ptr<base::Value> ToValueImpl(const {{namespace}}::{{type.id}}& value, T*) {
  switch (value) {
    {% for literal in type.enum %}
    case {{namespace}}::{{type.id}}::{{literal | sanitize_literal | dash_to_camelcase | camelcase_to_hacker_style | upper }}:
      return base::WrapUnique(new base::Value("{{literal}}"));
    {% endfor %}
  };
  NOTREACHED();
  return nullptr;
}
    {% continue %}
  {% endif %}

  {% if not (type.type == "object") or not ("properties" in type) %}{% continue %}{% endif %}
template <>
struct FromValue<{{namespace}}::{{type.id}}> {
  static std::unique_ptr<{{namespace}}::{{type.id}}> Parse(const base::Value& value, ErrorReporter* errors) {
    return {{namespace}}::{{type.id}}::Parse(value, errors);
  }
};

template <typename T>
std::unique_ptr<base::Value> ToValueImpl(const {{namespace}}::{{type.id}}& value, T*) {
  return value.Serialize();
}

{% endfor %}

}  // namespace internal
}  // namespace headless

#endif  // HEADLESS_PUBLIC_DEVTOOLS_INTERNAL_TYPE_CONVERSIONS_{{domain.domain | camelcase_to_hacker_style | upper}}_H_