Index: base/json/json_value_converter.h |
diff --git a/base/json/json_value_converter.h b/base/json/json_value_converter.h |
index e933dadad5c4812f13526930ef374be89a391ade..d9daa92576e96d905a5cfccc01bdf7a2c4a65cba 100644 |
--- a/base/json/json_value_converter.h |
+++ b/base/json/json_value_converter.h |
@@ -13,6 +13,7 @@ |
#include "base/basictypes.h" |
#include "base/logging.h" |
#include "base/memory/scoped_ptr.h" |
+#include "base/memory/scoped_vector.h" |
#include "base/stl_util.h" |
#include "base/string16.h" |
#include "base/values.h" |
@@ -63,9 +64,11 @@ |
// } |
// }; |
// |
-// For repeated field, we just assume std::vector for its container |
-// and you can put RegisterRepeatedInt or some other types. Use |
-// RegisterRepeatedMessage for nested repeated fields. |
+// For repeated field, we just assume ScopedVector for its container |
+// and you can use RegisterRepeatedMessage (not std::vector to avoid |
+// copy constructors). For repeated fields of basic types such like |
+// int, we assume std::vector for its container and you can use |
+// RegisterRepeatedInt and so on. |
// |
namespace base { |
@@ -222,11 +225,12 @@ class RepeatedValueConverter : public ValueConverter<std::vector<Element> > { |
continue; |
Element e; |
- if (!basic_converter_.Convert(*element, &e)) { |
+ if (basic_converter_.Convert(*element, &e)) { |
+ field->push_back(e); |
+ } else { |
DVLOG(1) << "failure at " << i << "-th element"; |
return false; |
} |
- field->push_back(e); |
} |
return true; |
} |
@@ -238,12 +242,12 @@ class RepeatedValueConverter : public ValueConverter<std::vector<Element> > { |
template <typename NestedType> |
class RepeatedMessageConverter |
- : public ValueConverter<std::vector<NestedType> > { |
+ : public ValueConverter<ScopedVector<NestedType> > { |
public: |
RepeatedMessageConverter() {} |
- virtual bool Convert( |
- const base::Value& value, std::vector<NestedType>* field) const OVERRIDE { |
+ virtual bool Convert(const base::Value& value, |
+ ScopedVector<NestedType>* field) const OVERRIDE { |
const base::ListValue* list = NULL; |
if (!value.GetAsList(&list)) |
return false; |
@@ -254,8 +258,10 @@ class RepeatedMessageConverter |
if (!list->Get(i, &element)) |
continue; |
- field->push_back(NestedType()); |
- if (!converter_.Convert(*element, &field->back())) { |
+ NestedType* nested = new NestedType(); |
+ if (converter_.Convert(*element, nested)) { |
+ field->push_back(nested); |
+ } else { |
DVLOG(1) << "failure at " << i << "-th element"; |
return false; |
} |
@@ -277,10 +283,6 @@ class JSONValueConverter { |
StructType::RegisterJSONConverter(this); |
} |
- ~JSONValueConverter() { |
- STLDeleteContainerPointers(fields_.begin(), fields_.end()); |
- } |
- |
void RegisterIntField(const std::string& field_name, |
int StructType::* field) { |
fields_.push_back(new internal::FieldConverter<StructType, int>( |
@@ -361,9 +363,9 @@ class JSONValueConverter { |
template <class NestedType> |
void RegisterRepeatedMessage(const std::string& field_name, |
- std::vector<NestedType> StructType::* field) { |
+ ScopedVector<NestedType> StructType::* field) { |
fields_.push_back( |
- new internal::FieldConverter<StructType, std::vector<NestedType> >( |
+ new internal::FieldConverter<StructType, ScopedVector<NestedType> >( |
field_name, |
field, |
new internal::RepeatedMessageConverter<NestedType>)); |
@@ -374,12 +376,12 @@ class JSONValueConverter { |
if (!value.GetAsDictionary(&dictionary_value)) |
return false; |
- for(std::vector<internal::FieldConverterBase*>::const_iterator it = |
- fields_.begin(); it != fields_.end(); ++it) { |
+ for(size_t i = 0; i < fields_.size(); ++i) { |
+ const internal::FieldConverterBase* field_converter = fields_[i]; |
base::Value* field = NULL; |
- if (dictionary_value->Get((*it)->field_path(), &field)) { |
- if (!(*it)->ConvertField(*field, output)) { |
- DVLOG(1) << "failure at field " << (*it)->field_path(); |
+ if (dictionary_value->Get(field_converter->field_path(), &field)) { |
+ if (!field_converter->ConvertField(*field, output)) { |
+ DVLOG(1) << "failure at field " << field_converter->field_path(); |
return false; |
} |
} |
@@ -388,7 +390,7 @@ class JSONValueConverter { |
} |
private: |
- std::vector<internal::FieldConverterBase*> fields_; |
+ ScopedVector<internal::FieldConverterBase> fields_; |
DISALLOW_COPY_AND_ASSIGN(JSONValueConverter); |
}; |