Chromium Code Reviews| Index: tools/json_schema_compiler/features_cc_generator.py |
| diff --git a/tools/json_schema_compiler/features_cc_generator.py b/tools/json_schema_compiler/features_cc_generator.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..58dc4eb648eca7729bed3bb0ff3527e658a7aa79 |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/features_cc_generator.py |
| @@ -0,0 +1,83 @@ |
| +# Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import os.path |
| + |
| +from code import Code |
| +import cpp_util |
| + |
| + |
| +class CCGenerator(object): |
| + def Generate(self, permission_defs, source_file): |
| + return _Generator(permission_defs, source_file).Generate() |
| + |
| + |
| +class _Generator(object): |
| + """A .cc generator for PermissionFeatures. |
| + """ |
| + def __init__(self, permission_defs, source_file): |
| + self._feature_defs = cpp_util.RemoveFeatureDuplicates(permission_defs) |
| + self._source_file = source_file |
| + self._source_file_filename = os.path.splitext(source_file)[0] |
| + |
| + def Generate(self): |
| + """Generates a Code object for PermissionFeatures. |
| + """ |
| + c = Code() |
| + (c.Append(cpp_util.CHROMIUM_LICENSE) |
| + .Append() |
| + .Append(cpp_util.GENERATED_FEATURE_MESSAGE % self._source_file) |
| + .Append() |
| + .Append('#include <string>') |
| + .Append() |
| + .Append('#include "%s.h"' % self._source_file_filename) |
| + .Append() |
| + .Append('#include "base/logging.h"') |
| + .Append() |
| + ) |
| + |
| + # Generate the ToString function. |
| + (c.Append('std::string PermissionFeatures::ToString(' \ |
| + 'PermissionFeatures::ID id) {') |
| + .Sblock() |
| + .Append('switch (id) {') |
| + .Sblock() |
| + ) |
| + for feature in self._feature_defs: |
| + c.Append('case %s: return "%s";' % (feature.constant_name, |
| + feature.name)) |
| + (c.Append('default: ') |
|
not at google - send to devlin
2013/09/12 16:20:43
no default:, let compiler fail, which.. it shouldn
dhnishi (use Chromium)
2013/09/13 17:36:42
Done.
|
| + .Sblock() |
| + .Append('NOTREACHED();') |
| + .Append('return "";') |
| + .Eblock() |
| + ) |
| + (c.Eblock() |
| + .Append('}') |
| + .Eblock() |
| + .Append('}') |
| + .Append() |
| + ) |
| + |
| + # Generate the FromString function. |
| + (c.Append('PermissionFeatures::ID PermissionFeatures::FromString(' \ |
| + 'const std::string& id) {') |
| + .Sblock() |
| + ) |
| + for feature in self._feature_defs: |
| + c.Append('if (id == "%s") return %s;' % (feature.name, |
| + feature.constant_name)) |
|
not at google - send to devlin
2013/09/12 16:20:43
prefer either:
c.Append('if (id == "%s") return %
dhnishi (use Chromium)
2013/09/13 17:36:42
Done.
|
| + (c.Append() |
| + .Append('NOTREACHED();') |
| + .Append('return kUnknown;') |
| + .Eblock() |
| + .Append('}') |
| + .Append() |
| + ) |
|
not at google - send to devlin
2013/09/12 16:20:43
generate a std::map<std::string, ID> on constructi
dhnishi (use Chromium)
2013/09/13 17:36:42
Done. I worried that I'm breaking design patterns
|
| + |
| + (c.Eblock(None, 1) # public |
| + .Eblock() |
| + .Eblock(None, 1) # class PermissionFeatures |
|
not at google - send to devlin
2013/09/12 16:20:43
not sure what this is
dhnishi (use Chromium)
2013/09/13 17:36:42
Whoops. That was a remnant from an older revision.
|
| + ) |
| + return c |