Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: headless/public/internal/value_conversions.h

Issue 1805983002: headless: Implement client API generation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make PendingMessage moveable Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef HEADLESS_PUBLIC_INTERNAL_VALUE_CONVERSIONS_H_
6 #define HEADLESS_PUBLIC_INTERNAL_VALUE_CONVERSIONS_H_
7
8 #include <memory>
9
10 #include "base/memory/ptr_util.h"
11 #include "headless/public/util/error_reporter.h"
12
13 namespace headless {
14 namespace internal {
15
16 // Generic conversion from a type to a base::Value. Implemented in
17 // type_conversions.h after all type-specific ToValueImpls have been defined.
18 template <typename T>
19 std::unique_ptr<base::Value> ToValue(const T& value);
20
21 // Generic conversion from a base::Value to a type. Note that this generic
22 // variant is never defined. Instead, we declare a specific template
23 // specialization for all the used types.
24 template <typename T>
25 struct FromValue {
26 static std::unique_ptr<T> Parse(const base::Value& value,
27 ErrorReporter* errors);
28 };
29
30 // ToValueImpl is a helper used by the ToValue template for dispatching into
31 // type-specific serializers. It uses a dummy |T*| argument as a way to
32 // partially specialize vector types.
33 template <typename T>
34 std::unique_ptr<base::Value> ToValueImpl(int value, T*) {
35 return base::WrapUnique(new base::FundamentalValue(value));
36 }
37
38 template <typename T>
39 std::unique_ptr<base::Value> ToValueImpl(double value, T*) {
40 return base::WrapUnique(new base::FundamentalValue(value));
41 }
42
43 template <typename T>
44 std::unique_ptr<base::Value> ToValueImpl(bool value, T*) {
45 return base::WrapUnique(new base::FundamentalValue(value));
46 }
47
48 template <typename T>
49 std::unique_ptr<base::Value> ToValueImpl(const std::string& value, T*) {
50 return base::WrapUnique(new base::StringValue(value));
51 }
52
53 template <typename T>
54 std::unique_ptr<base::Value> ToValueImpl(const base::Value& value, T*) {
55 return value.CreateDeepCopy();
56 }
57
58 template <typename T>
59 std::unique_ptr<base::Value> ToValueImpl(const std::vector<T>& vector,
60 const std::vector<T>*) {
61 std::unique_ptr<base::ListValue> result(new base::ListValue());
62 for (const auto& it : vector)
63 result->Append(ToValue(it));
64 return std::move(result);
65 }
66
67 template <typename T>
68 std::unique_ptr<base::Value> ToValueImpl(const std::unique_ptr<T>& value,
69 std::unique_ptr<T>*) {
70 return ToValue(value.get());
71 }
72
73 // FromValue specializations for basic types.
74 template <>
75 struct FromValue<bool> {
76 static bool Parse(const base::Value& value, ErrorReporter* errors) {
77 bool result = false;
78 if (!value.GetAsBoolean(&result))
79 errors->AddError("boolean value expected");
80 return result;
81 }
82 };
83
84 template <>
85 struct FromValue<int> {
86 static int Parse(const base::Value& value, ErrorReporter* errors) {
87 int result = 0;
88 if (!value.GetAsInteger(&result))
89 errors->AddError("integer value expected");
90 return result;
91 }
92 };
93
94 template <>
95 struct FromValue<double> {
96 static double Parse(const base::Value& value, ErrorReporter* errors) {
97 double result = 0;
98 if (!value.GetAsDouble(&result))
99 errors->AddError("double value expected");
100 return result;
101 }
102 };
103
104 template <>
105 struct FromValue<std::string> {
106 static std::string Parse(const base::Value& value, ErrorReporter* errors) {
107 std::string result;
108 if (!value.GetAsString(&result))
109 errors->AddError("string value expected");
110 return result;
111 }
112 };
113
114 template <>
115 struct FromValue<base::DictionaryValue> {
116 static std::unique_ptr<base::DictionaryValue> Parse(const base::Value& value,
117 ErrorReporter* errors) {
118 const base::DictionaryValue* result;
119 if (!value.GetAsDictionary(&result)) {
120 errors->AddError("dictionary value expected");
121 return nullptr;
122 }
123 return result->CreateDeepCopy();
124 }
125 };
126
127 template <>
128 struct FromValue<base::Value> {
129 static std::unique_ptr<base::Value> Parse(const base::Value& value,
130 ErrorReporter* errors) {
131 return value.CreateDeepCopy();
132 }
133 };
134
135 template <typename T>
136 struct FromValue<std::unique_ptr<T>> {
137 static std::unique_ptr<T> Parse(const base::Value& value,
138 ErrorReporter* errors) {
139 return FromValue<T>::Parse(value, errors);
140 }
141 };
142
143 template <typename T>
144 struct FromValue<std::vector<T>> {
145 static std::vector<T> Parse(const base::Value& value, ErrorReporter* errors) {
146 std::vector<T> result;
147 const base::ListValue* list;
148 if (!value.GetAsList(&list)) {
149 errors->AddError("list value expected");
150 return result;
151 }
152 errors->Push();
153 for (const auto& item : *list)
154 result.push_back(FromValue<T>::Parse(*item, errors));
155 errors->Pop();
156 return result;
157 }
158 };
159
160 } // namespace internal
161 } // namespace headless
162
163 #endif // HEADLESS_PUBLIC_INTERNAL_VALUE_CONVERSIONS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698