OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 BASE_JSON_JSON_VALUE_CONVERTER_H_ | 5 #ifndef BASE_JSON_JSON_VALUE_CONVERTER_H_ |
6 #define BASE_JSON_JSON_VALUE_CONVERTER_H_ | 6 #define BASE_JSON_JSON_VALUE_CONVERTER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
| 10 #include <memory> |
10 #include <string> | 11 #include <string> |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
13 #include "base/base_export.h" | 14 #include "base/base_export.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "base/memory/scoped_ptr.h" | |
17 #include "base/memory/scoped_vector.h" | 17 #include "base/memory/scoped_vector.h" |
18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
19 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
20 #include "base/strings/string_piece.h" | 20 #include "base/strings/string_piece.h" |
21 #include "base/values.h" | 21 #include "base/values.h" |
22 | 22 |
23 // JSONValueConverter converts a JSON value into a C++ struct in a | 23 // JSONValueConverter converts a JSON value into a C++ struct in a |
24 // lightweight way. | 24 // lightweight way. |
25 // | 25 // |
26 // Usage: | 26 // Usage: |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 field_pointer_(field), | 124 field_pointer_(field), |
125 value_converter_(converter) { | 125 value_converter_(converter) { |
126 } | 126 } |
127 | 127 |
128 bool ConvertField(const base::Value& value, StructType* dst) const override { | 128 bool ConvertField(const base::Value& value, StructType* dst) const override { |
129 return value_converter_->Convert(value, &(dst->*field_pointer_)); | 129 return value_converter_->Convert(value, &(dst->*field_pointer_)); |
130 } | 130 } |
131 | 131 |
132 private: | 132 private: |
133 FieldType StructType::* field_pointer_; | 133 FieldType StructType::* field_pointer_; |
134 scoped_ptr<ValueConverter<FieldType> > value_converter_; | 134 std::unique_ptr<ValueConverter<FieldType>> value_converter_; |
135 DISALLOW_COPY_AND_ASSIGN(FieldConverter); | 135 DISALLOW_COPY_AND_ASSIGN(FieldConverter); |
136 }; | 136 }; |
137 | 137 |
138 template <typename FieldType> | 138 template <typename FieldType> |
139 class BasicValueConverter; | 139 class BasicValueConverter; |
140 | 140 |
141 template <> | 141 template <> |
142 class BASE_EXPORT BasicValueConverter<int> : public ValueConverter<int> { | 142 class BASE_EXPORT BasicValueConverter<int> : public ValueConverter<int> { |
143 public: | 143 public: |
144 BasicValueConverter() {} | 144 BasicValueConverter() {} |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 // The field is not a list. | 259 // The field is not a list. |
260 return false; | 260 return false; |
261 } | 261 } |
262 | 262 |
263 field->reserve(list->GetSize()); | 263 field->reserve(list->GetSize()); |
264 for (size_t i = 0; i < list->GetSize(); ++i) { | 264 for (size_t i = 0; i < list->GetSize(); ++i) { |
265 const base::Value* element = NULL; | 265 const base::Value* element = NULL; |
266 if (!list->Get(i, &element)) | 266 if (!list->Get(i, &element)) |
267 continue; | 267 continue; |
268 | 268 |
269 scoped_ptr<Element> e(new Element); | 269 std::unique_ptr<Element> e(new Element); |
270 if (basic_converter_.Convert(*element, e.get())) { | 270 if (basic_converter_.Convert(*element, e.get())) { |
271 field->push_back(e.release()); | 271 field->push_back(e.release()); |
272 } else { | 272 } else { |
273 DVLOG(1) << "failure at " << i << "-th element"; | 273 DVLOG(1) << "failure at " << i << "-th element"; |
274 return false; | 274 return false; |
275 } | 275 } |
276 } | 276 } |
277 return true; | 277 return true; |
278 } | 278 } |
279 | 279 |
(...skipping 13 matching lines...) Expand all Loading... |
293 const base::ListValue* list = NULL; | 293 const base::ListValue* list = NULL; |
294 if (!value.GetAsList(&list)) | 294 if (!value.GetAsList(&list)) |
295 return false; | 295 return false; |
296 | 296 |
297 field->reserve(list->GetSize()); | 297 field->reserve(list->GetSize()); |
298 for (size_t i = 0; i < list->GetSize(); ++i) { | 298 for (size_t i = 0; i < list->GetSize(); ++i) { |
299 const base::Value* element = NULL; | 299 const base::Value* element = NULL; |
300 if (!list->Get(i, &element)) | 300 if (!list->Get(i, &element)) |
301 continue; | 301 continue; |
302 | 302 |
303 scoped_ptr<NestedType> nested(new NestedType); | 303 std::unique_ptr<NestedType> nested(new NestedType); |
304 if (converter_.Convert(*element, nested.get())) { | 304 if (converter_.Convert(*element, nested.get())) { |
305 field->push_back(nested.release()); | 305 field->push_back(nested.release()); |
306 } else { | 306 } else { |
307 DVLOG(1) << "failure at " << i << "-th element"; | 307 DVLOG(1) << "failure at " << i << "-th element"; |
308 return false; | 308 return false; |
309 } | 309 } |
310 } | 310 } |
311 return true; | 311 return true; |
312 } | 312 } |
313 | 313 |
(...skipping 16 matching lines...) Expand all Loading... |
330 const base::ListValue* list = NULL; | 330 const base::ListValue* list = NULL; |
331 if (!value.GetAsList(&list)) | 331 if (!value.GetAsList(&list)) |
332 return false; | 332 return false; |
333 | 333 |
334 field->reserve(list->GetSize()); | 334 field->reserve(list->GetSize()); |
335 for (size_t i = 0; i < list->GetSize(); ++i) { | 335 for (size_t i = 0; i < list->GetSize(); ++i) { |
336 const base::Value* element = NULL; | 336 const base::Value* element = NULL; |
337 if (!list->Get(i, &element)) | 337 if (!list->Get(i, &element)) |
338 continue; | 338 continue; |
339 | 339 |
340 scoped_ptr<NestedType> nested(new NestedType); | 340 std::unique_ptr<NestedType> nested(new NestedType); |
341 if ((*convert_func_)(element, nested.get())) { | 341 if ((*convert_func_)(element, nested.get())) { |
342 field->push_back(nested.release()); | 342 field->push_back(nested.release()); |
343 } else { | 343 } else { |
344 DVLOG(1) << "failure at " << i << "-th element"; | 344 DVLOG(1) << "failure at " << i << "-th element"; |
345 return false; | 345 return false; |
346 } | 346 } |
347 } | 347 } |
348 return true; | 348 return true; |
349 } | 349 } |
350 | 350 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 | 507 |
508 private: | 508 private: |
509 ScopedVector<internal::FieldConverterBase<StructType> > fields_; | 509 ScopedVector<internal::FieldConverterBase<StructType> > fields_; |
510 | 510 |
511 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); | 511 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); |
512 }; | 512 }; |
513 | 513 |
514 } // namespace base | 514 } // namespace base |
515 | 515 |
516 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ | 516 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ |
OLD | NEW |