OLD | NEW |
1 // This file is generated | 1 // This file is generated |
2 | 2 |
3 // Copyright 2016 The Chromium Authors. All rights reserved. | 3 // Copyright 2016 The Chromium Authors. All rights reserved. |
4 // Use of this source code is governed by a BSD-style license that can be | 4 // Use of this source code is governed by a BSD-style license that can be |
5 // found in the LICENSE file. | 5 // found in the LICENSE file. |
6 | 6 |
7 #include "headless/public/domains/types.h" | 7 {% for domain_name in domain.dependencies %} |
| 8 #include "headless/public/devtools/domains/types_{{domain_name | camelcase_to_ha
cker_style}}.h" |
| 9 {% endfor %} |
8 | 10 |
9 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
10 #include "headless/public/domains/type_conversions.h" | 12 {% for dep in domain.dependencies %} |
| 13 #include "headless/public/devtools/internal/type_conversions_{{dep | camelcase_t
o_hacker_style}}.h" |
| 14 {% endfor %} |
11 | 15 |
12 namespace headless { | 16 namespace headless { |
13 | 17 |
14 // ------------- Enum values from types. | |
15 {% for domain in api.domains %} | |
16 {% continue %} | |
17 | |
18 namespace internal { | 18 namespace internal { |
19 | 19 |
20 {% for type in domain.types %} | |
21 // {{type}} | |
22 {% if type.type == "array" %} | |
23 template <> | |
24 struct FromValue<{{resolve_type(type).raw_type}}> { | |
25 static {{resolve_type(type).raw_type}} Parse(const base::Value& value, ErrorRe
porter* errors) { | |
26 {{resolve_type(type).raw_type}} result; | |
27 const base::ListValue* list; | |
28 if (!value.GetAsList(&list)) { | |
29 errors->AddError("list value expected"); | |
30 return result; | |
31 } | |
32 errors->Push(); | |
33 for (const auto& item : *list) | |
34 result.push_back(FromValue<{{resolve_type(type).raw_type}}::value_type>::P
arse(*item, errors)); | |
35 errors->Pop(); | |
36 return result; | |
37 } | |
38 }; | |
39 | |
40 {% continue %} | |
41 {% endif %} | |
42 #} | |
43 {% if not (type.type == "object") or not ("properties" in type) %}{% continu
e %}{% endif %} | |
44 {% set namespace = domain.domain | camelcase_to_hacker_style %} | |
45 template <> | |
46 struct FromValue<{{namespace}}::{{type.id}}> { | |
47 static std::unique_ptr<{{namespace}}::{{type.id}}> Parse(const base::Value& va
lue, ErrorReporter* errors) { | |
48 return {{namespace}}::{{type.id}}::Parse(value, errors); | |
49 } | |
50 }; | |
51 | |
52 template <typename T> | 20 template <typename T> |
53 std::unique_ptr<base::Value> ToValueImpl(const {{namespace}}::{{type.id}}& value
, T*) { | 21 std::unique_ptr<base::Value> ToValue(const T& value) { |
54 return value.Serialize(); | 22 return ToValueImpl(value, static_cast<T*>(nullptr)); |
55 } | 23 } |
56 | 24 |
57 {% endfor %} | |
58 } // namespace internal | 25 } // namespace internal |
59 {% endfor %} | |
60 | |
61 {% for domain in api.domains %} | |
62 | 26 |
63 namespace {{domain.domain | camelcase_to_hacker_style}} { | 27 namespace {{domain.domain | camelcase_to_hacker_style}} { |
64 {% for type in domain.types %} | 28 {% for type in domain.types %} |
65 {% if not (type.type == "object") or not ("properties" in type) %}{% continu
e %}{% endif %} | 29 {% if not (type.type == "object") or not ("properties" in type) %}{% continue
%}{% endif %} |
66 | 30 |
67 std::unique_ptr<{{type.id}}> {{type.id}}::Parse(const base::Value& value, ErrorR
eporter* errors) { | 31 std::unique_ptr<{{type.id}}> {{type.id}}::Parse(const base::Value& value, ErrorR
eporter* errors) { |
68 errors->Push(); | 32 errors->Push(); |
69 errors->SetName("{{type.id}}"); | 33 errors->SetName("{{type.id}}"); |
70 const base::DictionaryValue* object; | 34 const base::DictionaryValue* object; |
71 if (!value.GetAsDictionary(&object)) { | 35 if (!value.GetAsDictionary(&object)) { |
72 errors->AddError("object expected"); | 36 errors->AddError("object expected"); |
73 errors->Pop(); | 37 errors->Pop(); |
74 return nullptr; | 38 return nullptr; |
75 } | 39 } |
76 | 40 |
77 std::unique_ptr<{{type.id}}> result(new {{type.id}}()); | 41 std::unique_ptr<{{type.id}}> result(new {{type.id}}()); |
78 errors->Push(); | 42 errors->Push(); |
79 errors->SetName("{{type.id}}"); | 43 errors->SetName("{{type.id}}"); |
80 {% for property in type.properties %} | 44 {% for property in type.properties %} |
81 {% set value_name = property.name | camelcase_to_hacker_style + "_value" %
} | 45 {% set value_name = property.name | camelcase_to_hacker_style + "_value" %} |
82 const base::Value* {{value_name}}; | 46 const base::Value* {{value_name}}; |
83 if (object->Get("{{property.name}}", &{{value_name}})) { | 47 if (object->Get("{{property.name}}", &{{value_name}})) { |
84 errors->SetName("{{property.name}}"); | 48 errors->SetName("{{property.name}}"); |
85 {% if property.optional %} | 49 {% if property.optional %} |
86 result->{{property.name | camelcase_to_hacker_style}}_ = internal::FromValue
<{{resolve_type(property).raw_type}}>::Parse(*{{value_name}}, errors); | 50 result->{{property.name | camelcase_to_hacker_style}}_ = internal::FromValue
<{{resolve_type(property).raw_type}}>::Parse(*{{value_name}}, errors); |
87 {% else %} | 51 {% else %} |
88 result->{{property.name | camelcase_to_hacker_style}}_ = internal::FromValue
<{{resolve_type(property).raw_type}}>::Parse(*{{value_name}}, errors); | 52 result->{{property.name | camelcase_to_hacker_style}}_ = internal::FromValue
<{{resolve_type(property).raw_type}}>::Parse(*{{value_name}}, errors); |
89 {% endif %} | 53 {% endif %} |
90 {% if property.optional %} | 54 {% if property.optional %} |
91 } | 55 } |
92 {% else %} | 56 {% else %} |
93 } else { | 57 } else { |
94 errors->AddError("required property missing: {{property.name}}"); | 58 errors->AddError("required property missing: {{property.name}}"); |
95 } | 59 } |
96 {% endif %} | 60 {% endif %} |
97 {% endfor %} | 61 {% endfor %} |
98 errors->Pop(); | 62 errors->Pop(); |
99 errors->Pop(); | 63 errors->Pop(); |
100 if (errors->HasErrors()) | 64 if (errors->HasErrors()) |
101 return nullptr; | 65 return nullptr; |
102 return result; | 66 return result; |
103 } | 67 } |
104 | 68 |
105 std::unique_ptr<base::Value> {{type.id}}::Serialize() const { | 69 std::unique_ptr<base::Value> {{type.id}}::Serialize() const { |
106 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | 70 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
107 {% for property in type.properties %} | 71 {% for property in type.properties %} |
108 {% set type = resolve_type(property) %} | 72 {% set type = resolve_type(property) %} |
109 {% if property.optional %} | 73 {% if property.optional %} |
110 if ({{property.name | camelcase_to_hacker_style}}_) | 74 if ({{property.name | camelcase_to_hacker_style}}_) |
111 result->Set("{{property.name}}", internal::ToValue({{type.to_raw_type % ("%s
_.value()" % property.name | camelcase_to_hacker_style)}})); | 75 result->Set("{{property.name}}", internal::ToValue({{type.to_raw_type % ("%s
_.value()" % property.name | camelcase_to_hacker_style)}})); |
112 {% else %} | 76 {% else %} |
113 result->Set("{{property.name}}", internal::ToValue({{type.to_raw_type % ("%s_"
% property.name | camelcase_to_hacker_style)}})); | 77 result->Set("{{property.name}}", internal::ToValue({{type.to_raw_type % ("%s_"
% property.name | camelcase_to_hacker_style)}})); |
114 {% endif %} | 78 {% endif %} |
115 {% endfor %} | 79 {% endfor %} |
116 return std::move(result); | 80 return std::move(result); |
117 } | 81 } |
118 | 82 |
119 std::unique_ptr<{{type.id}}> {{type.id}}::Clone() const { | 83 std::unique_ptr<{{type.id}}> {{type.id}}::Clone() const { |
120 ErrorReporter errors; | 84 ErrorReporter errors; |
121 std::unique_ptr<{{type.id}}> result = Parse(*Serialize(), &errors); | 85 std::unique_ptr<{{type.id}}> result = Parse(*Serialize(), &errors); |
122 DCHECK(!errors.HasErrors()); | 86 DCHECK(!errors.HasErrors()); |
123 return result; | 87 return result; |
124 } | 88 } |
125 | 89 |
126 {% endfor %} | |
127 } // namespace {{domain.domain | camelcase_to_hacker_style}} | |
128 {% endfor %} | 90 {% endfor %} |
129 | 91 |
130 } // namespace headless | 92 } // namespace {{domain.domain | camelcase_to_hacker_style}} |
| 93 } // namespace headless |
OLD | NEW |