Chromium Code Reviews| 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): | |
|
not at google - send to devlin
2013/09/13 21:37:52
you need to remove references to "permissions", th
dhnishi (use Chromium)
2013/09/16 22:26:21
Began phasing out references.
| |
| 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) | |
| 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 feature map singleton. | |
| 41 (c.Append('std::map<std::string, PermissionFeatures::ID>* ' | |
| 42 '_FeatureMapSingleton() {') | |
| 43 .Sblock() | |
| 44 .Append('static std::map<std::string, PermissionFeatures::ID>* ' | |
| 45 'singleton = new std::map<std::string, PermissionFeatures::ID>;') | |
|
not at google - send to devlin
2013/09/13 21:37:52
You can't do this - it's a memory leak. Usually in
dhnishi (use Chromium)
2013/09/16 22:26:21
Made all of the methods non-static in preparation.
| |
| 46 .Append('return singleton;') | |
| 47 .Eblock() | |
| 48 .Append('}') | |
| 49 .Append() | |
| 50 ) | |
| 51 | |
| 52 # Generate the constructor. | |
| 53 (c.Append('PermissionFeatures::PermissionFeatures() {') | |
| 54 .Sblock() | |
| 55 ) | |
| 56 for feature in self._feature_defs: | |
| 57 c.Append('_FeatureMapSingleton()->insert(std::pair<std::string, ' | |
| 58 'PermissionFeatures::ID>("%s", %s));' | |
| 59 % (feature.name, cpp_util.ConstantName(feature.name))) | |
| 60 (c.Eblock() | |
| 61 .Append('}') | |
| 62 .Append() | |
| 63 ) | |
| 64 | |
| 65 # Generate the ToString function. | |
| 66 (c.Append('const char* PermissionFeatures::ToString(' | |
| 67 'PermissionFeatures::ID id) {') | |
| 68 .Sblock() | |
| 69 .Append('switch (id) {') | |
| 70 .Sblock() | |
| 71 ) | |
| 72 for feature in self._feature_defs: | |
| 73 c.Append('case %s: return "%s";' % | |
| 74 (cpp_util.ConstantName(feature.name), feature.name)) | |
| 75 (c.Append('case kUnknown: break;') | |
| 76 .Append('case kEnumBoundary: break;') | |
| 77 .Eblock() | |
| 78 .Append('}') | |
| 79 .Append('NOTREACHED();') | |
| 80 .Append('return "";') | |
| 81 ) | |
| 82 (c.Eblock() | |
| 83 .Append('}') | |
| 84 .Append() | |
| 85 ) | |
| 86 | |
| 87 # Generate the FromString function. | |
| 88 (c.Append('PermissionFeatures::ID PermissionFeatures::FromString(' \ | |
| 89 'const std::string& id) {') | |
| 90 .Sblock() | |
| 91 .Append('std::map<std::string, PermissionFeatures::ID>::const_iterator it' | |
| 92 ' = _FeatureMapSingleton()->find(id);') | |
| 93 .Append('if (it == _FeatureMapSingleton()->end())') | |
| 94 .Append(' return kUnknown;') | |
| 95 .Append('return it->second;') | |
| 96 .Eblock() | |
| 97 .Append('}') | |
| 98 .Append() | |
| 99 ) | |
| 100 | |
| 101 return c | |
| OLD | NEW |