sdbus++: convert binding enum types to valid C++
When an enumeration is used in the bindings, the type of
it depends on the context. We use 'string' types to send
the enumeration across the dbus, so we need to use 'string'
types when doing message operations. We use the real enum
type when calling binding functions, so we need to specify
that in prototypes.
Change-Id: I31ab137b0c751d8a2bdec1c417a60455690ce103
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/tools/sdbusplus/property.py b/tools/sdbusplus/property.py
index bdff93d..bcb50d9 100644
--- a/tools/sdbusplus/property.py
+++ b/tools/sdbusplus/property.py
@@ -11,6 +11,32 @@
super(Property, self).__init__(**kwargs)
+ """ Return a conversion of the cppTypeName valid as a function parameter.
+ Currently only 'enum' requires conversion.
+ """
+ def cppTypeParam(self, interface, server=True):
+ if self.cppTypeName.startswith("enum<"):
+ # strip off 'enum<' and '>'
+ r = self.cppTypeName.split('>')[0].split('<')[1]
+
+ # self. means local type.
+ if r.startswith("self."):
+ return r.split('self.')[1]
+
+ r = r.split('.')
+ r.insert(-2, "server" if server else "client")
+ r = "::".join(r)
+ return r
+ return self.cppTypeName
+
+ """ Return a conversion of the cppTypeName valid as it is read out of a
+ message. Currently only 'enum' requires conversion.
+ """
+ def cppTypeMessage(self, interface):
+ if self.cppTypeName.startswith("enum<"):
+ return "std::string"
+ return self.cppTypeName
+
def parse_cpp_type(self, typeName):
if not typeName:
return None
diff --git a/tools/sdbusplus/templates/interface.mako.server.cpp b/tools/sdbusplus/templates/interface.mako.server.cpp
index 0b71244..b17ddda 100644
--- a/tools/sdbusplus/templates/interface.mako.server.cpp
+++ b/tools/sdbusplus/templates/interface.mako.server.cpp
@@ -35,7 +35,8 @@
% endfor
% for p in interface.properties:
-${p.cppTypeName} ${classname}::${p.camelCase}() const
+auto ${classname}::${p.camelCase}() const ->
+ ${p.cppTypeParam(interface.name)}
{
return _${p.camelCase};
}
@@ -53,7 +54,8 @@
return true;
}
-${p.cppTypeName} ${classname}::${p.camelCase}(${p.cppTypeName} value)
+auto ${classname}::${p.camelCase}(${p.cppTypeParam(interface.name)} value) ->
+ ${p.cppTypeParam(interface.name)}
{
if (_${p.camelCase} != value)
{
@@ -73,7 +75,7 @@
auto o = static_cast<${classname}*>(context);
- decltype(_${p.camelCase}) v{};
+ ${p.cppTypeMessage(interface.name)} v{};
m.read(v);
o->${p.camelCase}(v);
@@ -86,7 +88,7 @@
{
static const auto _property_${p.name} =
utility::tuple_to_array(message::types::type_id<
- ${p.cppTypeName}>());
+ ${p.cppTypeMessage(interface.name)}>());
}
}
% endfor
diff --git a/tools/sdbusplus/templates/interface.mako.server.hpp b/tools/sdbusplus/templates/interface.mako.server.hpp
index 954742d..5a67a43 100644
--- a/tools/sdbusplus/templates/interface.mako.server.hpp
+++ b/tools/sdbusplus/templates/interface.mako.server.hpp
@@ -39,6 +39,15 @@
*/
${classname}(bus::bus& bus, const char* path);
+ % for e in interface.enums:
+ enum class ${e.name}
+ {
+ % for v in e.values:
+ ${v.name},
+ % endfor
+ };
+ % endfor
+
% for m in interface.methods:
${ m.cpp_prototype(loader, interface=interface, ptype='header') }
% endfor
@@ -49,9 +58,10 @@
% for p in interface.properties:
/** Get value of ${p.name} */
- virtual ${p.cppTypeName} ${p.camelCase}() const;
+ virtual ${p.cppTypeParam(interface.name)} ${p.camelCase}() const;
/** Set value of ${p.name} */
- virtual ${p.cppTypeName} ${p.camelCase}(${p.cppTypeName} value);
+ virtual ${p.cppTypeParam(interface.name)} \
+${p.camelCase}(${p.cppTypeParam(interface.name)} value);
% endfor
private:
@@ -78,9 +88,9 @@
% for p in interface.properties:
% if p.defaultValue:
- ${p.cppTypeName} _${p.camelCase} = ${p.defaultValue};
+ ${p.cppTypeParam(interface.name)} _${p.camelCase} = ${p.defaultValue};
% else:
- ${p.cppTypeName} _${p.camelCase}{};
+ ${p.cppTypeParam(interface.name)} _${p.camelCase}{};
% endif
% endfor
diff --git a/tools/sdbusplus/templates/method.mako.prototype.hpp b/tools/sdbusplus/templates/method.mako.prototype.hpp
index 12b1574..96e7390 100644
--- a/tools/sdbusplus/templates/method.mako.prototype.hpp
+++ b/tools/sdbusplus/templates/method.mako.prototype.hpp
@@ -3,7 +3,7 @@
if len(method.returns) == 0:
return "void"
elif len(method.returns) == 1:
- return method.returns[0].cppTypeName
+ return method.returns[0].cppTypeParam(interface.name)
else:
return "std::tuple<" + \
returns_as_list() + \
@@ -20,16 +20,18 @@
return ", ".join([ p.camelCase for p in method.parameters ])
def parameters_types_as_list():
- return ", ".join([ p.cppTypeName for p in method.parameters ])
+ return ", ".join([ p.cppTypeMessage(interface.name)
+ for p in method.parameters ])
def parameter(p, defaultValue=False):
- r = "%s %s" % (p.cppTypeName, p.camelCase)
+ r = "%s %s" % (p.cppTypeParam(interface.name), p.camelCase)
if defaultValue:
r += default_value(p)
return r
- def returns_as_list():
- return ", ".join([ r.cppTypeName for r in method.returns ])
+ def returns_as_list(as_param=True):
+ return ", ".join([ (r.cppTypeParam(interface.name) if as_param else
+ r.cppTypeMessage(interface.name)) for r in method.returns ])
def returns_as_tuple_index(tuple):
return ", ".join([ "std::move(std::get<%d>(%s))" % (i,tuple) \
@@ -75,7 +77,8 @@
% if len(method.returns) != 0:
*
% for r in method.returns:
- * @return ${r.camelCase}[${r.cppTypeName}] - ${r.description.strip()}
+ * @return ${r.camelCase}[${r.cppTypeParam(interface.name)}] \
+- ${r.description.strip()}
% endfor
% endif
*/
@@ -167,7 +170,7 @@
utility::tuple_to_array(std::make_tuple('\0'));
% else:
utility::tuple_to_array(message::types::type_id<
- ${ returns_as_list() }>());
+ ${ returns_as_list(as_param=False) }>());
% endif
}
}
diff --git a/tools/sdbusplus/templates/signal.mako.prototype.hpp b/tools/sdbusplus/templates/signal.mako.prototype.hpp
index 7050ec3..eb5ffca 100644
--- a/tools/sdbusplus/templates/signal.mako.prototype.hpp
+++ b/tools/sdbusplus/templates/signal.mako.prototype.hpp
@@ -4,7 +4,7 @@
join([ parameter(p, defaultValue) for p in signal.properties ])
def parameter(p, defaultValue=False):
- r = "%s %s" % (p.cppTypeName, p.camelCase)
+ r = "%s %s" % (p.cppTypeParam(interface.name), p.camelCase)
if defaultValue:
r += default_value(p)
return r
@@ -13,7 +13,8 @@
return ", ".join([ p.camelCase for p in signal.properties ])
def parameters_types_as_list():
- return ", ".join([ p.cppTypeName for p in signal.properties ])
+ return ", ".join([ p.cppTypeMessage(interface.name)
+ for p in signal.properties ])
def default_value(p):
if p.defaultValue != None: