首页 文章

Boost ::将sigaction函数引用绑定到实例

提问于
浏览
0

我想使用Linux内核信号来异步地将事件的发生从模块传递到用户空间应用程序 . 我通过以下方式在C中工作:

void rcv_signal(int n, siginfo_t *info, void *unused)
{
   // Do something interesting with the signal
}

// Register for updates from the kernel
int main(int argc, char *argv[])
{
    struct sigaction sig;
    sig.sa_sigaction = rcv_signal; // Function pointer
    sig.sa_flags = SA_SIGINFO;
    sigaction(SOME_CUSTOM_SIGNAL, &sig, NULL);

    // Code to wait() then return
}

现在,我想转向C实现 . 更具体地说,我想使用boost :: function / boost :: bind将sa_sigaction绑定到方法 InputCapture::receive . 但是,我正在努力获得正确的功能签名 .

以下是InputCapture类的定义:

class InputCapture
{
    public: InputCapture(uint8_t intimer) : timer(_timer) {}
    public: ~InputCapture() {}
    private: void receive(int n, siginfo_t *info, void *unused) {}
    private: uint8_t timer;
};

这是修改后的sa_sigaction:

// Register for updates from the kernel
struct sigaction sig;
sig.sa_sigaction = boost::function<void (int n, siginfo_t *info, void *unused)> (
    boost::bind(&InputCapture::receive, this, _1));
sig.sa_flags = SA_SIGINFO;
sigaction(SOME_CUSTOM_SIGNAL, &sig, NULL);

但是,我收到以下编译错误:

来自/home/asymingt/export/rootfs/usr/include/boost/bind.hpp:22:0的文件,来自/home/asymingt/Workspace/Source/roseline/timesync/src/stamp/LogicalStamp.hpp: 12,来自/home/asymingt/Workspace/Source/roseline/timesync/src/stamp/InputCapture.hpp:4,来自/home/asymingt/Workspace/Source/roseline/timesync/src/stamp/InputCapture.cpp:1: /home/asymingt/export/rootfs/usr/include/boost/bind/bind.hpp:在'struct boost :: _ bi :: result_traits'的实例化中:/ home / asymingt / export / rootfs / usr / include / boost / bind / bind_template.hpp:15:48:从'类boost :: _ bi :: bind_t)(int,siginfo,void *),boost :: _ bi :: list2,boost :: arg <1>>>'/ home / asymingt / Workspace / Source / roseline / timesync / src / stamp / InputCapture.cpp:41:57:从这里需要/home/asymingt/export/rootfs/usr/include/boost/bind/bind.hpp:69: 37:错误:'void(timesync :: InputCapture ::)(int,siginfo,void *)'不是类,结构或联合类型typedef typename F :: result_type type; ^ /home/asymingt/Workspace/Source/roseline/timesync/src/stamp/InputCapture.cpp:在构造函数'timesync :: InputCapture :: InputCapture(uint8_t)':/ home / asymingt / Workspace / Source / roseline / timesync / src / stamp / InputCapture.cpp:40:19:错误:无法将'boost :: function'转换为'void()(int,siginfo_t,void *){aka void()(int,siginfo,void *)}'在赋值sig.sa_sigaction = boost :: function(^文件包含在/home/asymingt/export/rootfs/usr/include/boost/function/detail/maybe_include.hpp:28:0,来自/ home / asymingt / export /rootfs/usr/include/boost/function/detail/function_iterate.hpp:14,来自/home/asymingt/export/rootfs/usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:62,来自/home/asymingt/export/rootfs/usr/include/boost/function.hpp:64,来自/home/asymingt/Workspace/Source/roseline/timesync/src/stamp/InputCapture.cpp:3:/ home / asymingt / export / rootfs / usr / include / boost / function / function_template.hpp:在'static void boost :: detail :: functio的实例化中n :: void_function_obj_invoker3 :: invoke(boost :: detail :: function :: function_buffer&,T0,T1,T2)[with FunctionObj = boost :: _ bi :: bind_t)(int,siginfo,void *),boost :: _ bi :: list2,boost :: arg <1 >>>; R =无效; T0 = int; T1 = siginfo *; T2 = void *]':/ home /asymingt/export/rootfs/usr/include/boost/function/function_template.hpp:907:38:从'void boost :: function3 :: assign_to(Functor)[需要Functor = boost :: _ bi :: bind_t)(int,siginfo,void *),boost :: _ bi :: list2,boost :: arg <1 >>>; R =无效; T0 = int; T1 = siginfo *; T2 = void *]'/ home /asymingt/export/rootfs/usr/include/boost/function/function_template.hpp:722:7:从'boost :: function3 :: function3中需要(Functor,typename boost :: enable_if_c: :value> :: value,int> :: type)[with Functor = boost :: _ bi :: bind_t)(int,siginfo,void *),boost :: _ bi :: list2,boost :: arg <1 >> >; R =无效; T0 = int; T1 = siginfo ; T2 =无效; typename boost :: enable_if_c :: value> :: value,int> :: type = int]'/ home /asymingt/export/rootfs/usr/include/boost/function/function_template.hpp:1042:16:required from' boost :: function :: function(Functor,typename boost :: enable_if_c :: value> :: value,int> :: type)[with Functor = boost :: _ bi :: bind_t)(int,siginfo,void *), boost :: _ bi :: list2,boost :: arg <1 >>>; R =无效; T0 = int; T1 = siginfo ; T2 =无效; typename boost :: enable_if_c :: value> :: value,int> :: type = int]'/ home /asymingt/Workspace/Source/roseline/timesync/src/stamp/InputCapture.cpp:41:58:从这里需要/home/asymingt/export/rootfs/usr/include/boost/function/function_template.hpp:153:57:错误:无法调用'(boost :: _ bi :: bind_t)(int,siginfo,void *) ,boost :: _ bi :: list2,boost :: arg <1 >>>)(int&,siginfo *&,void &)'BOOST_FUNCTION_RETURN(( f)(BOOST_FUNCTION_ARGS)); ^ /home/asymingt/export/rootfs/usr/include/boost/function/function_template.hpp:75:36:注意:在宏'BOOST_FUNCTION_RETURN'的定义中#define BOOST_FUNCTION_RETURN(X)X

是我想要的实现可能,如果可能,我哪里出错了?

1 回答

  • 1

    你不能做这个 .

    请注意 sigaction::sa_sigaction 是指向函数的指针 . boost::functionboost::bind 的返回值都不能转换为函数指针!

相关问题