param: redefine type-safe versions for param_get() when used in C++ code

This commit is contained in:
Beat Küng
2017-10-27 10:43:15 +02:00
committed by Lorenz Meier
parent 4416c4ddb3
commit c35b0aa270
+31
View File
@@ -451,4 +451,35 @@ struct param_info_s {
__END_DECLS
#ifdef __cplusplus
#if 0 // set to 1 to debug param type mismatches
#include <cstdio>
#define CHECK_PARAM_TYPE(param, type) \
if (param_type(param) != type) { \
/* use printf() to avoid having to use more includes */ \
printf("wrong type passed to param_get() for param %s\n", param_name(param)); \
}
#else
#define CHECK_PARAM_TYPE(param, type)
#endif
// param is a C-interface. This means there is no overloading, and thus no type-safety for param_get().
// So for C++ code we redefine param_get() to inlined overloaded versions, which gives us type-safety
// w/o having to use a different interface
static inline int param_get_cplusplus(param_t param, float *val)
{
CHECK_PARAM_TYPE(param, PARAM_TYPE_FLOAT);
return param_get(param, val);
}
static inline int param_get_cplusplus(param_t param, int32_t *val)
{
CHECK_PARAM_TYPE(param, PARAM_TYPE_INT32);
return param_get(param, val);
}
#define param_get(param, val) param_get_cplusplus(param, val)
#endif /* __cplusplus */
#endif