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 <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 public: | 116 public: |
117 explicit FieldConverter(const std::string& path, | 117 explicit FieldConverter(const std::string& path, |
118 FieldType StructType::* field, | 118 FieldType StructType::* field, |
119 ValueConverter<FieldType>* converter) | 119 ValueConverter<FieldType>* converter) |
120 : FieldConverterBase<StructType>(path), | 120 : FieldConverterBase<StructType>(path), |
121 field_pointer_(field), | 121 field_pointer_(field), |
122 value_converter_(converter) { | 122 value_converter_(converter) { |
123 } | 123 } |
124 | 124 |
125 virtual bool ConvertField( | 125 virtual bool ConvertField( |
126 const base::Value& value, StructType* dst) const OVERRIDE { | 126 const base::Value& value, StructType* dst) const override { |
127 return value_converter_->Convert(value, &(dst->*field_pointer_)); | 127 return value_converter_->Convert(value, &(dst->*field_pointer_)); |
128 } | 128 } |
129 | 129 |
130 private: | 130 private: |
131 FieldType StructType::* field_pointer_; | 131 FieldType StructType::* field_pointer_; |
132 scoped_ptr<ValueConverter<FieldType> > value_converter_; | 132 scoped_ptr<ValueConverter<FieldType> > value_converter_; |
133 DISALLOW_COPY_AND_ASSIGN(FieldConverter); | 133 DISALLOW_COPY_AND_ASSIGN(FieldConverter); |
134 }; | 134 }; |
135 | 135 |
136 template <typename FieldType> | 136 template <typename FieldType> |
137 class BasicValueConverter; | 137 class BasicValueConverter; |
138 | 138 |
139 template <> | 139 template <> |
140 class BasicValueConverter<int> : public ValueConverter<int> { | 140 class BasicValueConverter<int> : public ValueConverter<int> { |
141 public: | 141 public: |
142 BasicValueConverter() {} | 142 BasicValueConverter() {} |
143 | 143 |
144 virtual bool Convert(const base::Value& value, int* field) const OVERRIDE { | 144 virtual bool Convert(const base::Value& value, int* field) const override { |
145 return value.GetAsInteger(field); | 145 return value.GetAsInteger(field); |
146 } | 146 } |
147 | 147 |
148 private: | 148 private: |
149 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); | 149 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
150 }; | 150 }; |
151 | 151 |
152 template <> | 152 template <> |
153 class BasicValueConverter<std::string> : public ValueConverter<std::string> { | 153 class BasicValueConverter<std::string> : public ValueConverter<std::string> { |
154 public: | 154 public: |
155 BasicValueConverter() {} | 155 BasicValueConverter() {} |
156 | 156 |
157 virtual bool Convert( | 157 virtual bool Convert( |
158 const base::Value& value, std::string* field) const OVERRIDE { | 158 const base::Value& value, std::string* field) const override { |
159 return value.GetAsString(field); | 159 return value.GetAsString(field); |
160 } | 160 } |
161 | 161 |
162 private: | 162 private: |
163 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); | 163 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
164 }; | 164 }; |
165 | 165 |
166 template <> | 166 template <> |
167 class BasicValueConverter<string16> : public ValueConverter<string16> { | 167 class BasicValueConverter<string16> : public ValueConverter<string16> { |
168 public: | 168 public: |
169 BasicValueConverter() {} | 169 BasicValueConverter() {} |
170 | 170 |
171 virtual bool Convert( | 171 virtual bool Convert( |
172 const base::Value& value, string16* field) const OVERRIDE { | 172 const base::Value& value, string16* field) const override { |
173 return value.GetAsString(field); | 173 return value.GetAsString(field); |
174 } | 174 } |
175 | 175 |
176 private: | 176 private: |
177 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); | 177 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
178 }; | 178 }; |
179 | 179 |
180 template <> | 180 template <> |
181 class BasicValueConverter<double> : public ValueConverter<double> { | 181 class BasicValueConverter<double> : public ValueConverter<double> { |
182 public: | 182 public: |
183 BasicValueConverter() {} | 183 BasicValueConverter() {} |
184 | 184 |
185 virtual bool Convert(const base::Value& value, double* field) const OVERRIDE { | 185 virtual bool Convert(const base::Value& value, double* field) const override { |
186 return value.GetAsDouble(field); | 186 return value.GetAsDouble(field); |
187 } | 187 } |
188 | 188 |
189 private: | 189 private: |
190 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); | 190 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
191 }; | 191 }; |
192 | 192 |
193 template <> | 193 template <> |
194 class BasicValueConverter<bool> : public ValueConverter<bool> { | 194 class BasicValueConverter<bool> : public ValueConverter<bool> { |
195 public: | 195 public: |
196 BasicValueConverter() {} | 196 BasicValueConverter() {} |
197 | 197 |
198 virtual bool Convert(const base::Value& value, bool* field) const OVERRIDE { | 198 virtual bool Convert(const base::Value& value, bool* field) const override { |
199 return value.GetAsBoolean(field); | 199 return value.GetAsBoolean(field); |
200 } | 200 } |
201 | 201 |
202 private: | 202 private: |
203 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); | 203 DISALLOW_COPY_AND_ASSIGN(BasicValueConverter); |
204 }; | 204 }; |
205 | 205 |
206 template <typename FieldType> | 206 template <typename FieldType> |
207 class ValueFieldConverter : public ValueConverter<FieldType> { | 207 class ValueFieldConverter : public ValueConverter<FieldType> { |
208 public: | 208 public: |
209 typedef bool(*ConvertFunc)(const base::Value* value, FieldType* field); | 209 typedef bool(*ConvertFunc)(const base::Value* value, FieldType* field); |
210 | 210 |
211 ValueFieldConverter(ConvertFunc convert_func) | 211 ValueFieldConverter(ConvertFunc convert_func) |
212 : convert_func_(convert_func) {} | 212 : convert_func_(convert_func) {} |
213 | 213 |
214 virtual bool Convert(const base::Value& value, | 214 virtual bool Convert(const base::Value& value, |
215 FieldType* field) const OVERRIDE { | 215 FieldType* field) const override { |
216 return convert_func_(&value, field); | 216 return convert_func_(&value, field); |
217 } | 217 } |
218 | 218 |
219 private: | 219 private: |
220 ConvertFunc convert_func_; | 220 ConvertFunc convert_func_; |
221 | 221 |
222 DISALLOW_COPY_AND_ASSIGN(ValueFieldConverter); | 222 DISALLOW_COPY_AND_ASSIGN(ValueFieldConverter); |
223 }; | 223 }; |
224 | 224 |
225 template <typename FieldType> | 225 template <typename FieldType> |
226 class CustomFieldConverter : public ValueConverter<FieldType> { | 226 class CustomFieldConverter : public ValueConverter<FieldType> { |
227 public: | 227 public: |
228 typedef bool(*ConvertFunc)(const StringPiece& value, FieldType* field); | 228 typedef bool(*ConvertFunc)(const StringPiece& value, FieldType* field); |
229 | 229 |
230 CustomFieldConverter(ConvertFunc convert_func) | 230 CustomFieldConverter(ConvertFunc convert_func) |
231 : convert_func_(convert_func) {} | 231 : convert_func_(convert_func) {} |
232 | 232 |
233 virtual bool Convert(const base::Value& value, | 233 virtual bool Convert(const base::Value& value, |
234 FieldType* field) const OVERRIDE { | 234 FieldType* field) const override { |
235 std::string string_value; | 235 std::string string_value; |
236 return value.GetAsString(&string_value) && | 236 return value.GetAsString(&string_value) && |
237 convert_func_(string_value, field); | 237 convert_func_(string_value, field); |
238 } | 238 } |
239 | 239 |
240 private: | 240 private: |
241 ConvertFunc convert_func_; | 241 ConvertFunc convert_func_; |
242 | 242 |
243 DISALLOW_COPY_AND_ASSIGN(CustomFieldConverter); | 243 DISALLOW_COPY_AND_ASSIGN(CustomFieldConverter); |
244 }; | 244 }; |
245 | 245 |
246 template <typename NestedType> | 246 template <typename NestedType> |
247 class NestedValueConverter : public ValueConverter<NestedType> { | 247 class NestedValueConverter : public ValueConverter<NestedType> { |
248 public: | 248 public: |
249 NestedValueConverter() {} | 249 NestedValueConverter() {} |
250 | 250 |
251 virtual bool Convert( | 251 virtual bool Convert( |
252 const base::Value& value, NestedType* field) const OVERRIDE { | 252 const base::Value& value, NestedType* field) const override { |
253 return converter_.Convert(value, field); | 253 return converter_.Convert(value, field); |
254 } | 254 } |
255 | 255 |
256 private: | 256 private: |
257 JSONValueConverter<NestedType> converter_; | 257 JSONValueConverter<NestedType> converter_; |
258 DISALLOW_COPY_AND_ASSIGN(NestedValueConverter); | 258 DISALLOW_COPY_AND_ASSIGN(NestedValueConverter); |
259 }; | 259 }; |
260 | 260 |
261 template <typename Element> | 261 template <typename Element> |
262 class RepeatedValueConverter : public ValueConverter<ScopedVector<Element> > { | 262 class RepeatedValueConverter : public ValueConverter<ScopedVector<Element> > { |
263 public: | 263 public: |
264 RepeatedValueConverter() {} | 264 RepeatedValueConverter() {} |
265 | 265 |
266 virtual bool Convert( | 266 virtual bool Convert( |
267 const base::Value& value, ScopedVector<Element>* field) const OVERRIDE { | 267 const base::Value& value, ScopedVector<Element>* field) const override { |
268 const base::ListValue* list = NULL; | 268 const base::ListValue* list = NULL; |
269 if (!value.GetAsList(&list)) { | 269 if (!value.GetAsList(&list)) { |
270 // The field is not a list. | 270 // The field is not a list. |
271 return false; | 271 return false; |
272 } | 272 } |
273 | 273 |
274 field->reserve(list->GetSize()); | 274 field->reserve(list->GetSize()); |
275 for (size_t i = 0; i < list->GetSize(); ++i) { | 275 for (size_t i = 0; i < list->GetSize(); ++i) { |
276 const base::Value* element = NULL; | 276 const base::Value* element = NULL; |
277 if (!list->Get(i, &element)) | 277 if (!list->Get(i, &element)) |
(...skipping 15 matching lines...) Expand all Loading... |
293 DISALLOW_COPY_AND_ASSIGN(RepeatedValueConverter); | 293 DISALLOW_COPY_AND_ASSIGN(RepeatedValueConverter); |
294 }; | 294 }; |
295 | 295 |
296 template <typename NestedType> | 296 template <typename NestedType> |
297 class RepeatedMessageConverter | 297 class RepeatedMessageConverter |
298 : public ValueConverter<ScopedVector<NestedType> > { | 298 : public ValueConverter<ScopedVector<NestedType> > { |
299 public: | 299 public: |
300 RepeatedMessageConverter() {} | 300 RepeatedMessageConverter() {} |
301 | 301 |
302 virtual bool Convert(const base::Value& value, | 302 virtual bool Convert(const base::Value& value, |
303 ScopedVector<NestedType>* field) const OVERRIDE { | 303 ScopedVector<NestedType>* field) const override { |
304 const base::ListValue* list = NULL; | 304 const base::ListValue* list = NULL; |
305 if (!value.GetAsList(&list)) | 305 if (!value.GetAsList(&list)) |
306 return false; | 306 return false; |
307 | 307 |
308 field->reserve(list->GetSize()); | 308 field->reserve(list->GetSize()); |
309 for (size_t i = 0; i < list->GetSize(); ++i) { | 309 for (size_t i = 0; i < list->GetSize(); ++i) { |
310 const base::Value* element = NULL; | 310 const base::Value* element = NULL; |
311 if (!list->Get(i, &element)) | 311 if (!list->Get(i, &element)) |
312 continue; | 312 continue; |
313 | 313 |
(...skipping 16 matching lines...) Expand all Loading... |
330 template <typename NestedType> | 330 template <typename NestedType> |
331 class RepeatedCustomValueConverter | 331 class RepeatedCustomValueConverter |
332 : public ValueConverter<ScopedVector<NestedType> > { | 332 : public ValueConverter<ScopedVector<NestedType> > { |
333 public: | 333 public: |
334 typedef bool(*ConvertFunc)(const base::Value* value, NestedType* field); | 334 typedef bool(*ConvertFunc)(const base::Value* value, NestedType* field); |
335 | 335 |
336 RepeatedCustomValueConverter(ConvertFunc convert_func) | 336 RepeatedCustomValueConverter(ConvertFunc convert_func) |
337 : convert_func_(convert_func) {} | 337 : convert_func_(convert_func) {} |
338 | 338 |
339 virtual bool Convert(const base::Value& value, | 339 virtual bool Convert(const base::Value& value, |
340 ScopedVector<NestedType>* field) const OVERRIDE { | 340 ScopedVector<NestedType>* field) const override { |
341 const base::ListValue* list = NULL; | 341 const base::ListValue* list = NULL; |
342 if (!value.GetAsList(&list)) | 342 if (!value.GetAsList(&list)) |
343 return false; | 343 return false; |
344 | 344 |
345 field->reserve(list->GetSize()); | 345 field->reserve(list->GetSize()); |
346 for (size_t i = 0; i < list->GetSize(); ++i) { | 346 for (size_t i = 0; i < list->GetSize(); ++i) { |
347 const base::Value* element = NULL; | 347 const base::Value* element = NULL; |
348 if (!list->Get(i, &element)) | 348 if (!list->Get(i, &element)) |
349 continue; | 349 continue; |
350 | 350 |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 | 518 |
519 private: | 519 private: |
520 ScopedVector<internal::FieldConverterBase<StructType> > fields_; | 520 ScopedVector<internal::FieldConverterBase<StructType> > fields_; |
521 | 521 |
522 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); | 522 DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); |
523 }; | 523 }; |
524 | 524 |
525 } // namespace base | 525 } // namespace base |
526 | 526 |
527 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ | 527 #endif // BASE_JSON_JSON_VALUE_CONVERTER_H_ |
OLD | NEW |