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

Unified Diff: tools/json_schema_compiler/features_h_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 side-by-side diff with in-line comments
Download patch
Index: tools/json_schema_compiler/features_h_generator.py
diff --git a/tools/json_schema_compiler/features_h_generator.py b/tools/json_schema_compiler/features_h_generator.py
new file mode 100644
index 0000000000000000000000000000000000000000..18e3be456cf8d356306ae33f188ab20349edf0d8
--- /dev/null
+++ b/tools/json_schema_compiler/features_h_generator.py
@@ -0,0 +1,97 @@
+# Copyright (c) 2013 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import os.path
+
+from code import Code
+import cpp_util
+
+
+class HGenerator(object):
+ def Generate(self, features, source_file):
+ return _Generator(features, source_file).Generate()
+
+
+class _Generator(object):
+ """A .cc generator for PermissionFeatures.
+ """
+ def __init__(self, features, source_file):
+ self._feature_defs = features
+ self._source_file = source_file
+ self._source_file_filename, _ = os.path.splitext(source_file)
+
+ def Generate(self):
+ """Generates a Code object for PermissionFeatures.
+ """
+ c = Code()
+ (c.Append(cpp_util.CHROMIUM_LICENSE)
+ .Append()
+ .Append(cpp_util.GENERATED_FEATURE_MESSAGE % self._source_file)
+ .Append()
+ )
+ ifndef_name = cpp_util.GenerateIfndefName(self._source_file_filename,
+ "PermissionFeatures")
+ (c.Append('#ifndef %s' % ifndef_name)
+ .Append('#define %s' % ifndef_name)
+ .Append()
+ )
+
+ (c.Append('#include <map>')
+ .Append()
+ )
+
+ (c.Append('class PermissionFeatures {')
+ .Append(' public:')
+ .Sblock()
+ .Concat(self._GeneratePublicBody())
not at google - send to devlin 2013/09/17 01:28:07 IIRC, Sblock() Concat(...) Eblock() can be writt
dhnishi (use Chromium) 2013/09/17 18:17:47 Cblock(...) concats another code object. I could d
+ .Eblock()
+ .Append(' private:')
+ .Sblock()
+ .Concat(self._GeneratePrivateBody())
+ .Eblock('};')
+ )
+ (c.Append()
+ .Append('#endif // %s' % ifndef_name)
+ .Append()
+ )
+ return c
+
+ def _GeneratePublicBody(self):
+ c = Code()
+
+ (c.Append('PermissionFeatures();')
+ .Append())
+
not at google - send to devlin 2013/09/17 01:28:07 on need to break the chain here?
dhnishi (use Chromium) 2013/09/17 18:17:47 Break removed.
+ (c.Append('enum ID {')
+ .Concat(self._GenerateEnumConstants())
+ .Eblock('};')
+ .Append()
+ )
+
+ # Generate the ToString function.
+ c.Append('const char* ToString(ID id);')
not at google - send to devlin 2013/09/17 01:28:07 this method can be const
dhnishi (use Chromium) 2013/09/17 18:17:47 Changed the parameter to be const.
+
+ # Generate the FromString function.
+ (c.Append('ID FromString(const std::string& id);')
not at google - send to devlin 2013/09/17 01:28:07 so can this method
dhnishi (use Chromium) 2013/09/17 18:17:47 Method is now const.
+ .Append()
+ )
not at google - send to devlin 2013/09/17 01:28:07 nor here and above. the comments are self-explanat
dhnishi (use Chromium) 2013/09/17 18:17:47 Removed the comments. Code seemed simple enough.
+ return c
+
+ def _GeneratePrivateBody(self):
+ c = Code()
+
+ c.Append('std::map<std::string, PermissionFeatures::ID> _featureMap;')
not at google - send to devlin 2013/09/17 01:28:07 features_ or feature_map_ not _featureMap.
dhnishi (use Chromium) 2013/09/17 18:17:47 Opted for features_
+ return c
not at google - send to devlin 2013/09/17 01:28:07 also this can be return Code().Append('...')
dhnishi (use Chromium) 2013/09/17 18:17:47 Done.
+
+ def _GenerateEnumConstants(self):
+ c = Code()
+
+ (c.Sblock()
+ .Append('kUnknown,')
+ )
+ for feature in self._feature_defs:
+ c.Append('%s,' % cpp_util.ConstantName(feature.name))
+ c.Append('kEnumBoundary')
+
+ return c

Powered by Google App Engine
This is Rietveld 408576698