OLD | NEW |
---|---|
(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, permission_defs, source_file): | |
13 return _Generator(permission_defs, source_file).Generate() | |
14 | |
15 | |
16 class _Generator(object): | |
17 """A .cc generator for PermissionFeatures. | |
18 """ | |
19 def __init__(self, permission_defs, source_file): | |
20 self._feature_defs = cpp_util.RemoveFeatureDuplicates(permission_defs) | |
21 self._source_file = source_file | |
22 self._source_file_filename = os.path.splitext(source_file)[0] | |
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 ToString function. | |
41 (c.Append('std::string PermissionFeatures::ToString(' \ | |
42 'PermissionFeatures::ID id) {') | |
43 .Sblock() | |
44 .Append('switch (id) {') | |
45 .Sblock() | |
46 ) | |
47 for feature in self._feature_defs: | |
48 c.Append('case %s: return "%s";' % (feature.constant_name, | |
49 feature.name)) | |
50 (c.Append('default: ') | |
not at google - send to devlin
2013/09/12 16:20:43
no default:, let compiler fail, which.. it shouldn
dhnishi (use Chromium)
2013/09/13 17:36:42
Done.
| |
51 .Sblock() | |
52 .Append('NOTREACHED();') | |
53 .Append('return "";') | |
54 .Eblock() | |
55 ) | |
56 (c.Eblock() | |
57 .Append('}') | |
58 .Eblock() | |
59 .Append('}') | |
60 .Append() | |
61 ) | |
62 | |
63 # Generate the FromString function. | |
64 (c.Append('PermissionFeatures::ID PermissionFeatures::FromString(' \ | |
65 'const std::string& id) {') | |
66 .Sblock() | |
67 ) | |
68 for feature in self._feature_defs: | |
69 c.Append('if (id == "%s") return %s;' % (feature.name, | |
70 feature.constant_name)) | |
not at google - send to devlin
2013/09/12 16:20:43
prefer either:
c.Append('if (id == "%s") return %
dhnishi (use Chromium)
2013/09/13 17:36:42
Done.
| |
71 (c.Append() | |
72 .Append('NOTREACHED();') | |
73 .Append('return kUnknown;') | |
74 .Eblock() | |
75 .Append('}') | |
76 .Append() | |
77 ) | |
not at google - send to devlin
2013/09/12 16:20:43
generate a std::map<std::string, ID> on constructi
dhnishi (use Chromium)
2013/09/13 17:36:42
Done. I worried that I'm breaking design patterns
| |
78 | |
79 (c.Eblock(None, 1) # public | |
80 .Eblock() | |
81 .Eblock(None, 1) # class PermissionFeatures | |
not at google - send to devlin
2013/09/12 16:20:43
not sure what this is
dhnishi (use Chromium)
2013/09/13 17:36:42
Whoops. That was a remnant from an older revision.
| |
82 ) | |
83 return c | |
OLD | NEW |