sdbus++: interface: reduce mako embedded python
Reduce the amount of Python embedded in Mako for simplicity and faster
execution.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Iff8cac1b80bfce6b823b2e90464c788b79e67301
diff --git a/tools/sdbusplus/interface.py b/tools/sdbusplus/interface.py
index 9f85f03..e28425b 100644
--- a/tools/sdbusplus/interface.py
+++ b/tools/sdbusplus/interface.py
@@ -40,6 +40,9 @@
def cppNamespacedClass(self):
return self.cppNamespace() + "::" + self.classname
+ def joinedName(self, join_str, append):
+ return join_str.join(self.namespaces + [self.classname, append])
+
def enum_includes(self, inc_list):
includes = []
namespaces = []
diff --git a/tools/sdbusplus/templates/interface.client.hpp.mako b/tools/sdbusplus/templates/interface.client.hpp.mako
index c615ced..78f7125 100644
--- a/tools/sdbusplus/templates/interface.client.hpp.mako
+++ b/tools/sdbusplus/templates/interface.client.hpp.mako
@@ -1,25 +1,21 @@
#pragma once
-<%
- namespaces = interface.name.split('.')
- classname = namespaces.pop()
-%>
namespace sdbusplus
{
-% for s in namespaces:
+% for s in interface.namespaces:
namespace ${s}
{
% endfor
namespace client
{
-namespace ${classname}
+namespace ${interface.classname}
{
static constexpr auto interface = "${interface.name}";
-} // namespace ${classname}
+} // namespace ${interface.classname}
} // namespace client
-% for s in reversed(namespaces):
+% for s in reversed(interface.namespaces):
} // namespace ${s}
% endfor
} // namespace sdbusplus
diff --git a/tools/sdbusplus/templates/interface.server.cpp.mako b/tools/sdbusplus/templates/interface.server.cpp.mako
index fa1c12d..f544ed7 100644
--- a/tools/sdbusplus/templates/interface.server.cpp.mako
+++ b/tools/sdbusplus/templates/interface.server.cpp.mako
@@ -7,14 +7,10 @@
#include <string>
#include <tuple>
-#include <${"/".join(interface.name.split('.') + [ 'server.hpp' ])}>
+#include <${interface.joinedName("/", "server.hpp")}>
% for m in interface.methods + interface.properties + interface.signals:
${ m.cpp_prototype(loader, interface=interface, ptype='callback-cpp-includes') }
% endfor
-<%
- namespaces = interface.name.split('.')
- classname = namespaces.pop()
-%>
namespace sdbusplus::${interface.cppNamespace()}
{
@@ -31,7 +27,7 @@
% endfor
% if interface.properties:
-void ${classname}::setPropertyByName(const std::string& _name,
+void ${interface.classname}::setPropertyByName(const std::string& _name,
const PropertiesVariant& val,
bool skipSignal)
{
@@ -46,7 +42,7 @@
% endfor
}
-auto ${classname}::getPropertyByName(const std::string& _name) ->
+auto ${interface.classname}::getPropertyByName(const std::string& _name) ->
PropertiesVariant
{
% for p in interface.properties:
@@ -64,26 +60,26 @@
namespace
{
-/** String to enum mapping for ${classname}::${e.name} */
-static const std::tuple<const char*, ${classname}::${e.name}> \
-mapping${classname}${e.name}[] =
+/** String to enum mapping for ${interface.classname}::${e.name} */
+static const std::tuple<const char*, ${interface.classname}::${e.name}> \
+mapping${interface.classname}${e.name}[] =
{
% for v in e.values:
std::make_tuple( "${interface.name}.${e.name}.${v.name}", \
- ${classname}::${e.name}::${v.name} ),
+ ${interface.classname}::${e.name}::${v.name} ),
% endfor
};
} // anonymous namespace
-auto ${classname}::convertStringTo${e.name}(const std::string& s) noexcept ->
+auto ${interface.classname}::convertStringTo${e.name}(const std::string& s) noexcept ->
std::optional<${e.name}>
{
auto i = std::find_if(
- std::begin(mapping${classname}${e.name}),
- std::end(mapping${classname}${e.name}),
+ std::begin(mapping${interface.classname}${e.name}),
+ std::end(mapping${interface.classname}${e.name}),
[&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } );
- if (std::end(mapping${classname}${e.name}) == i)
+ if (std::end(mapping${interface.classname}${e.name}) == i)
{
return std::nullopt;
}
@@ -93,7 +89,7 @@
}
}
-auto ${classname}::convert${e.name}FromString(const std::string& s) ->
+auto ${interface.classname}::convert${e.name}FromString(const std::string& s) ->
${e.name}
{
auto r = convertStringTo${e.name}(s);
@@ -108,13 +104,13 @@
}
}
-std::string ${classname}::convert${e.name}ToString(${classname}::${e.name} v)
+std::string ${interface.classname}::convert${e.name}ToString(${interface.classname}::${e.name} v)
{
auto i = std::find_if(
- std::begin(mapping${classname}${e.name}),
- std::end(mapping${classname}${e.name}),
+ std::begin(mapping${interface.classname}${e.name}),
+ std::end(mapping${interface.classname}${e.name}),
[v](auto& e){ return v == std::get<1>(e); });
- if (i == std::end(mapping${classname}${e.name}))
+ if (i == std::end(mapping${interface.classname}${e.name}))
{
throw std::invalid_argument(std::to_string(static_cast<int>(v)));
}
@@ -122,7 +118,7 @@
}
% endfor
-const vtable_t ${classname}::_vtable[] = {
+const vtable_t ${interface.classname}::_vtable[] = {
vtable::start(),
% for m in interface.methods:
${ m.cpp_prototype(loader, interface=interface, ptype='vtable') }
@@ -132,7 +128,7 @@
% endfor
% for p in interface.properties:
vtable::property("${p.name}",
- details::${classname}::_property_${p.name}
+ details::${interface.classname}::_property_${p.name}
.data(),
_callback_get_${p.name},
% if 'const' not in p.flags and 'readonly' not in p.flags:
diff --git a/tools/sdbusplus/templates/interface.server.hpp.mako b/tools/sdbusplus/templates/interface.server.hpp.mako
index a1e38d5..bedf382 100644
--- a/tools/sdbusplus/templates/interface.server.hpp.mako
+++ b/tools/sdbusplus/templates/interface.server.hpp.mako
@@ -12,21 +12,15 @@
${ m.cpp_prototype(loader, interface=interface, ptype='callback-hpp-includes') }\
% endfor
<%
- namespaces = interface.namespaces
- classname = interface.classname
-
def setOfPropertyTypes():
return set(p.cppTypeParam(interface.name) for p in
interface.properties);
-
- def interface_instance():
- return "_".join(interface.name.split('.') + ['interface'])
%>
namespace sdbusplus::${interface.cppNamespace()}
{
-class ${classname}
+class ${interface.classname}
{
public:
static constexpr auto interface =
@@ -41,19 +35,19 @@
* Allowed:
* - Destructor.
*/
- ${classname}() = delete;
- ${classname}(const ${classname}&) = delete;
- ${classname}& operator=(const ${classname}&) = delete;
- ${classname}(${classname}&&) = delete;
- ${classname}& operator=(${classname}&&) = delete;
- virtual ~${classname}() = default;
+ ${interface.classname}() = delete;
+ ${interface.classname}(const ${interface.classname}&) = delete;
+ ${interface.classname}& operator=(const ${interface.classname}&) = delete;
+ ${interface.classname}(${interface.classname}&&) = delete;
+ ${interface.classname}& operator=(${interface.classname}&&) = delete;
+ virtual ~${interface.classname}() = default;
/** @brief Constructor to put object onto bus at a dbus path.
* @param[in] bus - Bus to attach to.
* @param[in] path - Path to attach at.
*/
- ${classname}(bus_t& bus, const char* path) :
- _${interface_instance()}(
+ ${interface.classname}(bus_t& bus, const char* path) :
+ _${interface.joinedName("_", "interface")}(
bus, path, interface, _vtable, this),
_intf(bus.getInterface()) {}
@@ -76,10 +70,10 @@
* @param[in] path - Path to attach at.
* @param[in] vals - Map of property name to value for initialization.
*/
- ${classname}(bus_t& bus, const char* path,
+ ${interface.classname}(bus_t& bus, const char* path,
const std::map<std::string, PropertiesVariant>& vals,
bool skipSignal = false) :
- ${classname}(bus, path)
+ ${interface.classname}(bus, path)
{
for (const auto& v : vals)
{
@@ -153,13 +147,13 @@
/** @brief Emit interface added */
void emit_added()
{
- _${"_".join(interface.name.split('.'))}_interface.emit_added();
+ _${interface.joinedName("_", "interface")}.emit_added();
}
/** @brief Emit interface removed */
void emit_removed()
{
- _${"_".join(interface.name.split('.'))}_interface.emit_removed();
+ _${interface.joinedName("_", "interface")}.emit_removed();
}
private:
@@ -182,7 +176,7 @@
% endfor
static const vtable_t _vtable[];
sdbusplus::server::interface_t
- _${"_".join(interface.name.split('.'))}_interface;
+ _${interface.joinedName("_", "interface")};
sdbusplus::SdBusInterface* _intf;
% for p in interface.properties:
% if p.defaultValue is not None:
@@ -201,16 +195,16 @@
% for e in interface.enums:
/* Specialization of sdbusplus::server::convertForMessage
- * for enum-type ${classname}::${e.name}.
+ * for enum-type ${interface.classname}::${e.name}.
*
* This converts from the enum to a constant c-string representing the enum.
*
* @param[in] e - Enum value to convert.
* @return C-string representing the name for the enum value.
*/
-inline std::string convertForMessage(${classname}::${e.name} e)
+inline std::string convertForMessage(${interface.classname}::${e.name} e)
{
- return ${classname}::convert${e.name}ToString(e);
+ return ${interface.classname}::convert${e.name}ToString(e);
}
% endfor
diff --git a/tools/sdbusplus/templates/method.prototype.hpp.mako b/tools/sdbusplus/templates/method.prototype.hpp.mako
index f86458a..16f0312 100644
--- a/tools/sdbusplus/templates/method.prototype.hpp.mako
+++ b/tools/sdbusplus/templates/method.prototype.hpp.mako
@@ -1,7 +1,4 @@
<%
- def interface_name():
- return interface.name.split('.').pop()
-
def error_namespace(e):
n = e.split('.');
n.pop(); # Remove error name.
@@ -52,9 +49,9 @@
###
% elif ptype == 'vtable':
vtable::method("${method.name}",
- details::${interface_name()}::_param_${ method.CamelCase }
+ details::${interface.classname}::_param_${ method.CamelCase }
.data(),
- details::${interface_name()}::_return_${ method.CamelCase }
+ details::${interface.classname}::_return_${ method.CamelCase }
.data(),
% if method.cpp_flags:
_callback_${method.CamelCase},
@@ -66,10 +63,10 @@
### Emit 'callback-cpp'
###
% elif ptype == 'callback-cpp':
-int ${interface_name()}::_callback_${ method.CamelCase }(
+int ${interface.classname}::_callback_${ method.CamelCase }(
sd_bus_message* msg, void* context, sd_bus_error* error)
{
- auto o = static_cast<${interface_name()}*>(context);
+ auto o = static_cast<${interface.classname}*>(context);
% if method.errors:
try
@@ -99,7 +96,7 @@
namespace details
{
-namespace ${interface_name()}
+namespace ${interface.classname}
{
static const auto _param_${ method.CamelCase } =
% if len(method.parameters) == 0:
diff --git a/tools/sdbusplus/templates/property.prototype.hpp.mako b/tools/sdbusplus/templates/property.prototype.hpp.mako
index 39f12f8..48692db 100644
--- a/tools/sdbusplus/templates/property.prototype.hpp.mako
+++ b/tools/sdbusplus/templates/property.prototype.hpp.mako
@@ -1,10 +1,4 @@
<%
- namespaces = interface.name.split('.')
- classname = namespaces.pop()
-
- def interface_instance():
- return "_".join(interface.name.split('.') + ['interface'])
-
def error_namespace(e):
n = e.split('.');
n.pop(); # Remove error name.
@@ -21,18 +15,18 @@
return '/'.join(l) + '/error.hpp';
%>
% if ptype == 'callback-cpp':
-auto ${classname}::${property.camelCase}() const ->
+auto ${interface.classname}::${property.camelCase}() const ->
${property.cppTypeParam(interface.name)}
{
return _${property.camelCase};
}
-int ${classname}::_callback_get_${property.name}(
+int ${interface.classname}::_callback_get_${property.name}(
sd_bus* /*bus*/, const char* /*path*/, const char* /*interface*/,
const char* /*property*/, sd_bus_message* reply, void* context,
sd_bus_error* error)
{
- auto o = static_cast<${classname}*>(context);
+ auto o = static_cast<${interface.classname}*>(context);
% if property.errors:
try
@@ -55,7 +49,7 @@
% endfor
}
-auto ${classname}::${property.camelCase}(${property.cppTypeParam(interface.name)} value,
+auto ${interface.classname}::${property.camelCase}(${property.cppTypeParam(interface.name)} value,
bool skipSignal) ->
${property.cppTypeParam(interface.name)}
{
@@ -64,26 +58,26 @@
_${property.camelCase} = value;
if (!skipSignal)
{
- _${interface_instance()}.property_changed("${property.name}");
+ _${interface.joinedName("_", "interface")}.property_changed("${property.name}");
}
}
return _${property.camelCase};
}
-auto ${classname}::${property.camelCase}(${property.cppTypeParam(interface.name)} val) ->
+auto ${interface.classname}::${property.camelCase}(${property.cppTypeParam(interface.name)} val) ->
${property.cppTypeParam(interface.name)}
{
return ${property.camelCase}(val, false);
}
% if 'const' not in property.flags and 'readonly' not in property.flags:
-int ${classname}::_callback_set_${property.name}(
+int ${interface.classname}::_callback_set_${property.name}(
sd_bus* /*bus*/, const char* /*path*/, const char* /*interface*/,
const char* /*property*/, sd_bus_message* value, void* context,
sd_bus_error* error)
{
- auto o = static_cast<${classname}*>(context);
+ auto o = static_cast<${interface.classname}*>(context);
% if property.errors:
try
@@ -109,7 +103,7 @@
namespace details
{
-namespace ${classname}
+namespace ${interface.classname}
{
static const auto _property_${property.name} =
utility::tuple_to_array(message::types::type_id<
diff --git a/tools/sdbusplus/templates/signal.prototype.hpp.mako b/tools/sdbusplus/templates/signal.prototype.hpp.mako
index 113785e..33bcf27 100644
--- a/tools/sdbusplus/templates/signal.prototype.hpp.mako
+++ b/tools/sdbusplus/templates/signal.prototype.hpp.mako
@@ -22,9 +22,6 @@
return " = " + str(p.defaultValue)
else:
return ""
-
- def interface_name():
- return interface.name.split('.').pop()
%>
###
### Emit 'header'
@@ -47,13 +44,13 @@
###
% elif ptype == 'vtable':
vtable::signal("${signal.name}",
- details::${interface_name()}::_signal_${signal.CamelCase }
+ details::${interface.classname}::_signal_${signal.CamelCase }
.data()),
###
### Emit 'callback-cpp'
###
% elif ptype == 'callback-cpp':
-void ${interface_name()}::${ signal.camelCase }(
+void ${interface.classname}::${ signal.camelCase }(
${ parameters() })
{
auto& i = _${"_".join(interface.name.split('.'))}_interface;
@@ -65,7 +62,7 @@
namespace details
{
-namespace ${interface_name()}
+namespace ${interface.classname}
{
static const auto _signal_${ signal.CamelCase } =
% if len(signal.properties) == 0: