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

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 #2 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..2b407ca16d0f1867a05ba4567fb3c67b4988394e
--- /dev/null
+++ b/tools/json_schema_compiler/features_h_generator.py
@@ -0,0 +1,83 @@
+# 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 = cpp_util.RemoveFeatureDuplicates(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>')
+
+ (c.Append('class PermissionFeatures {')
+ .Append(' public:')
+ .Sblock()
+ .Concat(self._GenerateBody())
+ .Eblock('};')
+ )
+ (c.Append()
+ .Append('#endif // %s' % ifndef_name)
+ .Append()
+ )
+ return c
+
+ def _GenerateBody(self):
+ c = Code()
+
+ (c.Append('PermissionFeatures();')
+ .Append())
+
+ (c.Append('enum ID {')
+ .Concat(self._GenerateEnumConstants())
+ .Eblock('};')
+ .Append()
+ )
+
+ # Generate the ToString function.
+ c.Append('static const char* ToString(ID id);')
+
+ # Generate the FromString function.
+ c.Append('static ID FromString(const std::string& id);')
+ return c
+
+ 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