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 #ifndef HEADLESS_PUBLIC_DOMAINS_TYPE_CONVERSIONS_H_ | |
8 #define HEADLESS_PUBLIC_DOMAINS_TYPE_CONVERSIONS_H_ | |
9 | |
10 #include "headless/public/domains/types.h" | |
11 #include "headless/public/internal/value_conversions.h" | |
12 | |
13 namespace headless { | |
14 namespace internal { | |
15 | |
16 {% for domain in api.domains %} | |
17 {% for type in domain.types %} | |
18 {% set namespace = domain.domain | camelcase_to_hacker_style %} | |
19 {% if "enum" in type %} | |
20 template <> | |
21 struct FromValue<{{namespace}}::{{type.id}}> { | |
22 static {{namespace}}::{{type.id}} Parse(const base::Value& value, ErrorReporte
r* errors) { | |
23 {% set default = namespace + '::' + type.id + '::' + type.enum[0] | saniti
ze_literal | dash_to_camelcase | camelcase_to_hacker_style | upper %} | |
24 std::string string_value; | |
25 if (!value.GetAsString(&string_value)) { | |
26 errors->AddError("string enum value expected"); | |
27 {# Return an arbitrary enum member -- the caller will just ignore it. #} | |
28 return {{default}}; | |
29 } | |
30 {% for literal in type.enum %} | |
31 if (string_value == "{{literal}}") | |
32 return {{namespace}}::{{type.id}}::{{literal | sanitize_literal | dash_to_
camelcase | camelcase_to_hacker_style | upper }}; | |
33 {% endfor %} | |
34 errors->AddError("invalid enum value"); | |
35 return {{default}}; | |
36 } | |
37 }; | |
38 | |
39 template <typename T> | |
40 std::unique_ptr<base::Value> ToValueImpl(const {{namespace}}::{{type.id}}& value
, T*) { | |
41 switch (value) { | |
42 {% for literal in type.enum %} | |
43 case {{namespace}}::{{type.id}}::{{literal | sanitize_literal | dash_to_came
lcase | camelcase_to_hacker_style | upper }}: | |
44 return base::WrapUnique(new base::StringValue("{{literal}}")); | |
45 {% endfor %} | |
46 }; | |
47 NOTREACHED(); | |
48 return nullptr; | |
49 } | |
50 {% continue %} | |
51 {% endif %} | |
52 | |
53 {% if not (type.type == "object") or not ("properties" in type) %}{% continu
e %}{% endif %} | |
54 template <> | |
55 struct FromValue<{{namespace}}::{{type.id}}> { | |
56 static std::unique_ptr<{{namespace}}::{{type.id}}> Parse(const base::Value& va
lue, ErrorReporter* errors) { | |
57 return {{namespace}}::{{type.id}}::Parse(value, errors); | |
58 } | |
59 }; | |
60 | |
61 template <typename T> | |
62 std::unique_ptr<base::Value> ToValueImpl(const {{namespace}}::{{type.id}}& value
, T*) { | |
63 return value.Serialize(); | |
64 } | |
65 | |
66 {% endfor %} | |
67 {% endfor %} | |
68 | |
69 template <typename T> | |
70 std::unique_ptr<base::Value> ToValue(const T& value) { | |
71 return ToValueImpl(value, static_cast<T*>(nullptr)); | |
72 } | |
73 | |
74 } // namespace internal | |
75 } // namespace headless | |
76 | |
77 #endif // HEADLESS_PUBLIC_DOMAINS_TYPE_CONVERSIONS_H_ | |
OLD | NEW |