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 #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" |
Evan Martin
2012/01/26 16:03:55
It appears BASE_EXPORT is no longer used in this f
tzik
2012/01/27 02:04:11
Done.
| |
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/memory/scoped_vector.h" |
17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
18 #include "base/string16.h" | 18 #include "base/string16.h" |
19 #include "base/string_piece.h" | 19 #include "base/string_piece.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 | 21 |
22 // JSONValueConverter converts a JSON value into a C++ struct in a | 22 // JSONValueConverter converts a JSON value into a C++ struct in a |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 // } | 85 // } |
86 // }; | 86 // }; |
87 | 87 |
88 namespace base { | 88 namespace base { |
89 | 89 |
90 template <typename StructType> | 90 template <typename StructType> |
91 class JSONValueConverter; | 91 class JSONValueConverter; |
92 | 92 |
93 namespace internal { | 93 namespace internal { |
94 | 94 |
95 template<typename StructType> | |
95 class FieldConverterBase { | 96 class FieldConverterBase { |
96 public: | 97 public: |
97 BASE_EXPORT explicit FieldConverterBase(const std::string& path); | 98 explicit FieldConverterBase(const std::string& path) : field_path_(path) {} |
98 BASE_EXPORT virtual ~FieldConverterBase(); | 99 virtual ~FieldConverterBase() {} |
99 virtual bool ConvertField(const base::Value& value, void* obj) const = 0; | 100 virtual bool ConvertField(const base::Value& value, StructType* obj) |
101 const = 0; | |
100 const std::string& field_path() const { return field_path_; } | 102 const std::string& field_path() const { return field_path_; } |
101 | 103 |
102 private: | 104 private: |
103 std::string field_path_; | 105 std::string field_path_; |
104 DISALLOW_COPY_AND_ASSIGN(FieldConverterBase); | 106 DISALLOW_COPY_AND_ASSIGN(FieldConverterBase); |
105 }; | 107 }; |
106 | 108 |
107 template <typename FieldType> | 109 template <typename FieldType> |
108 class ValueConverter { | 110 class ValueConverter { |
109 public: | 111 public: |
110 virtual ~ValueConverter() {} | 112 virtual ~ValueConverter() {} |
111 virtual bool Convert(const base::Value& value, FieldType* field) const = 0; | 113 virtual bool Convert(const base::Value& value, FieldType* field) const = 0; |
112 }; | 114 }; |
113 | 115 |
114 template <typename StructType, typename FieldType> | 116 template <typename StructType, typename FieldType> |
115 class FieldConverter : public FieldConverterBase { | 117 class FieldConverter : public FieldConverterBase<StructType> { |
116 public: | 118 public: |
117 explicit FieldConverter(const std::string& path, | 119 explicit FieldConverter(const std::string& path, |
118 FieldType StructType::* field, | 120 FieldType StructType::* field, |
119 ValueConverter<FieldType>* converter) | 121 ValueConverter<FieldType>* converter) |
120 : FieldConverterBase(path), | 122 : FieldConverterBase<StructType>(path), |
121 field_pointer_(field), | 123 field_pointer_(field), |
122 value_converter_(converter) { | 124 value_converter_(converter) { |
123 } | 125 } |
124 | 126 |
125 virtual bool ConvertField( | 127 virtual bool ConvertField( |
126 const base::Value& value, void* obj) const OVERRIDE { | 128 const base::Value& value, StructType* dst) const OVERRIDE { |
127 StructType* dst = reinterpret_cast<StructType*>(obj); | |
128 return value_converter_->Convert(value, &(dst->*field_pointer_)); | 129 return value_converter_->Convert(value, &(dst->*field_pointer_)); |
129 } | 130 } |
130 | 131 |
131 private: | 132 private: |
132 FieldType StructType::* field_pointer_; | 133 FieldType StructType::* field_pointer_; |
133 scoped_ptr<ValueConverter<FieldType> > value_converter_; | 134 scoped_ptr<ValueConverter<FieldType> > value_converter_; |
134 DISALLOW_COPY_AND_ASSIGN(FieldConverter); | 135 DISALLOW_COPY_AND_ASSIGN(FieldConverter); |
135 }; | 136 }; |
136 | 137 |
137 template <typename FieldType> | 138 template <typename FieldType> |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
416 field, | 417 field, |
417 new internal::RepeatedMessageConverter<NestedType>)); | 418 new internal::RepeatedMessageConverter<NestedType>)); |
418 } | 419 } |
419 | 420 |
420 bool Convert(const base::Value& value, StructType* output) const { | 421 bool Convert(const base::Value& value, StructType* output) const { |
421 const DictionaryValue* dictionary_value = NULL; | 422 const DictionaryValue* dictionary_value = NULL; |
422 if (!value.GetAsDictionary(&dictionary_value)) | 423 if (!value.GetAsDictionary(&dictionary_value)) |
423 return false; | 424 return false; |
424 | 425 |
425 for(size_t i = 0; i < fields_.size(); ++i) { | 426 for(size_t i = 0; i < fields_.size(); ++i) { |
426 const internal::FieldConverterBase* field_converter = fields_[i]; | 427 const internal::FieldConverterBase<StructType>* field_converter = |
428 fields_[i]; | |
427 base::Value* field = NULL; | 429 base::Value* field = NULL; |
428 if (dictionary_value->Get(field_converter->field_path(), &field)) { | 430 if (dictionary_value->Get(field_converter->field_path(), &field)) { |
429 if (!field_converter->ConvertField(*field, output)) { | 431 if (!field_converter->ConvertField(*field, output)) { |
430 DVLOG(1) << "failure at field " << field_converter->field_path(); | 432 DVLOG(1) << "failure at field " << field_converter->field_path(); |
431 return false; | 433 return false; |
432 } | 434 } |
433 } | 435 } |
434 } | 436 } |
435 return true; | 437 return true; |
436 } | 438 } |
437 | 439 |
438 private: | 440 private: |
439 ScopedVector<internal::FieldConverterBase> fields_; | 441 ScopedVector<internal::FieldConverterBase<StructType> > fields_; |
440 | 442 |
441 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); | 443 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); |
442 }; | 444 }; |
443 | 445 |
444 } // namespace base | 446 } // namespace base |
445 | 447 |
446 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ | 448 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ |
OLD | NEW |