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

Side by Side Diff: base/json/json_value_converter.h

Issue 9187047: Use ScopedVector instead of std::vector in case of repeated messages. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: git rebase and merge Created 8 years, 11 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 | « no previous file | base/json/json_value_converter_unittest.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 (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 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/base_export.h" 12 #include "base/base_export.h"
13 #include "base/basictypes.h" 13 #include "base/basictypes.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
16 #include "base/stl_util.h" 17 #include "base/stl_util.h"
17 #include "base/string16.h" 18 #include "base/string16.h"
18 #include "base/string_piece.h" 19 #include "base/string_piece.h"
19 #include "base/values.h" 20 #include "base/values.h"
20 21
21 // JSONValueConverter converts a JSON value into a C++ struct in a 22 // JSONValueConverter converts a JSON value into a C++ struct in a
22 // lightweight way. 23 // lightweight way.
23 // 24 //
24 // Usage: 25 // Usage:
25 // For real examples, you may want to refer to _unittest.cc file. 26 // For real examples, you may want to refer to _unittest.cc file.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 // method. Then, just use RegisterNestedField() from the containing struct's 58 // method. Then, just use RegisterNestedField() from the containing struct's
58 // RegisterJSONConverter method. 59 // RegisterJSONConverter method.
59 // struct Nested { 60 // struct Nested {
60 // Message foo; 61 // Message foo;
61 // static void RegisterJSONConverter(...) { 62 // static void RegisterJSONConverter(...) {
62 // ... 63 // ...
63 // converter->RegisterNestedField("foo", &Nested::foo); 64 // converter->RegisterNestedField("foo", &Nested::foo);
64 // } 65 // }
65 // }; 66 // };
66 // 67 //
67 // For repeated field, we just assume std::vector for its container 68 // For repeated field, we just assume ScopedVector for its container
68 // and you can put RegisterRepeatedInt or some other types. Use 69 // and you can put RegisterRepeatedInt or some other types. Use
69 // RegisterRepeatedMessage for nested repeated fields. 70 // RegisterRepeatedMessage for nested repeated fields.
70 // 71 //
71 // Sometimes JSON format uses string representations for other types such 72 // Sometimes JSON format uses string representations for other types such
72 // like enum, timestamp, or URL. You can use RegisterCustomField method 73 // like enum, timestamp, or URL. You can use RegisterCustomField method
73 // and specify a function to convert a StringPiece to your type. 74 // and specify a function to convert a StringPiece to your type.
74 // bool ConvertFunc(const StringPiece& s, YourEnum* result) { 75 // bool ConvertFunc(const StringPiece& s, YourEnum* result) {
75 // // do something and return true if succeed... 76 // // do something and return true if succeed...
76 // } 77 // }
77 // struct Message { 78 // struct Message {
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 const base::Value& value, NestedType* field) const OVERRIDE { 234 const base::Value& value, NestedType* field) const OVERRIDE {
234 return converter_.Convert(value, field); 235 return converter_.Convert(value, field);
235 } 236 }
236 237
237 private: 238 private:
238 JSONValueConverter<NestedType> converter_; 239 JSONValueConverter<NestedType> converter_;
239 DISALLOW_COPY_AND_ASSIGN(NestedValueConverter); 240 DISALLOW_COPY_AND_ASSIGN(NestedValueConverter);
240 }; 241 };
241 242
242 template <typename Element> 243 template <typename Element>
243 class RepeatedValueConverter : public ValueConverter<std::vector<Element> > { 244 class RepeatedValueConverter : public ValueConverter<ScopedVector<Element> > {
244 public: 245 public:
245 RepeatedValueConverter() {} 246 RepeatedValueConverter() {}
246 247
247 virtual bool Convert( 248 virtual bool Convert(
248 const base::Value& value, std::vector<Element>* field) const OVERRIDE { 249 const base::Value& value, ScopedVector<Element>* field) const OVERRIDE {
249 const base::ListValue* list = NULL; 250 const base::ListValue* list = NULL;
250 if (!value.GetAsList(&list)) { 251 if (!value.GetAsList(&list)) {
251 // The field is not a list. 252 // The field is not a list.
252 return false; 253 return false;
253 } 254 }
254 255
255 field->reserve(list->GetSize()); 256 field->reserve(list->GetSize());
256 for (size_t i = 0; i < list->GetSize(); ++i) { 257 for (size_t i = 0; i < list->GetSize(); ++i) {
257 base::Value* element = NULL; 258 base::Value* element = NULL;
258 if (!list->Get(i, &element)) 259 if (!list->Get(i, &element))
259 continue; 260 continue;
260 261
261 Element e; 262 Element *e = new Element;
262 if (!basic_converter_.Convert(*element, &e)) { 263 if (basic_converter_.Convert(*element, e)) {
264 field->push_back(e);
265 } else {
263 DVLOG(1) << "failure at " << i << "-th element"; 266 DVLOG(1) << "failure at " << i << "-th element";
264 return false; 267 return false;
265 } 268 }
266 field->push_back(e);
267 } 269 }
268 return true; 270 return true;
269 } 271 }
270 272
271 private: 273 private:
272 BasicValueConverter<Element> basic_converter_; 274 BasicValueConverter<Element> basic_converter_;
273 DISALLOW_COPY_AND_ASSIGN(RepeatedValueConverter); 275 DISALLOW_COPY_AND_ASSIGN(RepeatedValueConverter);
274 }; 276 };
275 277
276 template <typename NestedType> 278 template <typename NestedType>
277 class RepeatedMessageConverter 279 class RepeatedMessageConverter
278 : public ValueConverter<std::vector<NestedType> > { 280 : public ValueConverter<ScopedVector<NestedType> > {
279 public: 281 public:
280 RepeatedMessageConverter() {} 282 RepeatedMessageConverter() {}
281 283
282 virtual bool Convert( 284 virtual bool Convert(const base::Value& value,
283 const base::Value& value, std::vector<NestedType>* field) const OVERRIDE { 285 ScopedVector<NestedType>* field) const OVERRIDE {
284 const base::ListValue* list = NULL; 286 const base::ListValue* list = NULL;
285 if (!value.GetAsList(&list)) 287 if (!value.GetAsList(&list))
286 return false; 288 return false;
287 289
288 field->reserve(list->GetSize()); 290 field->reserve(list->GetSize());
289 for (size_t i = 0; i < list->GetSize(); ++i) { 291 for (size_t i = 0; i < list->GetSize(); ++i) {
290 base::Value* element = NULL; 292 base::Value* element = NULL;
291 if (!list->Get(i, &element)) 293 if (!list->Get(i, &element))
292 continue; 294 continue;
293 295
294 field->push_back(NestedType()); 296 NestedType* nested = new NestedType();
295 if (!converter_.Convert(*element, &field->back())) { 297 if (converter_.Convert(*element, nested)) {
298 field->push_back(nested);
299 } else {
296 DVLOG(1) << "failure at " << i << "-th element"; 300 DVLOG(1) << "failure at " << i << "-th element";
297 return false; 301 return false;
298 } 302 }
299 } 303 }
300 return true; 304 return true;
301 } 305 }
302 306
303 private: 307 private:
304 JSONValueConverter<NestedType> converter_; 308 JSONValueConverter<NestedType> converter_;
305 DISALLOW_COPY_AND_ASSIGN(RepeatedMessageConverter); 309 DISALLOW_COPY_AND_ASSIGN(RepeatedMessageConverter);
306 }; 310 };
307 311
308 } // namespace internal 312 } // namespace internal
309 313
310 template <class StructType> 314 template <class StructType>
311 class JSONValueConverter { 315 class JSONValueConverter {
312 public: 316 public:
313 JSONValueConverter() { 317 JSONValueConverter() {
314 StructType::RegisterJSONConverter(this); 318 StructType::RegisterJSONConverter(this);
315 } 319 }
316 320
317 ~JSONValueConverter() {
318 STLDeleteContainerPointers(fields_.begin(), fields_.end());
319 }
320
321 void RegisterIntField(const std::string& field_name, 321 void RegisterIntField(const std::string& field_name,
322 int StructType::* field) { 322 int StructType::* field) {
323 fields_.push_back(new internal::FieldConverter<StructType, int>( 323 fields_.push_back(new internal::FieldConverter<StructType, int>(
324 field_name, field, new internal::BasicValueConverter<int>)); 324 field_name, field, new internal::BasicValueConverter<int>));
325 } 325 }
326 326
327 void RegisterStringField(const std::string& field_name, 327 void RegisterStringField(const std::string& field_name,
328 std::string StructType::* field) { 328 std::string StructType::* field) {
329 fields_.push_back(new internal::FieldConverter<StructType, std::string>( 329 fields_.push_back(new internal::FieldConverter<StructType, std::string>(
330 field_name, field, new internal::BasicValueConverter<std::string>)); 330 field_name, field, new internal::BasicValueConverter<std::string>));
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 const std::string& field_name, 362 const std::string& field_name,
363 FieldType StructType::* field, 363 FieldType StructType::* field,
364 bool (*convert_func)(const StringPiece&, FieldType*)) { 364 bool (*convert_func)(const StringPiece&, FieldType*)) {
365 fields_.push_back(new internal::FieldConverter<StructType, FieldType>( 365 fields_.push_back(new internal::FieldConverter<StructType, FieldType>(
366 field_name, 366 field_name,
367 field, 367 field,
368 new internal::CustomFieldConverter<FieldType>(convert_func))); 368 new internal::CustomFieldConverter<FieldType>(convert_func)));
369 } 369 }
370 370
371 void RegisterRepeatedInt(const std::string& field_name, 371 void RegisterRepeatedInt(const std::string& field_name,
372 std::vector<int> StructType::* field) { 372 ScopedVector<int> StructType::* field) {
373 fields_.push_back( 373 fields_.push_back(
374 new internal::FieldConverter<StructType, std::vector<int> >( 374 new internal::FieldConverter<StructType, ScopedVector<int> >(
375 field_name, field, new internal::RepeatedValueConverter<int>)); 375 field_name, field, new internal::RepeatedValueConverter<int>));
376 } 376 }
377 377
378 void RegisterRepeatedString(const std::string& field_name, 378 void RegisterRepeatedString(const std::string& field_name,
379 std::vector<std::string> StructType::* field) { 379 ScopedVector<std::string> StructType::* field) {
380 fields_.push_back( 380 fields_.push_back(
381 new internal::FieldConverter<StructType, std::vector<std::string> >( 381 new internal::FieldConverter<StructType, ScopedVector<std::string> >(
382 field_name, 382 field_name,
383 field, 383 field,
384 new internal::RepeatedValueConverter<std::string>)); 384 new internal::RepeatedValueConverter<std::string>));
385 } 385 }
386 386
387 void RegisterRepeatedString(const std::string& field_name, 387 void RegisterRepeatedString(const std::string& field_name,
388 std::vector<string16> StructType::* field) { 388 ScopedVector<string16> StructType::* field) {
389 fields_.push_back( 389 fields_.push_back(
390 new internal::FieldConverter<StructType, std::vector<string16> >( 390 new internal::FieldConverter<StructType, ScopedVector<string16> >(
391 field_name, 391 field_name,
392 field, 392 field,
393 new internal::RepeatedValueConverter<string16>)); 393 new internal::RepeatedValueConverter<string16>));
394 } 394 }
395 395
396 void RegisterRepeatedDouble(const std::string& field_name, 396 void RegisterRepeatedDouble(const std::string& field_name,
397 std::vector<double> StructType::* field) { 397 ScopedVector<double> StructType::* field) {
398 fields_.push_back( 398 fields_.push_back(
399 new internal::FieldConverter<StructType, std::vector<double> >( 399 new internal::FieldConverter<StructType, ScopedVector<double> >(
400 field_name, field, new internal::RepeatedValueConverter<double>)); 400 field_name, field, new internal::RepeatedValueConverter<double>));
401 } 401 }
402 402
403 void RegisterRepeatedBool(const std::string& field_name, 403 void RegisterRepeatedBool(const std::string& field_name,
404 std::vector<bool> StructType::* field) { 404 ScopedVector<bool> StructType::* field) {
405 fields_.push_back( 405 fields_.push_back(
406 new internal::FieldConverter<StructType, std::vector<bool> >( 406 new internal::FieldConverter<StructType, ScopedVector<bool> >(
407 field_name, field, new internal::RepeatedValueConverter<bool>)); 407 field_name, field, new internal::RepeatedValueConverter<bool>));
408 } 408 }
409 409
410 template <class NestedType> 410 template <class NestedType>
411 void RegisterRepeatedMessage(const std::string& field_name, 411 void RegisterRepeatedMessage(const std::string& field_name,
412 std::vector<NestedType> StructType::* field) { 412 ScopedVector<NestedType> StructType::* field) {
413 fields_.push_back( 413 fields_.push_back(
414 new internal::FieldConverter<StructType, std::vector<NestedType> >( 414 new internal::FieldConverter<StructType, ScopedVector<NestedType> >(
415 field_name, 415 field_name,
416 field, 416 field,
417 new internal::RepeatedMessageConverter<NestedType>)); 417 new internal::RepeatedMessageConverter<NestedType>));
418 } 418 }
419 419
420 bool Convert(const base::Value& value, StructType* output) const { 420 bool Convert(const base::Value& value, StructType* output) const {
421 const DictionaryValue* dictionary_value = NULL; 421 const DictionaryValue* dictionary_value = NULL;
422 if (!value.GetAsDictionary(&dictionary_value)) 422 if (!value.GetAsDictionary(&dictionary_value))
423 return false; 423 return false;
424 424
425 for(std::vector<internal::FieldConverterBase*>::const_iterator it = 425 for(size_t i = 0; i < fields_.size(); ++i) {
426 fields_.begin(); it != fields_.end(); ++it) { 426 const internal::FieldConverterBase* field_converter = fields_[i];
427 base::Value* field = NULL; 427 base::Value* field = NULL;
428 if (dictionary_value->Get((*it)->field_path(), &field)) { 428 if (dictionary_value->Get(field_converter->field_path(), &field)) {
429 if (!(*it)->ConvertField(*field, output)) { 429 if (!field_converter->ConvertField(*field, output)) {
430 DVLOG(1) << "failure at field " << (*it)->field_path(); 430 DVLOG(1) << "failure at field " << field_converter->field_path();
431 return false; 431 return false;
432 } 432 }
433 } 433 }
434 } 434 }
435 return true; 435 return true;
436 } 436 }
437 437
438 private: 438 private:
439 std::vector<internal::FieldConverterBase*> fields_; 439 ScopedVector<internal::FieldConverterBase> fields_;
440 440
441 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); 441 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter);
442 }; 442 };
443 443
444 } // namespace base 444 } // namespace base
445 445
446 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ 446 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_
OLDNEW
« no previous file with comments | « no previous file | base/json/json_value_converter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698