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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
// 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_INTERNAL_VALUE_CONVERSIONS_H_
#define HEADLESS_PUBLIC_INTERNAL_VALUE_CONVERSIONS_H_
#include <memory>
#include "headless/public/util/error_reporter.h"
namespace headless {
namespace internal {
// Generic conversion from a type to a base::Value. Implemented in
// types_DOMAIN.cc after all type-specific ToValueImpls have been defined.
template <typename T>
std::unique_ptr<base::Value> ToValue(const T& value);
// Generic conversion from a base::Value to a type. Note that this generic
// variant is never defined. Instead, we declare a specific template
// specialization for all the used types.
template <typename T>
struct FromValue {
static std::unique_ptr<T> Parse(const base::Value& value,
ErrorReporter* errors);
};
// ToValueImpl is a helper used by the ToValue template for dispatching into
// type-specific serializers. It uses a dummy |T*| argument as a way to
// partially specialize vector types.
template <typename T>
std::unique_ptr<base::Value> ToValueImpl(int value, T*) {
return std::make_unique<base::Value>(value);
}
template <typename T>
std::unique_ptr<base::Value> ToValueImpl(double value, T*) {
return std::make_unique<base::Value>(value);
}
template <typename T>
std::unique_ptr<base::Value> ToValueImpl(bool value, T*) {
return std::make_unique<base::Value>(value);
}
template <typename T>
std::unique_ptr<base::Value> ToValueImpl(const std::string& value, T*) {
return std::make_unique<base::Value>(value);
}
template <typename T>
std::unique_ptr<base::Value> ToValueImpl(const base::Value& value, T*) {
return value.CreateDeepCopy();
}
template <typename T>
std::unique_ptr<base::Value> ToValueImpl(const std::vector<T>& vector,
const std::vector<T>*) {
std::unique_ptr<base::ListValue> result(new base::ListValue());
for (const auto& it : vector)
result->Append(ToValue(it));
return std::move(result);
}
template <typename T>
std::unique_ptr<base::Value> ToValueImpl(const std::unique_ptr<T>& value,
std::unique_ptr<T>*) {
return ToValue(*value);
}
// FromValue specializations for basic types.
template <>
struct FromValue<bool> {
static bool Parse(const base::Value& value, ErrorReporter* errors) {
if (!value.is_bool()) {
errors->AddError("boolean value expected");
return false;
}
return value.GetBool();
}
};
template <>
struct FromValue<int> {
static int Parse(const base::Value& value, ErrorReporter* errors) {
if (!value.is_int()) {
errors->AddError("integer value expected");
return 0;
}
return value.GetInt();
}
};
template <>
struct FromValue<double> {
static double Parse(const base::Value& value, ErrorReporter* errors) {
if (!value.is_double() && !value.is_int()) {
errors->AddError("double value expected");
return 0;
}
return value.GetDouble();
}
};
template <>
struct FromValue<std::string> {
static std::string Parse(const base::Value& value, ErrorReporter* errors) {
if (!value.is_string()) {
errors->AddError("string value expected");
return "";
}
return value.GetString();
}
};
template <>
struct FromValue<base::DictionaryValue> {
static std::unique_ptr<base::DictionaryValue> Parse(const base::Value& value,
ErrorReporter* errors) {
const base::DictionaryValue* result;
if (!value.GetAsDictionary(&result)) {
errors->AddError("dictionary value expected");
return nullptr;
}
return result->CreateDeepCopy();
}
};
template <>
struct FromValue<base::Value> {
static std::unique_ptr<base::Value> Parse(const base::Value& value,
ErrorReporter* errors) {
return value.CreateDeepCopy();
}
};
template <typename T>
struct FromValue<std::unique_ptr<T>> {
static std::unique_ptr<T> Parse(const base::Value& value,
ErrorReporter* errors) {
return FromValue<T>::Parse(value, errors);
}
};
template <typename T>
struct FromValue<std::vector<T>> {
static std::vector<T> Parse(const base::Value& value, ErrorReporter* errors) {
std::vector<T> result;
if (!value.is_list()) {
errors->AddError("list value expected");
return result;
}
errors->Push();
for (const auto& item : value.GetList())
result.push_back(FromValue<T>::Parse(item, errors));
errors->Pop();
return result;
}
};
} // namespace internal
} // namespace headless
#endif // HEADLESS_PUBLIC_INTERNAL_VALUE_CONVERSIONS_H_
|