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

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 comments. 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('_featureMap.insert(std::pair<std::string, '
not at google - send to devlin 2013/09/17 01:28:07 feature_map_[%s] = %s not feature_map_.insert(..
dhnishi (use Chromium) 2013/09/17 18:17:47 Done.
46 'PermissionFeatures::ID>("%s", %s));'
47 % (feature.name, cpp_util.ConstantName(feature.name)))
48 (c.Eblock()
49 .Append('}')
50 .Append()
51 )
52
53 # Generate the ToString function.
54 (c.Append('const char* PermissionFeatures::ToString('
55 'PermissionFeatures::ID id) {')
56 .Sblock()
57 .Append('switch (id) {')
58 .Sblock()
59 )
60 for feature in self._feature_defs:
61 c.Append('case %s: return "%s";' %
62 (cpp_util.ConstantName(feature.name), feature.name))
63 (c.Append('case kUnknown: break;')
64 .Append('case kEnumBoundary: break;')
65 .Eblock()
66 .Append('}')
67 .Append('NOTREACHED();')
68 .Append('return "";')
69 )
70 (c.Eblock()
71 .Append('}')
72 .Append()
73 )
74
75 # Generate the FromString function.
76
77 (c.Append('PermissionFeatures::ID PermissionFeatures::FromString(' \
78 'const std::string& id) {')
79 .Sblock()
80 .Append('std::map<std::string, PermissionFeatures::ID>::const_iterator it'
81 ' = _featureMap.find(id);')
82 .Append('if (it == _featureMap.end())')
83 .Append(' return kUnknown;')
84 .Append('return it->second;')
85 .Eblock()
86 .Append('}')
87 .Append()
88 )
89
90 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698