Files
qt5-legatus-legacy/Lts/DDS/Core/function_reply_object.h
T
2026-08-13 18:33:10 +02:00

117 lines
3.5 KiB
C++
Executable File

/**
* Helper class to properly fill FunctionReply objects
* $Id$
* $HeadURL$
* $Author$
*/
#pragma once
#include <string>
#include <vector>
#include "core_export.h"
#ifndef tr_lts
#define tr_lts(ctx, t) (t)
#endif
namespace Core
{
/**
* @brief An object to handle replies for function requests
* It tries to simulate QString::arg() behaviour and collects all arguments.
* --> a string must contain '%1' , '%2', ... as argument placeholder like a Qt string
* which is used for translation.
* The complete object can then be passed back to the FTS reply handler which
* puts them into a Core::FunctionReply dds structure.
* On the client side, the data from the Core::FunctionReply structure is
* automatically put into the FunctionReplyObject so that the user can
* easily access the result of the function request.
* To check if there is an error, the function error() can be called.
* To get the error translated string with all arguments replaced, simply call translated()
* To get an untranslated output (e.g. for logging), use std::string operator()
*/
class CORE_COMMON_EXPORT FunctionReplyObject
{
public:
explicit FunctionReplyObject(int id = -1, const char *str = nullptr)
: m_str(str ? str : std::string())
, m_id(id)
{}
explicit FunctionReplyObject(const char *str)
: m_str(str ? str : std::string())
{}
explicit FunctionReplyObject(int id, const std::string &errStr, const std::vector<std::string> &errArgs)
: m_str(errStr)
, m_args(errArgs)
, m_id(id)
{}
explicit FunctionReplyObject(int id, std::string &&errStr, std::vector<std::string> &&errArgs)
: m_str(std::move(errStr))
, m_args(std::move(errArgs))
, m_id(id)
{}
explicit FunctionReplyObject(int id, const std::string &errStr, const std::vector<std::string> &errArgs, const std::string &additionalInfo)
: m_str(errStr)
, m_additionalInfo(additionalInfo)
, m_args(errArgs)
, m_id(id)
{}
explicit FunctionReplyObject(int id, std::string &&errStr, std::vector<std::string> &&errArgs, std::string &&additionalInfo)
: m_str(std::move(errStr))
, m_additionalInfo(std::move(additionalInfo))
, m_args(std::move(errArgs))
, m_id(id)
{}
~FunctionReplyObject() = default;
// simulate QString::arg() behaviour
FunctionReplyObject &arg(qint64 val);
FunctionReplyObject &arg(const std::string &str);
FunctionReplyObject &arg(const QString &str);
/**
* @brief true if the object contains an error description
*/
bool error() const { return !m_str.empty(); }
void setId(int id) { m_id = id; }
FunctionReplyObject &setStr(const char *str) { m_str = str; return *this; }
FunctionReplyObject &setAdditionalInfo(const char *additionalInfo) { m_additionalInfo = additionalInfo; return *this; }
int id() const { return m_id; }
std::string str() const { return m_str; }
std::string additionalInfo() const { return m_additionalInfo; }
std::vector<std::string> args() const { return m_args; }
/**
* @brief convert the error string and arguments to an untranslated std::string
* e.g. for logging output
*/
std::string operator()() const;
/**
* @brief convert and translate the error string and arguments to a translated QString
* The translation is done via QCoreApplication::translate("", errStr)
*/
QString translated() const;
int64_t uniqueId() const { return m_uniqueId; }
void setUniqueId(int64_t uniqueId) { m_uniqueId = uniqueId; }
private:
std::string m_str;
std::string m_additionalInfo;
std::vector<std::string> m_args;
int64_t m_uniqueId = 0;
int m_id = -1;
};
}