Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: tools/json_schema_compiler/features_h_generator.py

Issue 23594008: Initial code generation for features. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Adding tests for real. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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, namespace):
13 return _Generator(features, source_file, namespace).Generate()
14
15
16 class _Generator(object):
17 """A .cc generator for PermissionFeatures.
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._namespace = namespace
24
25 def Generate(self):
26 """Generates a Code object for PermissionFeatures.
27 """
28 c = Code()
29 (c.Append(cpp_util.CHROMIUM_LICENSE)
30 .Append()
31 .Append(cpp_util.GENERATED_FEATURE_MESSAGE % self._source_file)
32 .Append()
33 )
34 ifndef_name = cpp_util.GenerateIfndefName(self._source_file_filename,
35 "PermissionFeatures")
36 (c.Append('#ifndef %s' % ifndef_name)
37 .Append('#define %s' % ifndef_name)
38 .Append()
39 )
40
41 (c.Append('#include <map>')
42 .Append('#include <string>')
43 .Append()
44 .Concat(cpp_util.OpenNamespace(self._namespace))
45 .Append()
46 )
47
48 (c.Append('class PermissionFeatures {')
49 .Append(' public:')
50 .Sblock()
51 .Concat(self._GeneratePublicBody())
52 .Eblock()
53 .Append(' private:')
54 .Sblock()
55 .Concat(self._GeneratePrivateBody())
56 .Eblock('};')
57 .Append()
58 .Cblock(cpp_util.CloseNamespace(self._namespace))
59 )
60 (c.Append('#endif // %s' % ifndef_name)
61 .Append()
62 )
63 return c
64
65 def _GeneratePublicBody(self):
66 c = Code()
67
68 (c.Append('PermissionFeatures();')
69 .Append()
70 .Append('enum ID {')
71 .Concat(self._GenerateEnumConstants())
72 .Eblock('};')
73 .Append()
74 .Append('const char* ToString(ID id) const;')
75 .Append('ID FromString(const std::string& id) const;')
76 .Append()
77 )
78 return c
79
80 def _GeneratePrivateBody(self):
81 return Code().Append('std::map<std::string, '
82 'PermissionFeatures::ID> features_;')
83
84 def _GenerateEnumConstants(self):
85 c = Code()
86
87 (c.Sblock()
88 .Append('kUnknown,')
89 )
90 for feature in self._feature_defs:
91 c.Append('%s,' % cpp_util.ConstantName(feature.name))
92 c.Append('kEnumBoundary')
93
94 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698