C++ 可变参数回调

C++ variadic callback

C++ 中的可变参数函数允许用户使用任意数量的参数调用函数。例如,sscanf 将要解析的字符串、格式字符串和一组将采用已解析项的值的参数作为输入。 如果我想让这个范例异步怎么办?我需要解析一些数据,从一些字节中提取可变数量的参数。需要提取的参数在 sscanf 中以格式字符串指定。我希望我的函数被这样调用:

function <void (int, int, int)> myfunc = [=] (int a, int b, int c)
{
    // Do something here!
};

asyncsscanf(my_bytes, "iii", myfunc);

asyncsscanf 应该进行所需的处理,完成后我希望它使用格式字符串中指定的正确参数调用 myfunc。 有没有可能做这样的事情?

谢谢

我不知道如何用你的方法做到这一点。

首先,asyncscanf函数的第三个参数没有好的类型。只有在 asyncscanf 主体中可接受的是 void(...) (函数接受无限数量的参数并且 returns 什么都没有),但是作为第三个传递给 asyncscanf 的参数应该是那种类型,这可能是不可接受的。

其次,您必须根据格式(例如 "iii")进行 my_bytes 的调度。如果您有有限数量的不同格式字符串(那么您可以 'switch' 所有可能的格式),就可以这样做。但一般情况下我想是做不到的。

但是因为您用 'variadic-templates' 标记了您的问题,所以我假设您使用的是 C++11/14。 也许您想将 asyncscanf 的格式参数设为更具可读性的模板参数(我假设格式在编译时始终已知)。下面是解决方案的片段。

#include <functional>
#include <iostream>

// template parsing function, remaining_bytes parameter should contain pointer to not parsed part
// of my_bytes
template <typename return_type> return_type parse(const char *my_bytes, const char *&remaining_bytes);

// specialization of parsing function for desired types, fake implementation
template <> int parse<int>(const char *my_bytes, const char *&remaining_bytes) {
    remaining_bytes = my_bytes;
    return 0;
}

// specialization of parsing function for desired types, fake implementation
template <> long parse<long>(const char *my_bytes, const char *&remaining_bytes) {
    remaining_bytes = my_bytes;
    return 1;
}

// declare helper template for general case
template <typename to_be_parsed_tuple, typename parsed_tuple>
struct asyncscanf_helper;

// all params parsed case
template <typename... parsed_params>
struct asyncscanf_helper<std::tuple<>, std::tuple<parsed_params...>> {
    void operator()(const char *, std::function<void(parsed_params...)> fun, parsed_params... params) {
        fun(params...);
    }
};

// some params to be parsed case
template <typename first_param_to_be_parsed, typename...to_be_parsed_params, typename... parsed_params>
struct asyncscanf_helper<std::tuple<first_param_to_be_parsed, to_be_parsed_params...>, std::tuple<parsed_params...>> {
    void operator()(const char *my_bytes, std::function<void(parsed_params..., first_param_to_be_parsed, to_be_parsed_params...)> fun, parsed_params... params) {
        const char *remaining_bytes = 0;
        first_param_to_be_parsed p1 = parse<first_param_to_be_parsed>(my_bytes, remaining_bytes);
        asyncscanf_helper<std::tuple<to_be_parsed_params...>, std::tuple<parsed_params..., first_param_to_be_parsed>>()(remaining_bytes, fun, params..., p1);
    }
};

template <typename... params>
void asyncscanf(const char *my_bytes, void function(params...)) {
    asyncscanf_helper<std::tuple<params...>, std::tuple<>>()(my_bytes, function);
}

void test_fun(int a, int b, int c) {
    std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;
}

void test_fun2(int a, long b, int c) {
    std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;
}

int main() {
    asyncscanf("1 2 3", test_fun);
    asyncscanf("1 2 3", test_fun2);
}

代码注释:

  • 想法是我们有两个参数包:一个用于尚未解析的参数,一个用于已解析的参数,并将参数从第一个包一个一个地传递到第二个;解析完所有参数后,只需调用函数
  • 如果您忘记为作为第三个参数传递给 asyncscanf 的函数所需的类型专门化解析函数,编译器会告诉您。
  • 我不得不使用模板结构 asyncscanf_helper 和元组而不是简单的函数模板 asyncscanf_helper 因为在一个模板函数中使用两个参数包会出现问题。
  • 我在 asyncscanf_helper 中使用了 std::function,因为它更通用,您可以使用例如lambdas 作为你的参数,但目前我将 asyncscanf 中的标准函数保留为参数类型,因为否则它的第二个参数必须显式转换为 std::function 并具有适当的签名或模板参数必须明确说明。
  • 由于解析函数特化的虚假实现,如果您 运行 代码,您将看不到预期的结果,但因为解析不是您问题的一部分,所以我将其忽略。