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

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

Issue 23594008: Initial code generation for features. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addressing #6. 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 CCGenerator(object):
12 def Generate(self, feature_defs, source_file):
13 return _Generator(feature_defs, source_file).Generate()
14
15
16 class _Generator(object):
17 """A .cc generator for PermissionFeatures.
18 """
19 def __init__(self, feature_defs, source_file):
20 self._feature_defs = feature_defs
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 .Append('#include <string>')
33 .Append()
34 .Append('#include "%s.h"' % self._source_file_filename)
35 .Append()
36 .Append('#include "base/logging.h"')
37 .Append()
38 )
39
40 # Generate the constructor.
41 (c.Append('PermissionFeatures::PermissionFeatures() {')
42 .Sblock()
43 )
44 for feature in self._feature_defs:
45 c.Append('features_["%s"] = %s;'
46 % (feature.name, cpp_util.ConstantName(feature.name)))
47 (c.Eblock()
48 .Append('}')
49 .Append()
50 )
51
52 # Generate the ToString function.
53 (c.Append('const char* PermissionFeatures::ToString('
54 'const PermissionFeatures::ID id) {')
55 .Sblock()
56 .Append('switch (id) {')
57 .Sblock()
58 )
59 for feature in self._feature_defs:
60 c.Append('case %s: return "%s";' %
61 (cpp_util.ConstantName(feature.name), feature.name))
62 (c.Append('case kUnknown: break;')
63 .Append('case kEnumBoundary: break;')
64 .Eblock()
65 .Append('}')
66 .Append('NOTREACHED();')
67 .Append('return "";')
68 )
69 (c.Eblock()
70 .Append('}')
71 .Append()
72 )
73
74 # Generate the FromString function.
75
76 (c.Append('const PermissionFeatures::ID PermissionFeatures::FromString('
77 'const std::string& id) {')
78 .Sblock()
79 .Append('std::map<std::string, PermissionFeatures::ID>::const_iterator it'
80 ' = features_.find(id);')
81 .Append('if (it == features_.end())')
82 .Append(' return kUnknown;')
83 .Append('return it->second;')
84 .Eblock()
85 .Append('}')
86 .Append()
87 )
88
89 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698