如何使用带有sdbus回调的c ++ 14可变参数模板

时间:2017-12-15 19:14:38

标签: c++ c++11 c++14 variadic-templates dbus

sd-bus在定义d-bus方法时需要一个回调函数。正如我在做C ++ 14,我希望对类对象on_msg_method_here()函数进行调用。我想要实现的是这样的(在伪c ++中):

int callback_dbus_method_foo( message* msg, void* userdata, ... )
{
     MyClass* cls = (MyClass*)userdata;
     Type0 var0;
     if ( message_process( msg, signature[0], &var0 ) != 0 )
        //.. error here
     Type1 var1;
     if ( message_process( msg, signature[1], &var1 ) != 0 )
        //.. error here
     //... and these continue from 0 to N
     TypeN varN;
     if ( message_process( msg, signature[N], &varN ) != 0 )
        //.. error here
     int dbus_ret = cls->on_msg_method_foo( var1, var2, ..., varN )          
     handle_dbus_ret( msg, dbus_ret // ... );
     return 0;
}

int MyClass::register_callbacks( ... )
{

  // Well really we have something really different, this is to demonstrate

  // pass 'this' as userdata* to callback function
  dbus_register_callback( "method_foo", 
         &callback_dbus_method_foo, this )
}

现在我知道我可以用C-macro执行此操作,但如何使用C ++ 14 varidic宏正确

据我所知,调用某些类对象某些方法的麻烦可以用std :: bind处理(并通过userdata指针传递),变量声明和message_process可以通过可变参数模板剥离完成,但是如何将那些声明的变量(伪代码示例中的var0,var1,..)正确扩展到调用?总之,如何做到这一点:

MyClass::register_callbacks()
{
   Mystic fun_to_call = std::bind( &MyClass::on_dbus_method_foo, this ); 
   dbus_register_callback( "method_foo", 
            super_magic_template<int,double,bool>, &fun_to_call );
}

1 个答案:

答案 0 :(得分:0)

为了获得优雅和通用的解决方案,我会做一些事情。

我们需要一种方法来收集变量(var0,var1,...,varN)并将它们传递给函数。为此,我首先会有一个包装器来查询这些变量,因为它是索引i。我不确定你的例子中有signature,但我相信你可以解决这个问题。

template <class T>
T get_var(message* msg, unsigned i) {
  T var;
  if ( message_process( msg, signature[i], &var ) != 0)
    throw std::runtime_error("Oups"); // In this context, it's easier to deal with errors with exception.
  return var;
}

然后,我们可以通过解压缩可变参数模板参数以及用于索引的相关index_sequence来收集所有变量。像

这样的东西
template <class... Vars, class F>
void callback_wrapper(F& fcn, message* msg) {
  callback_wrapper_impl(fcn, msg, std::make_index_sequence<sizeof...(Vars)>());
}
template <class... Vars, class F, size_t... i>
void callback_wrapper_impl(F& fcn, message* msg, std::index_sequence<i...>) {
  fcn(get_var<Vars>(msg, i)...);
}

使用std :: bind会产生另一个困难,它会返回类似函数的对象fun_to_call。我们不能将它作为函数指针传递给dbus_register_callback,它不带任何数据,也不能将指针作为userdata传递给它,因为fun_to_call是局部变量,因此它的生命周期是太短了。

我不会仅依赖于super_magic_template回调,而是围绕dbus_register_callback做一个提供更简单界面的包装,让我们称之为modern_dbus_register_callback。我看到的最直接的解决方案是以内存分配和额外的间接级别为代价来使用动态存储持续时间 - 这类似于std :: function中使用的类型擦除。请注意,您可以sizeof(fun_to_call) < sizeof(void*)优化此项,方法是将fun_to_call作为userdata传递给值 - 这是小值优化。我相信使用没有捕获的lambda可能是有用的,因为它们是可转换为函数指针并避免大量模板样板。可能需要一些额外的工作来处理错误,同时避免内存泄漏。

template <class... Vars, class F>
void modern_dbus_register_callback(const char* name, F& fcn) {
  std::unique_ptr<F> fcn_ptr = std::make_unique<F>(fcn);
  dbus_register_callback(name, [](message* msg, void* userdata){
    std::unique_ptr<F> fcn_ptr(static_cast<F*>(userdata));
    callback_wrapper<Vars...>(*fcn_ptr, msg);
  }, fcn_ptr.release());
}

然后可以将其用作

modern_dbus_register_callback<int,double,bool>("method_foo", fun_to_call);
相关问题