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

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: 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 CCGenerator(object):
12 def Generate(self, feature_defs, source_file, namespace):
13 return _Generator(feature_defs, source_file, namespace).Generate()
14
15
16 class _Generator(object):
17 """A .cc generator for PermissionFeatures.
not at google - send to devlin 2013/09/18 20:04:47 sorry I totally missed that there was still Permis
dhnishi (use Chromium) 2013/09/18 22:00:23 For now, I've got it taking the path and turning i
not at google - send to devlin 2013/09/18 22:01:23 yeah, strip the leading _.
18 """
19 def __init__(self, feature_defs, source_file, namespace):
20 self._feature_defs = feature_defs
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 .Append('#include <string>')
34 .Append()
35 .Append('#include "%s.h"' % self._source_file_filename)
36 .Append()
37 .Append('#include "base/logging.h"')
38 .Append()
39 .Concat(cpp_util.OpenNamespace(self._namespace))
40 .Append()
41 )
42
43 # Generate the constructor.
44 (c.Append('PermissionFeatures::PermissionFeatures() {')
45 .Sblock()
46 )
47 for feature in self._feature_defs:
48 c.Append('features_["%s"] = %s;'
49 % (feature.name, cpp_util.ConstantName(feature.name)))
50 (c.Eblock()
51 .Append('}')
52 .Append()
53 )
54
55 # Generate the ToString function.
56 (c.Append('const char* PermissionFeatures::ToString('
57 'PermissionFeatures::ID id) const {')
58 .Sblock()
59 .Append('switch (id) {')
60 .Sblock()
61 )
62 for feature in self._feature_defs:
63 c.Append('case %s: return "%s";' %
64 (cpp_util.ConstantName(feature.name), feature.name))
65 (c.Append('case kUnknown: break;')
66 .Append('case kEnumBoundary: break;')
67 .Eblock()
68 .Append('}')
69 .Append('NOTREACHED();')
70 .Append('return "";')
71 )
72 (c.Eblock()
73 .Append('}')
74 .Append()
75 )
76
77 # Generate the FromString function.
78
79 (c.Append('PermissionFeatures::ID PermissionFeatures::FromString('
80 'const std::string& id) const {')
81 .Sblock()
82 .Append('std::map<std::string, PermissionFeatures::ID>::const_iterator it'
83 ' = features_.find(id);')
84 .Append('if (it == features_.end())')
85 .Append(' return kUnknown;')
86 .Append('return it->second;')
87 .Eblock()
88 .Append('}')
89 .Append()
90 .Cblock(cpp_util.CloseNamespace(self._namespace))
91 )
92
93 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698