执行控制的c++函数代理

c++ function proxy for execution control

本文关键字:函数 代理 c++ 控制 执行      更新时间:2023-10-16

我想创建一个"function proxy"

    一个函数对象。
  1. 它的返回类型和参数类型是自动继承的从给定的"基"函数类型作为模板参数。"基地"函数类型可以是(function pointer/boost::function/boost::bind)
  2. 用给定类型的函数对象初始化。
  3. 当它被调用时(就像你可以调用原始函数一样),它能够将调用存储到boost::bind之类的东西中,并将其传递到其他地方(有意为之,一个线程安全的队列,以便以后可以在另一个队列中调用它),然后返回调用结果。

现在,我的问题是如何(甚至可能)创建这个(functor)类使用模板teq,并传递未知参数列表给绑定。

template<typename R, typename... ARGS>
class Proxy {
  typedef std::function<R(ARGS...)> Function;
  Function f;
 public:
  Proxy(Function _f) : f(_f) {}
  R operator(ARGS... args) {
    std::function<R> bound = std::bind(f, args...);
    send_to_worker_thread(bound);
    wait_for_worker_thread();
    return worker_thread_result();
  }
};
// Because we really want type deduction
template<typename R, typename... ARGS>
Proxy<R,ARGS...>* newProxy(R(*x)(ARGS...)) {
  return new Proxy(std::function<R,ARGS...>(x);
}

我还没有测试过。

你可能想要异步的东西,但是我把它留给你。