Revert safe-browsing PrefixSet size_t handling.
[chromium-blink-merge.git] / tools / json_schema_compiler / features_h_generator.py
blob025f734fd9542d0623e8b938ee26e4391587f38c
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.
5 import os.path
7 from code import Code
8 import cpp_util
11 class HGenerator(object):
12 def Generate(self, features, source_file, namespace):
13 return _Generator(features, source_file, namespace).Generate()
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
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()
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()
42 (c.Append('#include <map>')
43 .Append('#include <string>')
44 .Append()
45 .Concat(cpp_util.OpenNamespace(self._namespace))
46 .Append()
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))
61 (c.Append('#endif // %s' % ifndef_name)
62 .Append()
64 return c
66 def _GeneratePublicBody(self):
67 c = Code()
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()
79 return c
81 def _GeneratePrivateBody(self):
82 return Code().Append('std::map<std::string, '
83 '%s::ID> features_;' % self._class_name)
85 def _GenerateEnumConstants(self):
86 c = Code()
88 (c.Sblock()
89 .Append('kUnknown,')
91 for feature in self._feature_defs:
92 c.Append('%s,' % cpp_util.ConstantName(feature.name))
93 c.Append('kEnumBoundary')
95 return c