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

Side by Side Diff: mojo/common/values_struct_traits.h

Issue 2803023005: Switch base::Value typemapping to be by value instead of by unique_ptr.
Patch Set: Created 3 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
« no previous file with comments | « mojo/common/values.typemap ('k') | mojo/common/values_struct_traits.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_COMMON_VALUES_STRUCT_TRAITS_H_ 5 #ifndef MOJO_COMMON_VALUES_STRUCT_TRAITS_H_
6 #define MOJO_COMMON_VALUES_STRUCT_TRAITS_H_ 6 #define MOJO_COMMON_VALUES_STRUCT_TRAITS_H_
7 7
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "mojo/common/values.mojom.h" 9 #include "mojo/common/values.mojom.h"
10 #include "mojo/public/cpp/bindings/array_traits.h" 10 #include "mojo/public/cpp/bindings/array_traits.h"
11 #include "mojo/public/cpp/bindings/clone_traits.h" 11 #include "mojo/public/cpp/bindings/clone_traits.h"
12 #include "mojo/public/cpp/bindings/map_traits.h" 12 #include "mojo/public/cpp/bindings/map_traits.h"
13 #include "mojo/public/cpp/bindings/struct_traits.h" 13 #include "mojo/public/cpp/bindings/struct_traits.h"
14 #include "mojo/public/cpp/bindings/union_traits.h" 14 #include "mojo/public/cpp/bindings/union_traits.h"
15 15
16 namespace mojo { 16 namespace mojo {
17 17
18 template <> 18 template <>
19 struct ArrayTraits<base::ListValue> { 19 struct ArrayTraits<base::ListValue> {
20 using Element = std::unique_ptr<base::Value>; 20 using Element = base::Value;
21 using ConstIterator = base::ListValue::const_iterator; 21 using ConstIterator = base::ListValue::const_iterator;
22 22
23 static size_t GetSize(const base::ListValue& input) { 23 static size_t GetSize(const base::ListValue& input) {
24 return input.GetSize(); 24 return input.GetSize();
25 } 25 }
26 26
27 static ConstIterator GetBegin(const base::ListValue& input) { 27 static ConstIterator GetBegin(const base::ListValue& input) {
28 return input.begin(); 28 return input.begin();
29 } 29 }
30 30
31 static void AdvanceIterator(ConstIterator& iterator) { ++iterator; } 31 static void AdvanceIterator(ConstIterator& iterator) { ++iterator; }
32 32
33 static const Element& GetValue(ConstIterator& iterator) { return *iterator; } 33 static const Element& GetValue(ConstIterator& iterator) { return **iterator; }
34 }; 34 };
35 35
36 template <> 36 template <>
37 struct StructTraits<common::mojom::ListValueDataView, base::ListValue> { 37 struct StructTraits<common::mojom::ListValueDataView, base::ListValue> {
38 static bool Read(common::mojom::ListValueDataView data,
39 base::ListValue* value);
40
38 static const base::ListValue& values(const base::ListValue& value) { 41 static const base::ListValue& values(const base::ListValue& value) {
39 return value; 42 return value;
40 } 43 }
41 }; 44 };
42 45
43 template <> 46 template <>
44 struct StructTraits<common::mojom::ListValueDataView,
45 std::unique_ptr<base::ListValue>> {
46 static bool IsNull(const std::unique_ptr<base::ListValue>& value) {
47 return !value;
48 }
49
50 static void SetToNull(std::unique_ptr<base::ListValue>* value) {
51 value->reset();
52 }
53
54 static const base::ListValue& values(
55 const std::unique_ptr<base::ListValue>& value) {
56 return *value;
57 }
58
59 static bool Read(common::mojom::ListValueDataView data,
60 std::unique_ptr<base::ListValue>* value);
61 };
62
63 template <>
64 struct MapTraits<base::DictionaryValue> { 47 struct MapTraits<base::DictionaryValue> {
65 using Key = std::string; 48 using Key = std::string;
66 using Value = base::Value; 49 using Value = base::Value;
67 using Iterator = base::DictionaryValue::Iterator; 50 using Iterator = base::DictionaryValue::Iterator;
68 51
69 static size_t GetSize(const base::DictionaryValue& input) { 52 static size_t GetSize(const base::DictionaryValue& input) {
70 return input.size(); 53 return input.size();
71 } 54 }
72 55
73 static Iterator GetBegin(const base::DictionaryValue& input) { 56 static Iterator GetBegin(const base::DictionaryValue& input) {
74 return Iterator(input); 57 return Iterator(input);
75 } 58 }
76 59
77 static void AdvanceIterator(Iterator& iterator) { iterator.Advance(); } 60 static void AdvanceIterator(Iterator& iterator) { iterator.Advance(); }
78 61
79 static const Key& GetKey(Iterator& iterator) { return iterator.key(); } 62 static const Key& GetKey(Iterator& iterator) { return iterator.key(); }
80 63
81 static const Value& GetValue(Iterator& iterator) { return iterator.value(); } 64 static const Value& GetValue(Iterator& iterator) { return iterator.value(); }
82 }; 65 };
83 66
84 template <> 67 template <>
85 struct StructTraits<common::mojom::DictionaryValueDataView, 68 struct StructTraits<common::mojom::DictionaryValueDataView,
86 base::DictionaryValue> { 69 base::DictionaryValue> {
70 static bool Read(common::mojom::DictionaryValueDataView data,
71 base::DictionaryValue* value);
72
87 static const base::DictionaryValue& values( 73 static const base::DictionaryValue& values(
88 const base::DictionaryValue& value) { 74 const base::DictionaryValue& value) {
89 return value; 75 return value;
90 } 76 }
91 }; 77 };
92 78
93 template <> 79 template <>
94 struct StructTraits<common::mojom::DictionaryValueDataView,
95 std::unique_ptr<base::DictionaryValue>> {
96 static bool IsNull(const std::unique_ptr<base::DictionaryValue>& value) {
97 return !value;
98 }
99
100 static void SetToNull(std::unique_ptr<base::DictionaryValue>* value) {
101 value->reset();
102 }
103
104 static const base::DictionaryValue& values(
105 const std::unique_ptr<base::DictionaryValue>& value) {
106 return *value;
107 }
108 static bool Read(common::mojom::DictionaryValueDataView data,
109 std::unique_ptr<base::DictionaryValue>* value);
110 };
111
112 template <>
113 struct CloneTraits<std::unique_ptr<base::DictionaryValue>, false> {
114 static std::unique_ptr<base::DictionaryValue> Clone(
115 const std::unique_ptr<base::DictionaryValue>& input);
116 };
117
118 template <>
119 struct UnionTraits<common::mojom::ValueDataView, base::Value> { 80 struct UnionTraits<common::mojom::ValueDataView, base::Value> {
81 static bool Read(common::mojom::ValueDataView data, base::Value* value);
82
120 static common::mojom::ValueDataView::Tag GetTag(const base::Value& data) { 83 static common::mojom::ValueDataView::Tag GetTag(const base::Value& data) {
121 switch (data.GetType()) { 84 switch (data.GetType()) {
122 case base::Value::Type::NONE: 85 case base::Value::Type::NONE:
123 return common::mojom::ValueDataView::Tag::NULL_VALUE; 86 return common::mojom::ValueDataView::Tag::NULL_VALUE;
124 case base::Value::Type::BOOLEAN: 87 case base::Value::Type::BOOLEAN:
125 return common::mojom::ValueDataView::Tag::BOOL_VALUE; 88 return common::mojom::ValueDataView::Tag::BOOL_VALUE;
126 case base::Value::Type::INTEGER: 89 case base::Value::Type::INTEGER:
127 return common::mojom::ValueDataView::Tag::INT_VALUE; 90 return common::mojom::ValueDataView::Tag::INT_VALUE;
128 case base::Value::Type::DOUBLE: 91 case base::Value::Type::DOUBLE:
129 return common::mojom::ValueDataView::Tag::DOUBLE_VALUE; 92 return common::mojom::ValueDataView::Tag::DOUBLE_VALUE;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 } 152 }
190 static const base::DictionaryValue& dictionary_value( 153 static const base::DictionaryValue& dictionary_value(
191 const base::Value& value) { 154 const base::Value& value) {
192 const base::DictionaryValue* dictionary_value = nullptr; 155 const base::DictionaryValue* dictionary_value = nullptr;
193 if (!value.GetAsDictionary(&dictionary_value)) 156 if (!value.GetAsDictionary(&dictionary_value))
194 NOTREACHED(); 157 NOTREACHED();
195 return *dictionary_value; 158 return *dictionary_value;
196 } 159 }
197 }; 160 };
198 161
199 template <>
200 struct UnionTraits<common::mojom::ValueDataView, std::unique_ptr<base::Value>> {
201 static bool IsNull(const std::unique_ptr<base::Value>& value) {
202 return !value;
203 }
204
205 static void SetToNull(std::unique_ptr<base::Value>* value) { value->reset(); }
206
207 static common::mojom::ValueDataView::Tag GetTag(
208 const std::unique_ptr<base::Value>& value) {
209 return UnionTraits<common::mojom::ValueDataView, base::Value>::GetTag(
210 *value);
211 }
212
213 static common::mojom::NullValuePtr null_value(
214 const std::unique_ptr<base::Value>& value) {
215 return UnionTraits<common::mojom::ValueDataView, base::Value>::null_value(
216 *value);
217 }
218 static bool bool_value(const std::unique_ptr<base::Value>& value) {
219 return UnionTraits<common::mojom::ValueDataView, base::Value>::bool_value(
220 *value);
221 }
222 static int32_t int_value(const std::unique_ptr<base::Value>& value) {
223 return UnionTraits<common::mojom::ValueDataView, base::Value>::int_value(
224 *value);
225 }
226 static double double_value(const std::unique_ptr<base::Value>& value) {
227 return UnionTraits<common::mojom::ValueDataView, base::Value>::double_value(
228 *value);
229 }
230 static base::StringPiece string_value(
231 const std::unique_ptr<base::Value>& value) {
232 return UnionTraits<common::mojom::ValueDataView, base::Value>::string_value(
233 *value);
234 }
235 static mojo::ConstCArray<uint8_t> binary_value(
236 const std::unique_ptr<base::Value>& value) {
237 return UnionTraits<common::mojom::ValueDataView, base::Value>::binary_value(
238 *value);
239 }
240 static const base::ListValue& list_value(
241 const std::unique_ptr<base::Value>& value) {
242 return UnionTraits<common::mojom::ValueDataView, base::Value>::list_value(
243 *value);
244 }
245 static const base::DictionaryValue& dictionary_value(
246 const std::unique_ptr<base::Value>& value) {
247 return UnionTraits<common::mojom::ValueDataView,
248 base::Value>::dictionary_value(*value);
249 }
250
251 static bool Read(common::mojom::ValueDataView data,
252 std::unique_ptr<base::Value>* value);
253 };
254
255 } // namespace mojo 162 } // namespace mojo
256 163
257 #endif // MOJO_COMMON_VALUES_STRUCT_TRAITS_H_ 164 #endif // MOJO_COMMON_VALUES_STRUCT_TRAITS_H_
OLDNEW
« no previous file with comments | « mojo/common/values.typemap ('k') | mojo/common/values_struct_traits.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698