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

Unified Diff: mojo/public/bindings/generators/mojom_cpp_generator.py

Issue 55133009: mojom: Minor cleanup to template code (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: A little more cleanup Created 7 years, 2 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/bindings/generators/mojom_cpp_generator.py
diff --git a/mojo/public/bindings/generators/mojom_cpp_generator.py b/mojo/public/bindings/generators/mojom_cpp_generator.py
index 99553223ab2f881ac4a4b3a5261ee1f16e1b04e9..5587c477b1d57c1e063f7ac816feed8aa84cc52c 100644
--- a/mojo/public/bindings/generators/mojom_cpp_generator.py
+++ b/mojo/public/bindings/generators/mojom_cpp_generator.py
@@ -22,19 +22,38 @@ def ReadTemplate(filename):
return Template(file.read())
-def AddForward(forwards, kind):
- """Adds a forward class declaration line to the |forwards| set."""
- if isinstance(kind, mojom.Struct):
- forwards.add("class %s;" % kind.name.capitalize())
- if isinstance(kind, mojom.Array):
- AddForward(forwards, kind.kind)
+class Forwards(object):
+ """Helper class to maintain unique set of forward declarations."""
+ def __init__(self):
+ self.forwards = set()
+ def Add(self, kind):
+ if isinstance(kind, mojom.Struct):
+ self.forwards.add("class %s;" % kind.name.capitalize())
+ if isinstance(kind, mojom.Array):
+ self.Add(kind.kind)
+
+ def __repr__(self):
+ if len(self.forwards) > 0:
+ return '\n'.join(sorted(self.forwards)) + '\n'
+ return ""
+
+
+class Lines(object):
+ """Helper class to maintain list of template expanded lines."""
+ def __init__(self, template):
+ self.template = template
+ self.lines = []
+
+ def Add(self, map = {}, **substitutions):
+ if len(substitutions) > 0:
+ map = map.copy()
+ map.update(substitutions)
+
+ self.lines.append(self.template.substitute(map))
-def GetForwards(forwards):
- """Returns a string suitable for substituting into a $FORWARDS section."""
- if len(forwards) > 0:
- return '\n'.join(sorted(forwards)) + '\n'
- return ""
+ def __repr__(self):
+ return '\n'.join(self.lines)
class CPPGenerator(object):
@@ -57,15 +76,15 @@ class CPPGenerator(object):
interface_stub_header_template = ReadTemplate("module_interface_stub.h")
interface_stub_source_template = ReadTemplate("module_interface_stub.cc")
interface_stub_case_template = ReadTemplate("module_interface_stub_case")
- field_template = Template(" $itype ${field}_;")
- bool_field_template = Template(" uint8_t ${field}_ : 1;")
+ field_template = Template(" $TYPE ${FIELD}_;")
+ bool_field_template = Template(" uint8_t ${FIELD}_ : 1;")
setter_template = \
- Template(" void set_$field($stype $field) { ${field}_ = $field; }")
+ Template(" void set_$FIELD($TYPE $FIELD) { ${FIELD}_ = $FIELD; }")
getter_template = \
- Template(" $gtype $field() const { return ${field}_; }")
+ Template(" $TYPE $FIELD() const { return ${FIELD}_; }")
ptr_getter_template = \
- Template(" $gtype $field() const { return ${field}_.ptr; }")
- pad_template = Template(" uint8_t _pad${count}_[$pad];")
+ Template(" $TYPE $FIELD() const { return ${FIELD}_.ptr; }")
+ pad_template = Template(" uint8_t _pad${COUNT}_[$PAD];")
HEADER_SIZE = 8
kind_to_type = {
@@ -112,18 +131,18 @@ class CPPGenerator(object):
template = cls.ptr_getter_template
else:
template = cls.getter_template
- return template.substitute(field=pf.field.name, gtype=gtype)
+ return template.substitute(FIELD=pf.field.name, TYPE=gtype)
@classmethod
def GetSetterLine(cls, pf):
stype = cls.GetType(pf.field.kind)
- return cls.setter_template.substitute(field=pf.field.name, stype=stype)
+ return cls.setter_template.substitute(FIELD=pf.field.name, TYPE=stype)
@classmethod
def GetFieldLine(cls, pf):
kind = pf.field.kind
if kind.spec == 'b':
- return cls.bool_field_template.substitute(field=pf.field.name)
+ return cls.bool_field_template.substitute(FIELD=pf.field.name)
itype = None
if isinstance(kind, mojom.Struct):
itype = "mojo::internal::StructPointer<%s>" % kind.name.capitalize()
@@ -133,19 +152,12 @@ class CPPGenerator(object):
itype = "mojo::internal::StringPointer"
else:
itype = cls.kind_to_type[kind]
- return cls.field_template.substitute(field=pf.field.name, itype=itype)
+ return cls.field_template.substitute(FIELD=pf.field.name, TYPE=itype)
@classmethod
def GetCaseLine(cls, interface, method):
- method_call = "%s(" % method.name
- first_param = True
- for param in method.parameters:
- if first_param:
- first_param = False
- else:
- method_call += ", "
- method_call += "params->%s()" % param.name
- method_call += ");"
+ params = map(lambda param: "params->%s()" % param.name, method.parameters)
+ method_call = "%s(%s);" % (method.name, ", ".join(params))
return cls.interface_stub_case_template.substitute(
CLASS = interface.name,
METHOD = method.name,
@@ -178,6 +190,8 @@ class CPPGenerator(object):
self.output_dir = output_dir
def WriteTemplateToFile(self, template, name, ext, **substitutions):
+ substitutions['YEAR'] = datetime.date.today().year
+ substitutions['NAMESPACE'] = self.module.namespace
if self.output_dir is None:
file = sys.stdout
else:
@@ -195,19 +209,19 @@ class CPPGenerator(object):
fields = []
setters = []
getters = []
- forwards = set()
+ forwards = Forwards()
pad_count = 0
num_fields = len(ps.packed_fields)
for i in xrange(num_fields):
pf = ps.packed_fields[i]
fields.append(self.GetFieldLine(pf))
- AddForward(forwards, pf.field.kind)
+ forwards.Add(pf.field.kind)
if i < (num_fields - 2):
next_pf = ps.packed_fields[i+1]
pad = next_pf.offset - (pf.offset + pf.size)
if pad > 0:
- fields.append(self.pad_template.substitute(count=pad_count, pad=pad))
+ fields.append(self.pad_template.substitute(COUNT=pad_count, PAD=pad))
pad_count += 1
setters.append(self.GetSetterLine(pf))
getters.append(self.GetGetterLine(pf))
@@ -217,19 +231,17 @@ class CPPGenerator(object):
offset = last_field.offset + last_field.size
pad = mojom_pack.GetPad(offset, 8)
if pad > 0:
- fields.append(self.pad_template.substitute(count=pad_count, pad=pad))
+ fields.append(self.pad_template.substitute(COUNT=pad_count, PAD=pad))
pad_count += 1
size = offset + pad
else:
size = 0
self.WriteTemplateToFile(self.struct_header_template, struct.name, 'h',
- YEAR = datetime.date.today().year,
HEADER_GUARD = self.GetHeaderGuard(struct.name),
- NAMESPACE = self.module.namespace,
CLASS = struct.name.capitalize(),
SIZE = size + self.HEADER_SIZE,
- FORWARDS = GetForwards(forwards),
+ FORWARDS = forwards,
SETTERS = '\n'.join(setters),
GETTERS = '\n'.join(getters),
FIELDS = '\n'.join(fields))
@@ -238,8 +250,6 @@ class CPPGenerator(object):
struct = ps.struct
header = self.GetHeaderFile(struct.name)
self.WriteTemplateToFile(self.struct_source_template, struct.name, 'cc',
- YEAR = datetime.date.today().year,
- NAMESPACE = self.module.namespace,
CLASS = struct.name.capitalize(),
NUM_FIELDS = len(struct.fields),
HEADER = header)
@@ -251,9 +261,7 @@ class CPPGenerator(object):
self.struct_serialization_header_template,
struct.name + "_serialization",
'h',
- YEAR = datetime.date.today().year,
HEADER_GUARD = self.GetHeaderGuard(struct.name + "_SERIALIZATION"),
- NAMESPACE = self.module.namespace,
CLASS = struct.name.capitalize(),
FULL_CLASS = "%s::%s" % \
(self.module.namespace, struct.name.capitalize()),
@@ -263,62 +271,52 @@ class CPPGenerator(object):
struct = ps.struct
serialization_header = self.GetHeaderFile(struct.name, "serialization")
class_header = self.GetHeaderFile(struct.name)
- clones = []
- encodes = []
- decodes = []
+ clones = Lines(self.struct_serialization_clone_template)
+ encodes = Lines(self.struct_serialization_encode_template)
+ decodes = Lines(self.struct_serialization_decode_template)
sizes = " return sizeof(*%s)" % struct.name.lower()
fields = self.GetSerializedFields(ps)
for field in fields:
substitutions = {'NAME': struct.name.lower(), 'FIELD': field.name.lower()}
sizes += \
self.struct_serialization_compute_template.substitute(substitutions)
- clones.append(
- self.struct_serialization_clone_template.substitute(substitutions))
- encodes.append(
- self.struct_serialization_encode_template.substitute(substitutions))
- decodes.append(
- self.struct_serialization_decode_template.substitute(substitutions))
+ clones.Add(substitutions)
+ encodes.Add(substitutions)
+ decodes.Add(substitutions)
sizes += ";"
self.WriteTemplateToFile(
self.struct_serialization_source_template,
struct.name + "_serialization",
'cc',
- YEAR = datetime.date.today().year,
NAME = struct.name.lower(),
FULL_CLASS = "%s::%s" % \
(self.module.namespace, struct.name.capitalize()),
SERIALIZATION_HEADER = serialization_header,
CLASS_HEADER = class_header,
SIZES = sizes,
- CLONES = '\n'.join(clones),
- ENCODES = '\n'.join(encodes),
- DECODES = '\n'.join(decodes))
+ CLONES = clones,
+ ENCODES = encodes,
+ DECODES = decodes)
def GenerateInterfaceHeader(self, interface):
cpp_methods = []
- forwards = set()
+ forwards = Forwards()
for method in interface.methods:
cpp_method = " virtual void %s(" % method.name
- first_param = True
+ params = []
for param in method.parameters:
- if first_param == True:
- first_param = False
- else:
- cpp_method += ", "
- AddForward(forwards, param.kind)
- cpp_method += "%s %s" % (self.GetConstType(param.kind), param.name)
- cpp_method += ") = 0;"
- cpp_methods.append(cpp_method)
+ forwards.Add(param.kind)
+ params.append("%s %s" % (self.GetConstType(param.kind), param.name))
+ cpp_methods.append(
+ " virtual void %s(%s) = 0" % (method.name, ", ".join(params)))
self.WriteTemplateToFile(
self.interface_header_template,
interface.name,
'h',
- YEAR = datetime.date.today().year,
HEADER_GUARD = self.GetHeaderGuard(interface.name),
- NAMESPACE = self.module.namespace,
CLASS = interface.name.capitalize(),
- FORWARDS = GetForwards(forwards),
+ FORWARDS = forwards,
METHODS = '\n'.join(cpp_methods))
def GenerateInterfaceStubHeader(self, interface):
@@ -327,9 +325,7 @@ class CPPGenerator(object):
self.interface_stub_header_template,
interface.name + "_stub",
'h',
- YEAR = datetime.date.today().year,
HEADER_GUARD = self.GetHeaderGuard(interface.name + "_STUB"),
- NAMESPACE = self.module.namespace,
CLASS = interface.name.capitalize(),
HEADER = header)
@@ -343,8 +339,6 @@ class CPPGenerator(object):
self.interface_stub_source_template,
interface.name + "_stub",
'cc',
- YEAR = datetime.date.today().year,
- NAMESPACE = self.module.namespace,
CLASS = interface.name.capitalize(),
CASES = '\n'.join(cases),
STUB_HEADER = stub_header,
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698