OLD | NEW |
| (Empty) |
1 // This file is generated | |
2 | |
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 | |
5 // found in the LICENSE file. | |
6 | |
7 #include "headless/public/domains/types.h" | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "headless/public/domains/type_conversions.h" | |
11 | |
12 namespace headless { | |
13 | |
14 // ------------- Enum values from types. | |
15 {% for domain in api.domains %} | |
16 {% continue %} | |
17 | |
18 namespace internal { | |
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> | |
53 std::unique_ptr<base::Value> ToValueImpl(const {{namespace}}::{{type.id}}& value
, T*) { | |
54 return value.Serialize(); | |
55 } | |
56 | |
57 {% endfor %} | |
58 } // namespace internal | |
59 {% endfor %} | |
60 | |
61 {% for domain in api.domains %} | |
62 | |
63 namespace {{domain.domain | camelcase_to_hacker_style}} { | |
64 {% for type in domain.types %} | |
65 {% if not (type.type == "object") or not ("properties" in type) %}{% continu
e %}{% endif %} | |
66 | |
67 std::unique_ptr<{{type.id}}> {{type.id}}::Parse(const base::Value& value, ErrorR
eporter* errors) { | |
68 errors->Push(); | |
69 errors->SetName("{{type.id}}"); | |
70 const base::DictionaryValue* object; | |
71 if (!value.GetAsDictionary(&object)) { | |
72 errors->AddError("object expected"); | |
73 errors->Pop(); | |
74 return nullptr; | |
75 } | |
76 | |
77 std::unique_ptr<{{type.id}}> result(new {{type.id}}()); | |
78 errors->Push(); | |
79 errors->SetName("{{type.id}}"); | |
80 {% for property in type.properties %} | |
81 {% set value_name = property.name | camelcase_to_hacker_style + "_value" %
} | |
82 const base::Value* {{value_name}}; | |
83 if (object->Get("{{property.name}}", &{{value_name}})) { | |
84 errors->SetName("{{property.name}}"); | |
85 {% if property.optional %} | |
86 result->{{property.name | camelcase_to_hacker_style}}_ = internal::FromValue
<{{resolve_type(property).raw_type}}>::Parse(*{{value_name}}, errors); | |
87 {% else %} | |
88 result->{{property.name | camelcase_to_hacker_style}}_ = internal::FromValue
<{{resolve_type(property).raw_type}}>::Parse(*{{value_name}}, errors); | |
89 {% endif %} | |
90 {% if property.optional %} | |
91 } | |
92 {% else %} | |
93 } else { | |
94 errors->AddError("required property missing: {{property.name}}"); | |
95 } | |
96 {% endif %} | |
97 {% endfor %} | |
98 errors->Pop(); | |
99 errors->Pop(); | |
100 if (errors->HasErrors()) | |
101 return nullptr; | |
102 return result; | |
103 } | |
104 | |
105 std::unique_ptr<base::Value> {{type.id}}::Serialize() const { | |
106 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | |
107 {% for property in type.properties %} | |
108 {% set type = resolve_type(property) %} | |
109 {% if property.optional %} | |
110 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)}})); | |
112 {% else %} | |
113 result->Set("{{property.name}}", internal::ToValue({{type.to_raw_type % ("%s_"
% property.name | camelcase_to_hacker_style)}})); | |
114 {% endif %} | |
115 {% endfor %} | |
116 return std::move(result); | |
117 } | |
118 | |
119 std::unique_ptr<{{type.id}}> {{type.id}}::Clone() const { | |
120 ErrorReporter errors; | |
121 std::unique_ptr<{{type.id}}> result = Parse(*Serialize(), &errors); | |
122 DCHECK(!errors.HasErrors()); | |
123 return result; | |
124 } | |
125 | |
126 {% endfor %} | |
127 } // namespace {{domain.domain | camelcase_to_hacker_style}} | |
128 {% endfor %} | |
129 | |
130 } // namespace headless | |
OLD | NEW |