Commit fcf06a7b authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Modernize C++ code generated by json_schema_compiler.

- Use nullptr
- Use std::make_unique()
- Use auto
- Use = default;
- Use modern copyright notice
- Stop using DISALLOW_COPY_AND_ASSIGN()

Bug: 1010217
Change-Id: Ib3e5ec1715b33325f11f391bb749e3e014c1ba4d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2495978Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821010}
parent 6b42b958
...@@ -135,7 +135,7 @@ class _Generator(object): ...@@ -135,7 +135,7 @@ class _Generator(object):
(c.Append('%s::%s()' % (classname_in_namespace, classname)) (c.Append('%s::%s()' % (classname_in_namespace, classname))
.Cblock(self._GenerateInitializersAndBody(type_)) .Cblock(self._GenerateInitializersAndBody(type_))
.Append('%s::~%s() {}' % (classname_in_namespace, classname)) .Append('%s::~%s() = default;' % (classname_in_namespace, classname))
) )
# Note: we use 'rhs' because some API objects have a member 'other'. # Note: we use 'rhs' because some API objects have a member 'other'.
(c.Append('%s::%s(%s&& rhs)' % (c.Append('%s::%s(%s&& rhs)' %
...@@ -335,7 +335,7 @@ class _Generator(object): ...@@ -335,7 +335,7 @@ class _Generator(object):
.Eblock('}')) .Eblock('}'))
if type_.properties or type_.additional_properties is not None: if type_.properties or type_.additional_properties is not None:
c.Append('const base::DictionaryValue* dict = ' c.Append('const auto* dict = '
'static_cast<const base::DictionaryValue*>(&value);') 'static_cast<const base::DictionaryValue*>(&value);')
if self._generate_error_messages: if self._generate_error_messages:
c.Append('std::set<std::string> keys;') c.Append('std::set<std::string> keys;')
...@@ -393,7 +393,7 @@ class _Generator(object): ...@@ -393,7 +393,7 @@ class _Generator(object):
""" """
c = Code() c = Code()
value_var = prop.unix_name + '_value' value_var = prop.unix_name + '_value'
c.Append('const base::Value* %(value_var)s = NULL;') c.Append('const base::Value* %(value_var)s = nullptr;')
if prop.optional: if prop.optional:
(c.Sblock( (c.Sblock(
'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {')
...@@ -437,7 +437,7 @@ class _Generator(object): ...@@ -437,7 +437,7 @@ class _Generator(object):
) )
if self._generate_error_messages: if self._generate_error_messages:
c.Append('DCHECK(error);') c.Append('DCHECK(error);')
(c.Append(' std::unique_ptr<%s> out(new %s());' % (classname, classname)) (c.Append(' auto out = std::make_unique<%s>();' % classname)
.Append(' if (!Populate(%s))' % self._GenerateArgs( .Append(' if (!Populate(%s))' % self._GenerateArgs(
('value', 'out.get()'))) ('value', 'out.get()')))
.Append(' return nullptr;') .Append(' return nullptr;')
...@@ -681,8 +681,8 @@ class _Generator(object): ...@@ -681,8 +681,8 @@ class _Generator(object):
c = Code() c = Code()
(c.Sblock('std::unique_ptr<base::DictionaryValue> %s::ToValue() const {' % (c.Sblock('std::unique_ptr<base::DictionaryValue> %s::ToValue() const {' %
cpp_namespace) cpp_namespace)
.Append('std::unique_ptr<base::DictionaryValue> to_value_result(' .Append('auto to_value_result =')
'new base::DictionaryValue());') .Append(' std::make_unique<base::DictionaryValue>();')
.Append() .Append()
) )
...@@ -772,8 +772,8 @@ class _Generator(object): ...@@ -772,8 +772,8 @@ class _Generator(object):
# Params::Populate function # Params::Populate function
if function.params: if function.params:
c.Concat(self._GeneratePropertyFunctions('Params', function.params)) c.Concat(self._GeneratePropertyFunctions('Params', function.params))
(c.Append('Params::Params() {}') (c.Append('Params::Params() = default;')
.Append('Params::~Params() {}') .Append('Params::~Params() = default;')
.Append() .Append()
.Cblock(self._GenerateFunctionParamsCreate(function)) .Cblock(self._GenerateFunctionParamsCreate(function))
) )
...@@ -875,16 +875,11 @@ class _Generator(object): ...@@ -875,16 +875,11 @@ class _Generator(object):
var = '*%s' % var var = '*%s' % var
return 'std::make_unique<base::Value>(%s)' % var return 'std::make_unique<base::Value>(%s)' % var
elif underlying_type.property_type == PropertyType.ARRAY: elif underlying_type.property_type == PropertyType.ARRAY:
return '%s' % self._util_cc_helper.CreateValueFromArray( return '%s' % self._util_cc_helper.CreateValueFromArray(var, is_ptr)
var,
is_ptr)
elif underlying_type.property_type.is_fundamental: elif underlying_type.property_type.is_fundamental:
if is_ptr: if is_ptr:
var = '*%s' % var var = '*%s' % var
if underlying_type.property_type == PropertyType.STRING: return 'std::make_unique<base::Value>(%s)' % var
return 'std::make_unique<base::Value>(%s)' % var
else:
return 'std::make_unique<base::Value>(%s)' % var
else: else:
raise NotImplementedError('Conversion of %s to base::Value not ' raise NotImplementedError('Conversion of %s to base::Value not '
'implemented' % repr(type_.type_)) 'implemented' % repr(type_.type_))
...@@ -945,7 +940,7 @@ class _Generator(object): ...@@ -945,7 +940,7 @@ class _Generator(object):
failure_value = 'std::unique_ptr<Params>()' failure_value = 'std::unique_ptr<Params>()'
c.Append() c.Append()
value_var = param.unix_name + '_value' value_var = param.unix_name + '_value'
(c.Append('const base::Value* %(value_var)s = NULL;') (c.Append('const base::Value* %(value_var)s = nullptr;')
.Append('if (args.Get(%(i)s, &%(value_var)s) &&') .Append('if (args.Get(%(i)s, &%(value_var)s) &&')
.Sblock(' !%(value_var)s->is_none()) {') .Sblock(' !%(value_var)s->is_none()) {')
.Concat(self._GeneratePopulatePropertyFromValue( .Concat(self._GeneratePopulatePropertyFromValue(
...@@ -989,7 +984,7 @@ class _Generator(object): ...@@ -989,7 +984,7 @@ class _Generator(object):
failure_value, failure_value,
is_ptr=False): is_ptr=False):
"""Generates code to populate a variable |dst_var| of type |type_| from a """Generates code to populate a variable |dst_var| of type |type_| from a
Value* at |src_var|. The Value* is assumed to be non-NULL. In the generated Value* at |src_var|. The Value* is assumed to be non-null. In the generated
code, if |dst_var| fails to be populated then Populate will return code, if |dst_var| fails to be populated then Populate will return
|failure_value|. |failure_value|.
""" """
...@@ -1012,7 +1007,7 @@ class _Generator(object): ...@@ -1012,7 +1007,7 @@ class _Generator(object):
c.Append('return %(failure_value)s;') c.Append('return %(failure_value)s;')
(c.Eblock('}') (c.Eblock('}')
.Append('else') .Append('else')
.Append(' %(dst_var)s.reset(new %(cpp_type)s(temp));') .Append(' %(dst_var)s = std::make_unique<%(cpp_type)s>(temp);')
) )
else: else:
(c.Sblock('if (!%s) {' % cpp_util.GetAsFundamentalValue( (c.Sblock('if (!%s) {' % cpp_util.GetAsFundamentalValue(
...@@ -1029,7 +1024,7 @@ class _Generator(object): ...@@ -1029,7 +1024,7 @@ class _Generator(object):
) )
elif underlying_type.property_type == PropertyType.OBJECT: elif underlying_type.property_type == PropertyType.OBJECT:
if is_ptr: if is_ptr:
(c.Append('const base::DictionaryValue* dictionary = NULL;') (c.Append('const base::DictionaryValue* dictionary = nullptr;')
.Sblock('if (!%(src_var)s->GetAsDictionary(&dictionary)) {') .Sblock('if (!%(src_var)s->GetAsDictionary(&dictionary)) {')
.Concat(self._GenerateError( .Concat(self._GenerateError(
'"\'%%(key)s\': expected dictionary, got " + ' + '"\'%%(key)s\': expected dictionary, got " + ' +
...@@ -1041,7 +1036,7 @@ class _Generator(object): ...@@ -1041,7 +1036,7 @@ class _Generator(object):
c.Append('return %(failure_value)s;') c.Append('return %(failure_value)s;')
(c.Eblock('}') (c.Eblock('}')
.Sblock('else {') .Sblock('else {')
.Append('std::unique_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') .Append('auto temp = std::make_unique<%(cpp_type)s>();')
.Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs( .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs(
('*dictionary', 'temp.get()'))) ('*dictionary', 'temp.get()')))
.Append(' return %(failure_value)s;') .Append(' return %(failure_value)s;')
...@@ -1052,7 +1047,7 @@ class _Generator(object): ...@@ -1052,7 +1047,7 @@ class _Generator(object):
.Eblock('}') .Eblock('}')
) )
else: else:
(c.Append('const base::DictionaryValue* dictionary = NULL;') (c.Append('const base::DictionaryValue* dictionary = nullptr;')
.Sblock('if (!%(src_var)s->GetAsDictionary(&dictionary)) {') .Sblock('if (!%(src_var)s->GetAsDictionary(&dictionary)) {')
.Concat(self._GenerateError( .Concat(self._GenerateError(
'"\'%%(key)s\': expected dictionary, got " + ' + '"\'%%(key)s\': expected dictionary, got " + ' +
...@@ -1066,12 +1061,12 @@ class _Generator(object): ...@@ -1066,12 +1061,12 @@ class _Generator(object):
) )
elif underlying_type.property_type == PropertyType.FUNCTION: elif underlying_type.property_type == PropertyType.FUNCTION:
if is_ptr: if is_ptr:
c.Append('%(dst_var)s.reset(new base::DictionaryValue());') c.Append('%(dst_var)s = std::make_unique<base::DictionaryValue>();')
elif underlying_type.property_type == PropertyType.ANY: elif underlying_type.property_type == PropertyType.ANY:
c.Append('%(dst_var)s = %(src_var)s->CreateDeepCopy();') c.Append('%(dst_var)s = %(src_var)s->CreateDeepCopy();')
elif underlying_type.property_type == PropertyType.ARRAY: elif underlying_type.property_type == PropertyType.ARRAY:
# util_cc_helper deals with optional and required arrays # util_cc_helper deals with optional and required arrays
(c.Append('const base::ListValue* list = NULL;') (c.Append('const base::ListValue* list = nullptr;')
.Sblock('if (!%(src_var)s->GetAsList(&list)) {') .Sblock('if (!%(src_var)s->GetAsList(&list)) {')
.Concat(self._GenerateError( .Concat(self._GenerateError(
'"\'%%(key)s\': expected list, got " + ' + '"\'%%(key)s\': expected list, got " + ' +
...@@ -1105,7 +1100,7 @@ class _Generator(object): ...@@ -1105,7 +1100,7 @@ class _Generator(object):
c.Eblock('}') c.Eblock('}')
elif underlying_type.property_type == PropertyType.CHOICES: elif underlying_type.property_type == PropertyType.CHOICES:
if is_ptr: if is_ptr:
(c.Append('std::unique_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') (c.Append('auto temp = std::make_unique<%(cpp_type)s>();')
.Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( .Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs(
('*%(src_var)s', 'temp.get()'))) ('*%(src_var)s', 'temp.get()')))
.Append(' return %(failure_value)s;') .Append(' return %(failure_value)s;')
...@@ -1132,8 +1127,8 @@ class _Generator(object): ...@@ -1132,8 +1127,8 @@ class _Generator(object):
.Sblock('else {') .Sblock('else {')
) )
if is_ptr: if is_ptr:
c.Append('%(dst_var)s.reset(new std::vector<uint8_t>(' c.Append('%(dst_var)s = std::make_unique<std::vector<uint8_t>>('
'%(src_var)s->GetBlob()));') '%(src_var)s->GetBlob());')
else: else:
c.Append('%(dst_var)s = %(src_var)s->GetBlob();') c.Append('%(dst_var)s = %(src_var)s->GetBlob();')
c.Eblock('}') c.Eblock('}')
...@@ -1165,7 +1160,7 @@ class _Generator(object): ...@@ -1165,7 +1160,7 @@ class _Generator(object):
if is_ptr: if is_ptr:
accessor = '->' accessor = '->'
cpp_type = self._type_helper.GetCppType(item_type, is_in_container=True) cpp_type = self._type_helper.GetCppType(item_type, is_in_container=True)
c.Append('%s.reset(new std::vector<%s>);' % c.Append('%s = std::make_unique<std::vector<%s>>();' %
(dst_var, cpp_type)) (dst_var, cpp_type))
(c.Sblock('for (const auto& it : *(%s)) {' % src_var) (c.Sblock('for (const auto& it : *(%s)) {' % src_var)
.Append('%s tmp;' % self._type_helper.GetCppType(item_type)) .Append('%s tmp;' % self._type_helper.GetCppType(item_type))
...@@ -1309,8 +1304,7 @@ class _Generator(object): ...@@ -1309,8 +1304,7 @@ class _Generator(object):
(c.Sblock('std::unique_ptr<base::ListValue> %(function_scope)s' (c.Sblock('std::unique_ptr<base::ListValue> %(function_scope)s'
'Create(%(declaration_list)s) {') 'Create(%(declaration_list)s) {')
.Append('std::unique_ptr<base::ListValue> create_results(' .Append('auto create_results = std::make_unique<base::ListValue>();')
'new base::ListValue());')
) )
declaration_list = [] declaration_list = []
for param in params: for param in params:
......
...@@ -12,7 +12,7 @@ import os ...@@ -12,7 +12,7 @@ import os
import re import re
CHROMIUM_LICENSE = ( CHROMIUM_LICENSE = (
"""// Copyright (c) %d The Chromium Authors. All rights reserved. """// Copyright %d The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.""" % datetime.now().year // found in the LICENSE file.""" % datetime.now().year
) )
......
...@@ -116,6 +116,7 @@ class _Generator(object): ...@@ -116,6 +116,7 @@ class _Generator(object):
for event in self._namespace.events.values(): for event in self._namespace.events.values():
c.Cblock(self._GenerateEvent(event)) c.Cblock(self._GenerateEvent(event))
(c.Concat(cpp_util.CloseNamespace(cpp_namespace)) (c.Concat(cpp_util.CloseNamespace(cpp_namespace))
.Append()
.Append('#endif // %s' % ifndef_name) .Append('#endif // %s' % ifndef_name)
.Append() .Append()
) )
...@@ -232,6 +233,8 @@ class _Generator(object): ...@@ -232,6 +233,8 @@ class _Generator(object):
(c.Sblock('struct %(classname)s {') (c.Sblock('struct %(classname)s {')
.Append('%(classname)s();') .Append('%(classname)s();')
.Append('~%(classname)s();') .Append('~%(classname)s();')
.Append('%(classname)s(const %(classname)s&) = delete;')
.Append('%(classname)s& operator=(const %(classname)s&) = delete;')
.Append('%(classname)s(%(classname)s&& rhs);') .Append('%(classname)s(%(classname)s&& rhs);')
.Append('%(classname)s& operator=(%(classname)s&& rhs);') .Append('%(classname)s& operator=(%(classname)s&& rhs);')
) )
...@@ -293,12 +296,7 @@ class _Generator(object): ...@@ -293,12 +296,7 @@ class _Generator(object):
self._type_helper.GetCppType(type_.additional_properties, self._type_helper.GetCppType(type_.additional_properties,
is_in_container=True)) is_in_container=True))
) )
(c.Eblock() (c.Eblock('};'))
.Append()
.Sblock(' private:')
.Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);')
.Eblock('};')
)
return c.Substitute({'classname': classname}) return c.Substitute({'classname': classname})
def _GenerateEvent(self, event): def _GenerateEvent(self, event):
...@@ -344,6 +342,8 @@ class _Generator(object): ...@@ -344,6 +342,8 @@ class _Generator(object):
(c.Sblock('struct Params {') (c.Sblock('struct Params {')
.Append('static std::unique_ptr<Params> Create(%s);' % .Append('static std::unique_ptr<Params> Create(%s);' %
self._GenerateParams(('const base::ListValue& args',))) self._GenerateParams(('const base::ListValue& args',)))
.Append('Params(const Params&) = delete;')
.Append('Params& operator=(const Params&) = delete;')
.Append('~Params();') .Append('~Params();')
.Append() .Append()
.Cblock(self._GenerateTypes(p.type_ for p in function.params)) .Cblock(self._GenerateTypes(p.type_ for p in function.params))
...@@ -352,8 +352,6 @@ class _Generator(object): ...@@ -352,8 +352,6 @@ class _Generator(object):
.Append() .Append()
.Sblock(' private:') .Sblock(' private:')
.Append('Params();') .Append('Params();')
.Append()
.Append('DISALLOW_COPY_AND_ASSIGN(Params);')
.Eblock('};') .Eblock('};')
) )
return c return c
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment