Generalized getAxisParam

This commit is contained in:
2025-06-16 15:24:28 +02:00
parent 43df40aaea
commit 31ff26cb78
2 changed files with 92 additions and 75 deletions

View File

@ -30,22 +30,23 @@ struct sinqAxisImpl {
epicsTimeStamp lastPollTime; epicsTimeStamp lastPollTime;
}; };
template <typename T> struct TypeTag {};
// Generic fallback - if the compiler tries to compile this function, it fails. // Generic fallback - if the compiler tries to compile this function, it fails.
template <typename A, typename C, typename T> template <typename A, typename C, typename T>
asynStatus setAxisParam(A *axis, C *controller, const char *indexName, asynStatus setAxisParamImpl(A *axis, C *controller, const char *indexName,
int (C::*func)(), T writeValue, int (C::*func)(), T writeValue,
const char *callerFunctionName, int lineNumber) { const char *callerFunctionName, int lineNumber,
static_assert( TypeTag<void>) {
sizeof(T) == 0, static_assert(sizeof(T) == 0, "Unsupported type for setAxisParamImpl");
"no specialization of setAxisParam exists for the given type");
return asynError; return asynError;
} }
template <typename A, typename C, typename T, template <typename A, typename C>
std::enable_if_t<std::is_same<T, int>::value, int> = 0> asynStatus setAxisParamImpl(A *axis, C *controller, const char *indexName,
asynStatus setAxisParam(A *axis, C *controller, const char *indexName, int (C::*func)(), int writeValue,
int (C::*func)(), T writeValue, const char *callerFunctionName, int lineNumber,
const char *callerFunctionName, int lineNumber) { TypeTag<int>) {
int indexValue = (axis->pController()->*func)(); int indexValue = (axis->pController()->*func)();
asynStatus status = axis->setIntegerParam(indexValue, writeValue); asynStatus status = axis->setIntegerParam(indexValue, writeValue);
if (status != asynSuccess) { if (status != asynSuccess) {
@ -55,20 +56,21 @@ asynStatus setAxisParam(A *axis, C *controller, const char *indexName,
return asynSuccess; return asynSuccess;
} }
template <typename A, typename C, typename T, template <typename A, typename C>
std::enable_if_t<std::is_same<T, bool>::value, bool> = 0> asynStatus setAxisParamImpl(A *axis, C *controller, const char *indexName,
asynStatus setAxisParam(A *axis, C *controller, const char *indexName, int (C::*func)(), bool writeValue,
int (C::*func)(), T writeValue, const char *callerFunctionName, int lineNumber,
const char *callerFunctionName, int lineNumber) { TypeTag<bool>) {
return setAxisParam(axis, indexName, func, static_cast<int>(writeValue), return setAxisParamImpl(axis, controller, indexName, func,
callerFunctionName, lineNumber); static_cast<int>(writeValue), callerFunctionName,
lineNumber, TypeTag<int>{});
} }
template <typename A, typename C, typename T, template <typename A, typename C>
std::enable_if_t<std::is_same<T, double>::value, double> = 0> asynStatus setAxisParamImpl(A *axis, C *controller, const char *indexName,
asynStatus setAxisParam(A *axis, C *controller, const char *indexName, int (sinqController::*func)(), double writeValue,
int (sinqController::*func)(), T writeValue, const char *callerFunctionName, int lineNumber,
const char *callerFunctionName, int lineNumber) { TypeTag<double>) {
int indexValue = (axis->pController()->*func)(); int indexValue = (axis->pController()->*func)();
asynStatus status = axis->setDoubleParam(indexValue, writeValue); asynStatus status = axis->setDoubleParam(indexValue, writeValue);
if (status != asynSuccess) { if (status != asynSuccess) {
@ -78,11 +80,11 @@ asynStatus setAxisParam(A *axis, C *controller, const char *indexName,
return asynSuccess; return asynSuccess;
} }
template <typename A, typename C, typename T, template <typename A, typename C>
std::enable_if_t<std::is_same<T, char *>::value, char *> = 0> asynStatus setAxisParamImpl(A *axis, C *controller, const char *indexName,
asynStatus setAxisParam(A *axis, C *controller, const char *indexName, int (C::*func)(), char *writeValue,
int (C::*func)(), T writeValue, const char *callerFunctionName, int lineNumber,
const char *callerFunctionName, int lineNumber) { TypeTag<char *>) {
int indexValue = (axis->pController()->*func)(); int indexValue = (axis->pController()->*func)();
asynStatus status = axis->setStringParam(indexValue, writeValue); asynStatus status = axis->setStringParam(indexValue, writeValue);
if (status != asynSuccess) { if (status != asynSuccess) {
@ -92,12 +94,11 @@ asynStatus setAxisParam(A *axis, C *controller, const char *indexName,
return asynSuccess; return asynSuccess;
} }
template < template <typename A, typename C>
typename A, typename C, typename T, asynStatus setAxisParamImpl(A *axis, C *controller, const char *indexName,
std::enable_if_t<std::is_same<T, const char *>::value, const char *> = 0> int (C::*func)(), const char *writeValue,
asynStatus setAxisParam(A *axis, C *controller, const char *indexName, const char *callerFunctionName, int lineNumber,
int (C::*func)(), T writeValue, TypeTag<const char *>) {
const char *callerFunctionName, int lineNumber) {
int indexValue = (axis->pController()->*func)(); int indexValue = (axis->pController()->*func)();
asynStatus status = axis->setStringParam(indexValue, writeValue); asynStatus status = axis->setStringParam(indexValue, writeValue);
if (status != asynSuccess) { if (status != asynSuccess) {
@ -107,22 +108,31 @@ asynStatus setAxisParam(A *axis, C *controller, const char *indexName,
return asynSuccess; return asynSuccess;
} }
template <typename A, typename C, typename T>
asynStatus setAxisParam(A *axis, C *controller, const char *indexName,
int (C::*func)(), T writeValue,
const char *callerFunctionName, int lineNumber) {
return setAxisParamImpl(axis, controller, indexName, func, writeValue,
callerFunctionName, lineNumber, TypeTag<T>{});
}
// Generic fallback - if the compiler tries to compile this function, it fails. // Generic fallback - if the compiler tries to compile this function, it fails.
template <typename A, typename C, typename T> template <typename A, typename C, typename T>
asynStatus getAxisParam(A *axis, C *controller, const char *indexName, asynStatus getAxisParamImpl(A *axis, C *controller, const char *indexName,
int (C::*func)(), T *readValue, int (C::*func)(), T *readValue,
const char *callerFunctionName, int lineNumber) { const char *callerFunctionName, int lineNumber,
TypeTag<void>) {
static_assert( static_assert(
sizeof(T) == 0, sizeof(T) == 0,
"no specialization of getAxisParam exists for the given type"); "no specialization of getAxisParam exists for the given type");
return asynError; return asynError;
} }
template <typename A, typename C, typename T, template <typename A, typename C>
std::enable_if_t<std::is_same<T, int>::value, int> = 0> asynStatus getAxisParamImpl(A *axis, C *controller, const char *indexName,
asynStatus getAxisParam<int>(A *axis, C *controller, const char *indexName, int (C::*func)(), int *readValue,
int (C::*func)(), T *readValue, const char *callerFunctionName, int lineNumber,
const char *callerFunctionName, int lineNumber) { TypeTag<int>) {
int indexValue = (axis->pController()->*func)(); int indexValue = (axis->pController()->*func)();
asynStatus status = axis->pController()->getIntegerParam( asynStatus status = axis->pController()->getIntegerParam(
axis->axisNo(), indexValue, readValue); axis->axisNo(), indexValue, readValue);
@ -133,21 +143,20 @@ asynStatus getAxisParam<int>(A *axis, C *controller, const char *indexName,
return asynSuccess; return asynSuccess;
} }
template <typename A, typename C, typename T, template <typename A, typename C>
std::enable_if_t<std::is_same<T, bool>::value, bool> = 0> asynStatus getAxisParamImpl(A *axis, C *controller, const char *indexName,
asynStatus getAxisParam<bool>(A *axis, C *controller, const char *indexName, int (C::*func)(), bool *readValue,
int (C::*func)(), T *readValue, const char *callerFunctionName, int lineNumber,
const char *callerFunctionName, int lineNumber) { TypeTag<bool>) {
return getAxisParam(axis, indexName, func, (int *)readValue, return getAxisParamImpl(axis, indexName, func, (int *)readValue,
callerFunctionName, lineNumber); callerFunctionName, lineNumber);
} }
template <typename A, typename C, typename T, template <typename A, typename C>
std::enable_if_t<std::is_same<T, double>::value, double> = 0> asynStatus getAxisParamImpl(A *axis, C *controller, const char *indexName,
asynStatus getAxisParam<double>(A *axis, C *controller, const char *indexName, int (C::*func)(), double *readValue,
int (C::*func)(), T *readValue, const char *callerFunctionName, int lineNumber,
const char *callerFunctionName, TypeTag<double>) {
int lineNumber) {
int indexValue = (axis->pController()->*func)(); int indexValue = (axis->pController()->*func)();
asynStatus status = axis->pController()->getDoubleParam( asynStatus status = axis->pController()->getDoubleParam(
axis->axisNo(), indexValue, readValue); axis->axisNo(), indexValue, readValue);
@ -158,11 +167,11 @@ asynStatus getAxisParam<double>(A *axis, C *controller, const char *indexName,
return asynSuccess; return asynSuccess;
} }
template <typename A, typename C, typename T, template <typename A, typename C>
std::enable_if_t<std::is_same<T, char>::value, char> = 0> asynStatus getAxisParamImpl(A *axis, C *controller, const char *indexName,
asynStatus getAxisParam<char>(A *axis, C *controller, const char *indexName, int (C::*func)(), char *readValue,
int (C::*func)(), T *readValue, const char *callerFunctionName, int lineNumber,
const char *callerFunctionName, int lineNumber) { TypeTag<char>) {
int maxChars = 200; int maxChars = 200;
@ -176,22 +185,22 @@ asynStatus getAxisParam<char>(A *axis, C *controller, const char *indexName,
return asynSuccess; return asynSuccess;
} }
template <typename A, typename C, typename T, template <typename A, typename C, size_t N>
std::enable_if_t<std::is_same<T, char>::value, char> = 0, size_t N> asynStatus getAxisParamImpl(A *axis, C *controller, const char *indexName,
asynStatus getAxisParam(A *axis, C *controller, const char *indexName, int (C::*func)(), char (&readValue)[N],
int (C::*func)(), T (&readValue)[N], const char *callerFunctionName, int lineNumber,
const char *callerFunctionName, int lineNumber) { TypeTag<char>) {
// Decay the array to char* // Decay the array to char*
return getAxisParam<char>(axis, controller, indexName, func, return getAxisParamImpl(axis, controller, indexName, func,
static_cast<char *>(readValue), static_cast<char *>(readValue), callerFunctionName,
callerFunctionName, lineNumber); lineNumber);
} }
template <typename A, typename C> template <typename A, typename C>
asynStatus asynStatus getAxisParamImpl(A *axis, C *controller, const char *indexName,
getAxisParam<std::string>(A *axis, C *controller, const char *indexName,
int (C::*func)(), std::string *readValue, int (C::*func)(), std::string *readValue,
const char *callerFunctionName, int lineNumber) { const char *callerFunctionName, int lineNumber,
TypeTag<std::string>) {
int indexValue = (axis->pController()->*func)(); int indexValue = (axis->pController()->*func)();
// Convert the pointer to a reference, since getStringParam expects the // Convert the pointer to a reference, since getStringParam expects the
@ -207,6 +216,14 @@ getAxisParam<std::string>(A *axis, C *controller, const char *indexName,
return asynSuccess; return asynSuccess;
} }
template <typename A, typename C, typename T>
asynStatus getAxisParam(A *axis, C *controller, const char *indexName,
int (C::*func)(), std::string *readValue,
const char *callerFunctionName, int lineNumber) {
return getAxisParamImpl(axis, controller, indexName, func, readValue,
callerFunctionName, lineNumber, TypeTag<T>{});
}
// ============================================================================= // =============================================================================
sinqAxis::sinqAxis(class sinqController *pC, int axisNo) sinqAxis::sinqAxis(class sinqController *pC, int axisNo)

View File

@ -502,9 +502,9 @@ asynStatus setAxisParam(A *axis, C *controller, const char *indexName,
* @return asynStatus * @return asynStatus
*/ */
template <typename A, typename C, typename T> template <typename A, typename C, typename T>
asynStatus getAxisParam(A *axis, const char *indexName, int (C::*func)(), asynStatus getAxisParam(A *axis, C *controller, const char *indexName,
T *readValue, const char *callerFunctionName, int (C::*func)(), T *readValue,
int lineNumber); const char *callerFunctionName, int lineNumber);
/** /**
* @brief Macro to get an paramLib parameter and error checking the return value * @brief Macro to get an paramLib parameter and error checking the return value