OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import os.path |
| 6 |
| 7 from code import Code |
| 8 import cpp_util |
| 9 |
| 10 |
| 11 class HGenerator(object): |
| 12 def Generate(self, features, source_file): |
| 13 return _Generator(features, source_file).Generate() |
| 14 |
| 15 |
| 16 class _Generator(object): |
| 17 """A .cc generator for PermissionFeatures. |
| 18 """ |
| 19 def __init__(self, features, source_file): |
| 20 self._feature_defs = cpp_util.RemoveFeatureDuplicates(features) |
| 21 self._source_file = source_file |
| 22 self._source_file_filename, _ = os.path.splitext(source_file) |
| 23 |
| 24 def Generate(self): |
| 25 """Generates a Code object for PermissionFeatures. |
| 26 """ |
| 27 c = Code() |
| 28 (c.Append(cpp_util.CHROMIUM_LICENSE) |
| 29 .Append() |
| 30 .Append(cpp_util.GENERATED_FEATURE_MESSAGE % self._source_file) |
| 31 .Append() |
| 32 ) |
| 33 ifndef_name = cpp_util.GenerateIfndefName(self._source_file_filename, |
| 34 "PermissionFeatures") |
| 35 (c.Append('#ifndef %s' % ifndef_name) |
| 36 .Append('#define %s' % ifndef_name) |
| 37 .Append() |
| 38 ) |
| 39 |
| 40 c.Append('#include <map>') |
| 41 |
| 42 (c.Append('class PermissionFeatures {') |
| 43 .Append(' public:') |
| 44 .Sblock() |
| 45 .Concat(self._GenerateBody()) |
| 46 .Eblock('};') |
| 47 ) |
| 48 (c.Append() |
| 49 .Append('#endif // %s' % ifndef_name) |
| 50 .Append() |
| 51 ) |
| 52 return c |
| 53 |
| 54 def _GenerateBody(self): |
| 55 c = Code() |
| 56 |
| 57 (c.Append('PermissionFeatures();') |
| 58 .Append()) |
| 59 |
| 60 (c.Append('enum ID {') |
| 61 .Concat(self._GenerateEnumConstants()) |
| 62 .Eblock('};') |
| 63 .Append() |
| 64 ) |
| 65 |
| 66 # Generate the ToString function. |
| 67 c.Append('static const char* ToString(ID id);') |
| 68 |
| 69 # Generate the FromString function. |
| 70 c.Append('static ID FromString(const std::string& id);') |
| 71 return c |
| 72 |
| 73 def _GenerateEnumConstants(self): |
| 74 c = Code() |
| 75 |
| 76 (c.Sblock() |
| 77 .Append('kUnknown,') |
| 78 ) |
| 79 for feature in self._feature_defs: |
| 80 c.Append('%s,' % cpp_util.ConstantName(feature.name)) |
| 81 c.Append('kEnumBoundary') |
| 82 |
| 83 return c |
OLD | NEW |