From 5313710a822dd1f178105ca25216d64c1579b8bc Mon Sep 17 00:00:00 2001
From: "p.karyukov"
Date: Mon, 13 Oct 2025 16:28:39 +0300
Subject: [PATCH] feat: add timeout parameter to xml2cpp codegen tool
([#509](https://github.com/pavelkaryukov/sdbus-cpp/issues/509))
A timeout can be added to the XML file using the annotation ,
where 500ms is the default parameter value required for interoperability with existing APIs.
These changes will allow flexible timeout configuration using configuration files.
---
tools/xml2cpp-codegen/ProxyGenerator.cpp | 28 +++++++++++++++++++-----
1 file changed, 22 insertions(+), 6 deletions(-)
diff --git a/tools/xml2cpp-codegen/ProxyGenerator.cpp b/tools/xml2cpp-codegen/ProxyGenerator.cpp
index cbd17aaf..f38d68ce 100644
--- a/tools/xml2cpp-codegen/ProxyGenerator.cpp
+++ b/tools/xml2cpp-codegen/ProxyGenerator.cpp
@@ -34,6 +34,7 @@
#include
#include
#include
+#include
using std::endl;
@@ -62,6 +63,20 @@ int ProxyGenerator::transformXmlToFileImpl(const Document &doc, const char *file
return writeToFile(filename, code.str());
}
+std::string prepareDefaultTimeout(const std::string& val, const std::string& unit)
+{
+ const std::unordered_map chronoTypes = {
+ {"us", "std::chrono::microseconds"},
+ {"ms", "std::chrono::milliseconds"},
+ {"s", "std::chrono::seconds"},
+ {"min", "std::chrono::minutes"}
+ };
+ const auto iter = chronoTypes.find(unit);
+
+ const std::string type = (iter != chronoTypes.end()) ? iter->second : "std::chrono::microseconds";
+ const std::string defaultValue = type + "(" + val + ")";
+ return defaultValue;
+}
std::string ProxyGenerator::processInterface(Node& interface) const
{
@@ -212,13 +227,16 @@ std::tuple ProxyGenerator::processMethods(const Nodes&
std::tie(outArgStr, outArgTypeStr, std::ignore, std::ignore) = argsToNamesAndTypes(outArgs);
const std::string realRetType = (async && !dontExpectReply ? (future ? "std::future<" + retType + ">" : "sdbus::PendingAsyncCall") : async ? "void" : retType);
- definitionSS << tab << realRetType << " " << nameSafe << "(" << inArgTypeStr << ")" << endl
- << tab << "{" << endl;
+ std::string timeoutDefaultValue;
if (!timeoutValue.empty())
{
- definitionSS << tab << tab << "using namespace std::chrono_literals;" << endl;
+ const auto val = smTimeout.str(1);
+ const auto unit = smTimeout.str(2);
+ timeoutDefaultValue = prepareDefaultTimeout(val, unit);
}
+ definitionSS << tab << realRetType << " " << nameSafe << "(" << inArgTypeStr << (!timeoutDefaultValue.empty() ? ", const std::chrono::microseconds& timeout = " + timeoutDefaultValue : "") << ")" << endl
+ << tab << "{" << endl;
if (outArgs.size() > 0 && !async)
{
@@ -230,9 +248,7 @@ std::tuple ProxyGenerator::processMethods(const Nodes&
if (!timeoutValue.empty())
{
- const auto val = smTimeout.str(1);
- const auto unit = smTimeout.str(2);
- definitionSS << ".withTimeout(" << val << (unit.empty() ? "us" : unit) << ")";
+ definitionSS << ".withTimeout(" << "timeout" << ")";
}
if (inArgs.size() > 0)