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

时间:2015-04-27 16:52:14

标签: c++ boost

我想使用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:需要来自'class boost :: _ bi :: bind_t)(int,siginfo,void *),boost :: _ bi :: list2,boost :: arg&lt; 1&gt; &GT; &GT;”   /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 :: function :: void_function_obj_invoker3 :: invoke(boost :: detail :: function ::)的实例化中function_buffer&amp;,T0,T1,T2)[with FunctionObj = boost :: _ bi :: bind_t)(int,siginfo,void *),boost :: _ bi :: list2,boost :: arg&lt; 1&gt; &GT;取代; 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&lt; 1&gt; &GT;取代; R =无效; T0 = int; T1 = siginfo *; T2 =无效*]'   /home/asymingt/export/rootfs/usr/include/boost/function/function_template.hpp:722:7:需要来自'boost :: function3 :: function3(Functor,typename boost :: enable_if_c :: value&gt; :: value) ,int&gt; :: type)[with Functor = boost :: _ bi :: bind_t)(int,siginfo,void *),boost :: _ bi :: list2,boost :: arg&lt; 1&gt; &GT;取代; R =无效; T0 = int; T1 = siginfo *; T2 =无效*; typename boost :: enable_if_c :: value&gt; :: value,int&gt; :: type = int]'   /home/asymingt/export/rootfs/usr/include/boost/function/function_template.hpp:1042:16:从'boost :: function :: function(Functor,typename boost :: enable_if_c :: value&gt; :: value)中需要,int&gt; :: type)[with Functor = boost :: _ bi :: bind_t)(int,siginfo,void *),boost :: _ bi :: list2,boost :: arg&lt; 1&gt; &GT;取代; R =无效; T0 = int; T1 = siginfo *; T2 =无效*; typename boost :: enable_if_c :: value&gt; :: value,int&gt; :: 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&lt; 1&gt; &GT; &gt;)(int&amp;,siginfo *&amp;,void *&amp;)'              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 个答案:

答案 0 :(得分:1)

你不能这样做。

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