Skip to content

Commit

Permalink
json_schema_compiler: Use range based for loops.
Browse files Browse the repository at this point in the history
Review URL: https://codereview.chromium.org/853653002

Cr-Commit-Position: refs/heads/master@{#311811}
  • Loading branch information
leizleiz authored and Commit bot committed Jan 16, 2015
1 parent bc20dbe commit 51a694f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 25 deletions.
26 changes: 9 additions & 17 deletions tools/json_schema_compiler/cc_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,17 +385,12 @@ def _GenerateObjectTypeToValue(self, cpp_namespace, type_):
# maps, so we need to unwrap them.
needs_unwrap = (
not self._type_helper.IsCopyable(type_.additional_properties))
cpp_type = self._type_helper.GetCppType(type_.additional_properties,
is_in_container=True)
(c.Sblock('for (std::map<std::string, %s>::const_iterator it =' %
cpp_util.PadForGenerics(cpp_type))
.Append(' additional_properties.begin();')
.Append(' it != additional_properties.end(); ++it) {')
(c.Sblock('for (const auto& it : additional_properties) {')
.Cblock(self._CreateValueFromType(
'value->SetWithoutPathExpansion(it->first, %s);',
'value->SetWithoutPathExpansion(it.first, %s);',
type_.additional_properties.name,
type_.additional_properties,
'%sit->second' % ('*' if needs_unwrap else '')))
'%sit.second' % ('*' if needs_unwrap else '')))
.Eblock('}')
)

Expand Down Expand Up @@ -491,7 +486,7 @@ def _CreateValueFromType(self, code, prop_name, type_, var, is_ptr=False):
# not support passing a namespace as an argument.
item_type = self._type_helper.FollowRef(underlying_type.item_type)
if item_type.property_type == PropertyType.ENUM:
vardot = '(%s)%s' % (var, '->' if is_ptr else '.')
varname = ('*' if is_ptr else '') + '(%s)' % var

maybe_namespace = ''
if type_.item_type.property_type == PropertyType.REF:
Expand All @@ -501,11 +496,9 @@ def _CreateValueFromType(self, code, prop_name, type_, var, is_ptr=False):
# Scope the std::vector variable declaration inside braces.
(c.Sblock('{')
.Append('std::vector<std::string> %s;' % enum_list_var)
.Append('for (std::vector<%s>::const_iterator it = %sbegin();'
% (self._type_helper.GetCppType(item_type), vardot))
.Sblock(' it != %send(); ++it) {' % vardot)
.Append('%s.push_back(%sToString(*it));' % (enum_list_var,
maybe_namespace))
.Append('for (const auto& it : %s) {' % varname)
.Append('%s.push_back(%sToString(it));' % (enum_list_var,
maybe_namespace))
.Eblock('}'))

# Because the std::vector above is always created for both required and
Expand Down Expand Up @@ -853,11 +846,10 @@ def _GenerateListValueToEnumArrayConversion(self,
cpp_type = self._type_helper.GetCppType(item_type, is_in_container=True)
c.Append('%s.reset(new std::vector<%s>);' %
(dst_var, cpp_util.PadForGenerics(cpp_type)))
(c.Sblock('for (base::ListValue::const_iterator it = %s->begin(); '
'it != %s->end(); ++it) {' % (src_var, src_var))
(c.Sblock('for (const auto& it : *(%s)) {' % src_var)
.Append('%s tmp;' % self._type_helper.GetCppType(item_type))
.Concat(self._GenerateStringToEnumConversion(item_type,
'(*it)',
'(it)',
'tmp',
failure_value))
.Append('%s%spush_back(tmp);' % (dst_var, accessor))
Expand Down
7 changes: 2 additions & 5 deletions tools/json_schema_compiler/features_cc_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,8 @@ def Generate(self):
'const std::string& id) const {'
% (self._class_name, self._class_name))
.Sblock()
.Append('std::map<std::string, %s::ID>::const_iterator it'
' = features_.find(id);' % self._class_name)
.Append('if (it == features_.end())')
.Append(' return kUnknown;')
.Append('return it->second;')
.Append('const auto& it = features_.find(id);' % self._class_name)
.Append('return (it == features_.end()) ? kUnknown : it->second;')
.Eblock()
.Append('}')
.Append()
Expand Down
5 changes: 2 additions & 3 deletions tools/json_schema_compiler/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@ void PopulateListFromArray(
const std::vector<T>& from,
base::ListValue* out) {
out->Clear();
for (typename std::vector<T>::const_iterator it = from.begin();
it != from.end(); ++it) {
AddItemToList(*it, out);
for (const auto& it : from) {
AddItemToList(it, out);
}
}

Expand Down

0 comments on commit 51a694f

Please sign in to comment.