Chromium Code Reviews| Index: tools/json_schema_compiler/features_h_generator.py |
| diff --git a/tools/json_schema_compiler/features_h_generator.py b/tools/json_schema_compiler/features_h_generator.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ab46e7bd50bf361abceaa8213cb60b84cdcd94fa |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/features_h_generator.py |
| @@ -0,0 +1,89 @@ |
| +# 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 HGenerator(object): |
| + def Generate(self, features, source_file): |
| + return _Generator(features, source_file).Generate() |
| + |
| + |
| +class _Generator(object): |
| + """A .cc generator for PermissionFeatures. |
| + """ |
| + def __init__(self, features, source_file): |
| + self._feature_defs = features |
| + self._source_file = source_file |
| + self._source_file_filename, _ = os.path.splitext(source_file) |
| + |
| + 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() |
| + ) |
| + ifndef_name = cpp_util.GenerateIfndefName(self._source_file_filename, |
| + "PermissionFeatures") |
| + (c.Append('#ifndef %s' % ifndef_name) |
| + .Append('#define %s' % ifndef_name) |
| + .Append() |
| + ) |
| + |
| + (c.Append('#include <map>') |
| + .Append() |
| + ) |
| + |
| + (c.Append('class PermissionFeatures {') |
| + .Append(' public:') |
| + .Sblock() |
| + .Concat(self._GeneratePublicBody()) |
| + .Eblock() |
| + .Append(' private:') |
| + .Sblock() |
| + .Concat(self._GeneratePrivateBody()) |
| + .Eblock('};') |
| + ) |
| + (c.Append() |
| + .Append('#endif // %s' % ifndef_name) |
| + .Append() |
| + ) |
| + return c |
| + |
| + def _GeneratePublicBody(self): |
| + c = Code() |
| + |
| + (c.Append('PermissionFeatures();') |
| + .Append() |
| + .Append('enum ID {') |
| + .Concat(self._GenerateEnumConstants()) |
| + .Eblock('};') |
| + .Append() |
| + .Append('const char* ToString(const ID id);') |
|
not at google - send to devlin
2013/09/17 18:26:19
const char* ToString(ID id) const;
dhnishi (use Chromium)
2013/09/18 18:32:45
Done.
|
| + .Append('const ID FromString(const std::string& id);') |
|
not at google - send to devlin
2013/09/17 18:26:19
ID FromString(const std::string& id) const;
dhnishi (use Chromium)
2013/09/18 18:32:45
Done.
|
| + .Append() |
| + ) |
| + return c |
| + |
| + def _GeneratePrivateBody(self): |
| + return Code().Append('std::map<std::string, ' |
| + 'PermissionFeatures::ID> features_;') |
| + |
| + def _GenerateEnumConstants(self): |
| + c = Code() |
| + |
| + (c.Sblock() |
| + .Append('kUnknown,') |
| + ) |
| + for feature in self._feature_defs: |
| + c.Append('%s,' % cpp_util.ConstantName(feature.name)) |
| + c.Append('kEnumBoundary') |
| + |
| + return c |