Add ICU message format support
[chromium-blink-merge.git] / tools / json_schema_compiler / features_h_generator.py
blob4198bb4b1d92e8c35893536f2d4315273b2f8d9e
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()
36 # Hack: for the purpose of gyp the header file will always be the source
37 # file with its file extension replaced by '.h'. Assume so.
38 output_file = os.path.splitext(self._namespace.source_file)[0] + '.h'
39 ifndef_name = cpp_util.GenerateIfndefName(output_file)
41 (c.Append('#ifndef %s' % ifndef_name)
42 .Append('#define %s' % ifndef_name)
43 .Append()
46 (c.Append('#include <map>')
47 .Append('#include <string>')
48 .Append()
49 .Concat(cpp_util.OpenNamespace(self._namespace))
50 .Append()
53 (c.Append('class %s {' % self._class_name)
54 .Append(' public:')
55 .Sblock()
56 .Concat(self._GeneratePublicBody())
57 .Eblock()
58 .Append(' private:')
59 .Sblock()
60 .Concat(self._GeneratePrivateBody())
61 .Eblock('};')
62 .Append()
63 .Cblock(cpp_util.CloseNamespace(self._namespace))
65 (c.Append('#endif // %s' % ifndef_name)
66 .Append()
68 return c
70 def _GeneratePublicBody(self):
71 c = Code()
73 (c.Append('%s();' % self._class_name)
74 .Append()
75 .Append('enum ID {')
76 .Concat(self._GenerateEnumConstants())
77 .Eblock('};')
78 .Append()
79 .Append('const char* ToString(ID id) const;')
80 .Append('ID FromString(const std::string& id) const;')
81 .Append()
83 return c
85 def _GeneratePrivateBody(self):
86 return Code().Append('std::map<std::string, '
87 '%s::ID> features_;' % self._class_name)
89 def _GenerateEnumConstants(self):
90 c = Code()
92 (c.Sblock()
93 .Append('kUnknown,')
95 for feature in self._feature_defs:
96 c.Append('%s,' % cpp_util.ConstantName(feature.name))
97 c.Append('kEnumBoundary')
99 return c