C 中的数据类型表示指向函数的任何指针
Data Type In C To Represent Any Pointer To Function
假设我对函数指针有以下定义:
typedef void(*Tmp112HardwareInit)();
typedef void(*Tmp112HardwareDeInit)();
typedef bool(*Tmp112HardwareIsBusy)();
我有一个接口对象来保存上述函数指针的实例:
typedef struct
{
Tmp112HardwareInit init;
Tmp112HardwareDeInit deinit;
Tmp112HardwareIsBusy is_busy;
} I2CInterface;
static I2CInterface _i2c_interface; // This is declared in module scope
在访问这些功能之前,我想检查一下它们是否被分配了一些东西。所以而不是
_i2c_interface.init();
我要...
if (_i2c_interface.init) _i2c_interface.init();
else error = true;
我不想为_i2c_interface
中的每个变量的每次访问重复这些行,所以我想写一个函数:
bool _SensorTMP112_InterfaceInit()
{
bool success = true;
if (_i2c_interface.init)
{
_i2c_interface.init();
}
else
{
success = false;
}
return success;
}
现在,我不想为 _i2c_interface
变量中的每个函数指针编写一个函数。我想要一个通用函数,它将被其他函数为这些函数指针中的每一个调用:
// Generic function
bool _SensorTMP112_InterfaceOperation(UnknownType fptr) // What should be UnknownType?
{
bool success = true;
if (fptr)
{
fptr();
}
else
{
success = false;
}
return success;
}
// Operation specific functions
bool _SensorTMP112_InterfaceInit()
{
return _SensorTMP112_InterfaceOperation(_i2c_interface.init);
}
bool _SensorTMP112_InterfaceDeInit()
{
return _SensorTMP112_InterfaceOperation(_i2c_interface.deinit);
}
bool _SensorTMP112_InterfaceIsBusy()
{
return _SensorTMP112_InterfaceOperation(_i2c_interface.is_busy);
}
我的问题是 UnknownType
的类型应该是什么?或者在标准 C 中甚至可能吗?或者还有其他方法可以实现我想要实现的目标吗?对非标准解决方案不感兴趣。
Quick'n'dirty 预处理器宏:
#define callfn(f) if(f) f(); else result = false
然后
callfn(_i2c_interface.init);
result = true;
callfn(_i2c_interface.deinit);
(当 bool success
较早定义时),或在函数中使用它:
bool _SensorTMP112_InterfaceInit()
{
bool success = true;
callfn(_i2c_interface.init);
return success;
}
(您也可以在宏中嵌入 bool success
和 return success
)。
但是您不应该公开该宏(即 #undef callfn
在调用它的函数体之后),因为它留下了许多敞开的门。
假设我对函数指针有以下定义:
typedef void(*Tmp112HardwareInit)();
typedef void(*Tmp112HardwareDeInit)();
typedef bool(*Tmp112HardwareIsBusy)();
我有一个接口对象来保存上述函数指针的实例:
typedef struct
{
Tmp112HardwareInit init;
Tmp112HardwareDeInit deinit;
Tmp112HardwareIsBusy is_busy;
} I2CInterface;
static I2CInterface _i2c_interface; // This is declared in module scope
在访问这些功能之前,我想检查一下它们是否被分配了一些东西。所以而不是
_i2c_interface.init();
我要...
if (_i2c_interface.init) _i2c_interface.init();
else error = true;
我不想为_i2c_interface
中的每个变量的每次访问重复这些行,所以我想写一个函数:
bool _SensorTMP112_InterfaceInit()
{
bool success = true;
if (_i2c_interface.init)
{
_i2c_interface.init();
}
else
{
success = false;
}
return success;
}
现在,我不想为 _i2c_interface
变量中的每个函数指针编写一个函数。我想要一个通用函数,它将被其他函数为这些函数指针中的每一个调用:
// Generic function
bool _SensorTMP112_InterfaceOperation(UnknownType fptr) // What should be UnknownType?
{
bool success = true;
if (fptr)
{
fptr();
}
else
{
success = false;
}
return success;
}
// Operation specific functions
bool _SensorTMP112_InterfaceInit()
{
return _SensorTMP112_InterfaceOperation(_i2c_interface.init);
}
bool _SensorTMP112_InterfaceDeInit()
{
return _SensorTMP112_InterfaceOperation(_i2c_interface.deinit);
}
bool _SensorTMP112_InterfaceIsBusy()
{
return _SensorTMP112_InterfaceOperation(_i2c_interface.is_busy);
}
我的问题是 UnknownType
的类型应该是什么?或者在标准 C 中甚至可能吗?或者还有其他方法可以实现我想要实现的目标吗?对非标准解决方案不感兴趣。
Quick'n'dirty 预处理器宏:
#define callfn(f) if(f) f(); else result = false
然后
callfn(_i2c_interface.init);
result = true;
callfn(_i2c_interface.deinit);
(当 bool success
较早定义时),或在函数中使用它:
bool _SensorTMP112_InterfaceInit()
{
bool success = true;
callfn(_i2c_interface.init);
return success;
}
(您也可以在宏中嵌入 bool success
和 return success
)。
但是您不应该公开该宏(即 #undef callfn
在调用它的函数体之后),因为它留下了许多敞开的门。