Applied PIMPL principle

This commit is contained in:
2025-05-23 12:16:25 +02:00
parent e0a74c5598
commit d1d694ad6b
5 changed files with 328 additions and 98 deletions

View File

@@ -12,6 +12,15 @@
#include <string>
#include <unistd.h>
struct masterMacsControllerImpl {
double comTimeout;
};
/*
Stores the constructor input comTimeout
*/
double;
/**
* @brief Copy src into dst and replace all NULL terminators up to the carriage
* return with spaces. This allows to print *dst with asynPrint.
@@ -59,8 +68,10 @@ masterMacsController::masterMacsController(const char *portName,
// Initialization of local variables
asynStatus status = asynSuccess;
// Initialization of all member variables
comTimeout_ = comTimeout;
pMasterMacsC_ =
std::make_unique<masterMacsControllerImpl>((masterMacsControllerImpl){
.comTimeout = comTimeout,
});
// =========================================================================
@@ -158,7 +169,7 @@ asynStatus masterMacsController::writeRead(int axisNo, int tcpCmd,
// Check if a custom timeout has been given
if (comTimeout < 0.0) {
comTimeout = comTimeout_;
comTimeout = pMasterMacsC_->comTimeout;
}
masterMacsAxis *axis = getMasterMacsAxis(axisNo);
@@ -192,7 +203,7 @@ asynStatus masterMacsController::writeRead(int axisNo, int tcpCmd,
msgPrintControlKey comKey =
msgPrintControlKey(portName, axisNo, __PRETTY_FUNCTION__, __LINE__);
if (status != asynSuccess) {
if (msgPrintControl_.shouldBePrinted(comKey, true, pasynUserSelf)) {
if (getMsgPrintControl().shouldBePrinted(comKey, true, pasynUserSelf)) {
char printableCommand[MAXBUF_] = {0};
adjustForPrint(printableCommand, fullCommand, MAXBUF_);
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
@@ -202,7 +213,7 @@ asynStatus masterMacsController::writeRead(int axisNo, int tcpCmd,
stringifyAsynStatus(status), printableCommand);
}
} else {
msgPrintControl_.resetCount(comKey, pasynUserSelf);
getMsgPrintControl().resetCount(comKey, pasynUserSelf);
}
// Create custom error messages for different failure modes
@@ -252,21 +263,34 @@ asynStatus masterMacsController::writeRead(int axisNo, int tcpCmd,
pl_status = axis->setIntegerParam(this->motorStatusCommsError_, 0);
} else {
// Check if the axis already is in an error communication mode. If
// it is not, upstream the error. This is done to avoid "flooding"
// the user with different error messages if more than one error
// ocurred before an error-free communication
/*
Since the communication failed, there is the possibility that the
controller is not connected at all to the network. In that case, we
cannot be sure that the information read out in the init method of the
axis is still up-to-date the next time we get a connection. Therefore,
an info flag is set which the axis object can use at the start of its
poll method to try to initialize itself.
*/
axis->setNeedInit(true);
/*
Check if the axis already is in an error communication mode. If
it is not, upstream the error. This is done to avoid "flooding"
the user with different error messages if more than one error
ocurred before an error-free communication
*/
pl_status =
getIntegerParam(axisNo, motorStatusProblem_, &motorStatusProblem);
if (pl_status != asynSuccess) {
return paramLibAccessFailed(pl_status, "motorStatusProblem_",
axisNo, __PRETTY_FUNCTION__, __LINE__);
return paramLibAccessFailed(pl_status, "motorStatusProblem", axisNo,
__PRETTY_FUNCTION__, __LINE__);
}
if (motorStatusProblem == 0) {
pl_status = axis->setStringParam(motorMessageText_, drvMessageText);
pl_status =
axis->setStringParam(motorMessageText(), drvMessageText);
if (pl_status != asynSuccess) {
return paramLibAccessFailed(pl_status, "motorMessageText_",
return paramLibAccessFailed(pl_status, "motorMessageText",
axisNo, __PRETTY_FUNCTION__,
__LINE__);
}
@@ -405,15 +429,15 @@ asynStatus masterMacsController::parseResponse(
"Tried to write with a read-only command. This is a "
"bug, please call the support.");
if (msgPrintControl_.shouldBePrinted(parseKey, true,
pasynUserSelf)) {
if (getMsgPrintControl().shouldBePrinted(parseKey, true,
pasynUserSelf)) {
adjustForPrint(printableCommand, fullCommand, MAXBUF_);
asynPrint(
this->pasynUserSelf, ASYN_TRACE_ERROR,
"Controller \"%s\", axis %d => %s, line %d:\nTried to "
"write with the read-only command %s.%s\n",
portName, axisNo, __PRETTY_FUNCTION__, __LINE__,
printableCommand, msgPrintControl_.getSuffix());
printableCommand, getMsgPrintControl().getSuffix());
}
responseValid = false;
break;
@@ -421,7 +445,7 @@ asynStatus masterMacsController::parseResponse(
}
if (responseValid) {
msgPrintControl_.resetCount(parseKey, pasynUserSelf);
getMsgPrintControl().resetCount(parseKey, pasynUserSelf);
// Check if the response matches the expectations. Each response
// contains the string "axisNo R tcpCmd" (including the spaces)
@@ -445,15 +469,15 @@ asynStatus masterMacsController::parseResponse(
adjustForPrint(printableCommand, fullCommand, MAXBUF_);
adjustForPrint(printableResponse, fullResponse, MAXBUF_);
if (msgPrintControl_.shouldBePrinted(parseKey, true,
pasynUserSelf)) {
if (getMsgPrintControl().shouldBePrinted(parseKey, true,
pasynUserSelf)) {
asynPrint(this->pasynUserSelf, ASYN_TRACEIO_DRIVER,
"Controller \"%s\", axis %d => %s, line "
"%d:\nMismatched "
"response %s to command %s.%s\n",
portName, axisNo, __PRETTY_FUNCTION__, __LINE__,
printableResponse, printableCommand,
msgPrintControl_.getSuffix());
getMsgPrintControl().getSuffix());
}
snprintf(drvMessageText, MAXBUF_,
@@ -462,7 +486,7 @@ asynStatus masterMacsController::parseResponse(
printableResponse, printableCommand);
return asynError;
} else {
msgPrintControl_.resetCount(responseMatchKey, pasynUserSelf);
getMsgPrintControl().resetCount(responseMatchKey, pasynUserSelf);
}
}
return asynSuccess;
@@ -471,7 +495,7 @@ asynStatus masterMacsController::parseResponse(
asynStatus masterMacsController::readInt32(asynUser *pasynUser,
epicsInt32 *value) {
// masterMacs can be disabled
if (pasynUser->reason == motorCanDisable_) {
if (pasynUser->reason == motorCanDisable()) {
*value = 1;
return asynSuccess;
} else {
@@ -479,6 +503,8 @@ asynStatus masterMacsController::readInt32(asynUser *pasynUser,
}
}
double masterMacsController::comTimeout() { return pMasterMacsC_->comTimeout; }
/***************************************************************************/
/** The following functions are C-wrappers, and can be called directly from
* iocsh */