| OLD | NEW |
| (Empty) |
| 1 # Copyright 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, namespace): | |
| 13 return _Generator(features, source_file, namespace).Generate() | |
| 14 | |
| 15 | |
| 16 class _Generator(object): | |
| 17 """A .cc generator for features. | |
| 18 """ | |
| 19 def __init__(self, features, source_file, namespace): | |
| 20 self._feature_defs = features | |
| 21 self._source_file = source_file | |
| 22 self._source_file_filename, _ = os.path.splitext(source_file) | |
| 23 self._class_name = cpp_util.ClassName(self._source_file_filename) | |
| 24 self._namespace = namespace | |
| 25 | |
| 26 def Generate(self): | |
| 27 """Generates a Code object for features. | |
| 28 """ | |
| 29 c = Code() | |
| 30 (c.Append(cpp_util.CHROMIUM_LICENSE) | |
| 31 .Append() | |
| 32 .Append(cpp_util.GENERATED_FEATURE_MESSAGE % self._source_file) | |
| 33 .Append() | |
| 34 ) | |
| 35 ifndef_name = cpp_util.GenerateIfndefName(self._source_file_filename, | |
| 36 self._class_name) | |
| 37 (c.Append('#ifndef %s' % ifndef_name) | |
| 38 .Append('#define %s' % ifndef_name) | |
| 39 .Append() | |
| 40 ) | |
| 41 | |
| 42 (c.Append('#include <map>') | |
| 43 .Append('#include <string>') | |
| 44 .Append() | |
| 45 .Concat(cpp_util.OpenNamespace(self._namespace)) | |
| 46 .Append() | |
| 47 ) | |
| 48 | |
| 49 (c.Append('class %s {' % self._class_name) | |
| 50 .Append(' public:') | |
| 51 .Sblock() | |
| 52 .Concat(self._GeneratePublicBody()) | |
| 53 .Eblock() | |
| 54 .Append(' private:') | |
| 55 .Sblock() | |
| 56 .Concat(self._GeneratePrivateBody()) | |
| 57 .Eblock('};') | |
| 58 .Append() | |
| 59 .Cblock(cpp_util.CloseNamespace(self._namespace)) | |
| 60 ) | |
| 61 (c.Append('#endif // %s' % ifndef_name) | |
| 62 .Append() | |
| 63 ) | |
| 64 return c | |
| 65 | |
| 66 def _GeneratePublicBody(self): | |
| 67 c = Code() | |
| 68 | |
| 69 (c.Append('%s();' % self._class_name) | |
| 70 .Append() | |
| 71 .Append('enum ID {') | |
| 72 .Concat(self._GenerateEnumConstants()) | |
| 73 .Eblock('};') | |
| 74 .Append() | |
| 75 .Append('const char* ToString(ID id) const;') | |
| 76 .Append('ID FromString(const std::string& id) const;') | |
| 77 .Append() | |
| 78 ) | |
| 79 return c | |
| 80 | |
| 81 def _GeneratePrivateBody(self): | |
| 82 return Code().Append('std::map<std::string, ' | |
| 83 '%s::ID> features_;' % self._class_name) | |
| 84 | |
| 85 def _GenerateEnumConstants(self): | |
| 86 c = Code() | |
| 87 | |
| 88 (c.Sblock() | |
| 89 .Append('kUnknown,') | |
| 90 ) | |
| 91 for feature in self._feature_defs: | |
| 92 c.Append('%s,' % cpp_util.ConstantName(feature.name)) | |
| 93 c.Append('kEnumBoundary') | |
| 94 | |
| 95 return c | |
| OLD | NEW |