Skip to content

Commit 0a2b300

Browse files
author
p.karyukov
committed
feat: add timeout parameter to xml2cpp codegen tool (#509)
A timeout can be added to the XML file using the annotation <annotation name="org.freedesktop.DBus.Method.Timeout" value="500ms"/>, where 500ms is the default parameter value required for interoperability with existing APIs. These changes will allow flexible timeout configuration using configuration files.
1 parent 7fbfcec commit 0a2b300

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

tools/xml2cpp-codegen/ProxyGenerator.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <algorithm>
3535
#include <iterator>
3636
#include <regex>
37+
#include <unordered_map>
3738

3839
using std::endl;
3940

@@ -62,6 +63,20 @@ int ProxyGenerator::transformXmlToFileImpl(const Document &doc, const char *file
6263
return writeToFile(filename, code.str());
6364
}
6465

66+
std::string prepareDefaultTimeout(const std::string& val, const std::string& unit)
67+
{
68+
const std::unordered_map<std::string, std::string> chronoTypes = {
69+
{"us", "std::chrono::microseconds"},
70+
{"ms", "std::chrono::milliseconds"},
71+
{"s", "std::chrono::seconds"},
72+
{"min", "std::chrono::minutes"}
73+
};
74+
const auto iter = chronoTypes.find(unit);
75+
76+
const std::string type = (iter != chronoTypes.end()) ? iter->second : "std::chrono::microseconds";
77+
const std::string defaultValue = type + "(" + val + ")";
78+
return defaultValue;
79+
}
6580

6681
std::string ProxyGenerator::processInterface(Node& interface) const
6782
{
@@ -212,13 +227,16 @@ std::tuple<std::string, std::string> ProxyGenerator::processMethods(const Nodes&
212227
std::tie(outArgStr, outArgTypeStr, std::ignore, std::ignore) = argsToNamesAndTypes(outArgs);
213228

214229
const std::string realRetType = (async && !dontExpectReply ? (future ? "std::future<" + retType + ">" : "sdbus::PendingAsyncCall") : async ? "void" : retType);
215-
definitionSS << tab << realRetType << " " << nameSafe << "(" << inArgTypeStr << ")" << endl
216-
<< tab << "{" << endl;
217230

231+
std::string timeoutDefaultValue;
218232
if (!timeoutValue.empty())
219233
{
220-
definitionSS << tab << tab << "using namespace std::chrono_literals;" << endl;
234+
const auto val = smTimeout.str(1);
235+
const auto unit = smTimeout.str(2);
236+
timeoutDefaultValue = prepareDefaultTimeout(val, unit);
221237
}
238+
definitionSS << tab << realRetType << " " << nameSafe << "(" << inArgTypeStr << (!timeoutDefaultValue.empty() ? ", std::chrono::microseconds timeout = " + timeoutDefaultValue : "") << ")" << endl
239+
<< tab << "{" << endl;
222240

223241
if (outArgs.size() > 0 && !async)
224242
{
@@ -230,9 +248,7 @@ std::tuple<std::string, std::string> ProxyGenerator::processMethods(const Nodes&
230248

231249
if (!timeoutValue.empty())
232250
{
233-
const auto val = smTimeout.str(1);
234-
const auto unit = smTimeout.str(2);
235-
definitionSS << ".withTimeout(" << val << (unit.empty() ? "us" : unit) << ")";
251+
definitionSS << ".withTimeout(" << "timeout" << ")";
236252
}
237253

238254
if (inArgs.size() > 0)

0 commit comments

Comments
 (0)