| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 import argparse | 5 import argparse |
| 6 import copy | 6 import copy |
| 7 from datetime import datetime | 7 from datetime import datetime |
| 8 import os | 8 import os |
| 9 | 9 |
| 10 from code import Code | 10 from code import Code |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 """Parses the feature from the given json value.""" | 350 """Parses the feature from the given json value.""" |
| 351 for key in parsed_json.keys(): | 351 for key in parsed_json.keys(): |
| 352 if key not in FEATURE_GRAMMAR: | 352 if key not in FEATURE_GRAMMAR: |
| 353 self._AddError(key, 'Unrecognized key') | 353 self._AddError(key, 'Unrecognized key') |
| 354 for key, key_grammar in FEATURE_GRAMMAR.iteritems(): | 354 for key, key_grammar in FEATURE_GRAMMAR.iteritems(): |
| 355 self._ParseKey(key, parsed_json, key_grammar) | 355 self._ParseKey(key, parsed_json, key_grammar) |
| 356 | 356 |
| 357 def GetCode(self, feature_class): | 357 def GetCode(self, feature_class): |
| 358 """Returns the Code object for generating this feature.""" | 358 """Returns the Code object for generating this feature.""" |
| 359 c = Code() | 359 c = Code() |
| 360 c.Append('%s* feature = new %s();' % (feature_class, feature_class)) | 360 c.Append('std::unique_ptr<%s> feature(new %s());' % |
| 361 (feature_class, feature_class)) |
| 361 c.Append('feature->set_name("%s");' % self.name) | 362 c.Append('feature->set_name("%s");' % self.name) |
| 362 for key in sorted(self.feature_values.keys()): | 363 for key in sorted(self.feature_values.keys()): |
| 363 if key in IGNORED_KEYS: | 364 if key in IGNORED_KEYS: |
| 364 continue; | 365 continue; |
| 365 c.Append('feature->set_%s(%s);' % (key, self.feature_values[key])) | 366 c.Append('feature->set_%s(%s);' % (key, self.feature_values[key])) |
| 366 return c | 367 return c |
| 367 | 368 |
| 368 class FeatureCompiler(object): | 369 class FeatureCompiler(object): |
| 369 """A compiler to load, parse, and generate C++ code for a number of | 370 """A compiler to load, parse, and generate C++ code for a number of |
| 370 features.json files.""" | 371 features.json files.""" |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 def Render(self): | 479 def Render(self): |
| 479 """Returns the Code object for the body of the .cc file, which handles the | 480 """Returns the Code object for the body of the .cc file, which handles the |
| 480 initialization of all features.""" | 481 initialization of all features.""" |
| 481 c = Code() | 482 c = Code() |
| 482 c.Append('%s::%s() {' % (self._provider_class, self._provider_class)) | 483 c.Append('%s::%s() {' % (self._provider_class, self._provider_class)) |
| 483 c.Sblock() | 484 c.Sblock() |
| 484 for k in sorted(self._features.keys()): | 485 for k in sorted(self._features.keys()): |
| 485 c.Sblock('{') | 486 c.Sblock('{') |
| 486 feature = self._features[k] | 487 feature = self._features[k] |
| 487 if type(feature) is list: | 488 if type(feature) is list: |
| 488 c.Append('std::vector<Feature*> features;') | 489 c.Append('std::unique_ptr<ComplexFeature::FeatureList> features(') |
| 490 c.Append(' new ComplexFeature::FeatureList());') |
| 489 for f in feature: | 491 for f in feature: |
| 490 c.Sblock('{') | 492 c.Sblock('{') |
| 491 c.Concat(f.GetCode(self._feature_class)) | 493 c.Concat(f.GetCode(self._feature_class)) |
| 492 c.Append('features.push_back(feature);') | 494 c.Append('features->push_back(std::move(feature));') |
| 493 c.Eblock('}') | 495 c.Eblock('}') |
| 494 c.Append('ComplexFeature* feature(new ComplexFeature(&features));') | 496 c.Append('std::unique_ptr<ComplexFeature> feature(') |
| 497 c.Append(' new ComplexFeature(std::move(features)));') |
| 495 c.Append('feature->set_name("%s");' % k) | 498 c.Append('feature->set_name("%s");' % k) |
| 496 else: | 499 else: |
| 497 c.Concat(feature.GetCode(self._feature_class)) | 500 c.Concat(feature.GetCode(self._feature_class)) |
| 498 c.Append('AddFeature("%s", feature);' % k) | 501 c.Append('AddFeature("%s", std::move(feature));' % k) |
| 499 c.Eblock('}') | 502 c.Eblock('}') |
| 500 c.Eblock('}') | 503 c.Eblock('}') |
| 501 return c | 504 return c |
| 502 | 505 |
| 503 def Write(self): | 506 def Write(self): |
| 504 """Writes the output.""" | 507 """Writes the output.""" |
| 505 header_file_path = self._out_base_filename + '.h' | 508 header_file_path = self._out_base_filename + '.h' |
| 506 cc_file_path = self._out_base_filename + '.cc' | 509 cc_file_path = self._out_base_filename + '.cc' |
| 507 substitutions = ({ | 510 substitutions = ({ |
| 508 'header_file_path': header_file_path, | 511 'header_file_path': header_file_path, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 'out_base_filename', type=str, | 551 'out_base_filename', type=str, |
| 549 help='The base filename for the C++ files (.h and .cc will be appended)') | 552 help='The base filename for the C++ files (.h and .cc will be appended)') |
| 550 parser.add_argument('source_files', type=str, nargs='+', | 553 parser.add_argument('source_files', type=str, nargs='+', |
| 551 help='The source features.json files') | 554 help='The source features.json files') |
| 552 args = parser.parse_args() | 555 args = parser.parse_args() |
| 553 c = FeatureCompiler(args.chrome_root, args.source_files, args.feature_class, | 556 c = FeatureCompiler(args.chrome_root, args.source_files, args.feature_class, |
| 554 args.provider_class, args.out_root, | 557 args.provider_class, args.out_root, |
| 555 args.out_base_filename) | 558 args.out_base_filename) |
| 556 c.Compile() | 559 c.Compile() |
| 557 c.Write() | 560 c.Write() |
| OLD | NEW |