Chromium Code Reviews| 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 = 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 .Append() | |
| 42 ) | |
| 43 | |
| 44 (c.Append('class PermissionFeatures {') | |
| 45 .Append(' public:') | |
| 46 .Sblock() | |
| 47 .Concat(self._GeneratePublicBody()) | |
| 48 .Eblock() | |
| 49 .Append(' private:') | |
| 50 .Sblock() | |
| 51 .Concat(self._GeneratePrivateBody()) | |
| 52 .Eblock('};') | |
| 53 ) | |
| 54 (c.Append() | |
| 55 .Append('#endif // %s' % ifndef_name) | |
| 56 .Append() | |
| 57 ) | |
| 58 return c | |
| 59 | |
| 60 def _GeneratePublicBody(self): | |
| 61 c = Code() | |
| 62 | |
| 63 (c.Append('PermissionFeatures();') | |
| 64 .Append() | |
| 65 .Append('enum ID {') | |
| 66 .Concat(self._GenerateEnumConstants()) | |
| 67 .Eblock('};') | |
| 68 .Append() | |
| 69 .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.
| |
| 70 .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.
| |
| 71 .Append() | |
| 72 ) | |
| 73 return c | |
| 74 | |
| 75 def _GeneratePrivateBody(self): | |
| 76 return Code().Append('std::map<std::string, ' | |
| 77 'PermissionFeatures::ID> features_;') | |
| 78 | |
| 79 def _GenerateEnumConstants(self): | |
| 80 c = Code() | |
| 81 | |
| 82 (c.Sblock() | |
| 83 .Append('kUnknown,') | |
| 84 ) | |
| 85 for feature in self._feature_defs: | |
| 86 c.Append('%s,' % cpp_util.ConstantName(feature.name)) | |
| 87 c.Append('kEnumBoundary') | |
| 88 | |
| 89 return c | |
| OLD | NEW |