19

如何编写一个可以包装任何函数并且可以像函数本身一样被调用的包装器?

我需要这个的原因:我想要一个 Timer 对象,它可以包装一个函数并像函数本身一样运行,而且它记录所有调用的累积时间。

场景如下所示:

// a function whose runtime should be logged
double foo(int x) {
  // do something that takes some time ...
}

Timer timed_foo(&foo); // timed_foo is a wrapping fct obj
double a = timed_foo(3);
double b = timed_foo(2);
double c = timed_foo(5);
std::cout << "Elapsed: " << timed_foo.GetElapsedTime();

我该如何写这Timer门课?

我正在尝试这样的事情:

#include <tr1/functional>
using std::tr1::function;

template<class Function>
class Timer {

public:

  Timer(Function& fct)
  : fct_(fct) {}

  ??? operator()(???){
    // call the fct_,   
    // measure runtime and add to elapsed_time_
  }

  long GetElapsedTime() { return elapsed_time_; }

private:
  Function& fct_;
  long elapsed_time_;
};

int main(int argc, char** argv){
    typedef function<double(int)> MyFct;
    MyFct fct = &foo;
    Timer<MyFct> timed_foo(fct);
    double a = timed_foo(3);
    double b = timed_foo(2);
    double c = timed_foo(5);
    std::cout << "Elapsed: " << timed_foo.GetElapsedTime();
}

(顺便说一句,我知道gprof还有其他用于分析运行时的工具,但是拥有这样一个Timer对象来记录一些选定函数的运行时对于我的目的来说更方便。)

4

11 回答 11

11

基本上,您想要做的事情在当前的 C++ 中是不可能的。对于要包装的任意数量的函数,您需要通过

const reference
non-const reference

但是它仍然不是完美的转发(一些边缘情况仍然存在),但它应该可以正常工作。如果您将自己限制为 const 引用,则可以使用这个(未经测试):

template<class Function>
class Timer {
    typedef typename boost::function_types
       ::result_type<Function>::type return_type;

public:

  Timer(Function fct)
  : fct_(fct) {}

// macro generating one overload
#define FN(Z, N, D) \
  BOOST_PP_EXPR_IF(N, template<BOOST_PP_ENUM_PARAMS(N, typename T)>) \
  return_type operator()(BOOST_PP_ENUM_BINARY_PARAMS(N, T, const& t)) { \
      /* some stuff here */ \
      fct_(ENUM_PARAMS(N, t)); \
  }

// generate overloads for up to 10 parameters
BOOST_PP_REPEAT(10, FN, ~)
#undef FN

  long GetElapsedTime() { return elapsed_time_; }

private:
  // void() -> void(*)()
  typename boost::decay<Function>::type fct_;
  long elapsed_time_;
};

请注意,对于返回类型,您可以使用 boost 的函数类型库。然后

Timer<void(int)> t(&foo);
t(10);

您还可以使用纯值参数重载,然后如果您想通过引用传递某些内容,请使用boost::ref. 这实际上是一种非常常见的技术,尤其是在要保存此类参数时(此技术也用于boost::bind):

// if you want to have reference parameters:
void bar(int &i) { i = 10; }

Timer<void(int&)> f(&bar);
int a; 
f(boost::ref(a)); 
assert(a == 10);

或者,您可以如上所述为 const 和非 const 版本添加这些重载。查看Boost.Preprocessor以了解如何编写正确的宏。

您应该意识到,如果您希望能够传递任意可调用对象(不仅仅是函数),整个事情将会变得更加困难,因为您需要一种方法来获取它们的结果类型(这并不那么容易)。C++1x 将使这种事情变得更容易。

于 2009-05-18T20:29:05.820 回答
10

这是包装函数的简单方法。

template<typename T>
class Functor {
  T f;
public:
  Functor(T t){
      f = t;
  }
  T& operator()(){
    return f;
  }
};


int add(int a, int b)
{
  return a+b;
}

void testing()
{
  Functor<int (*)(int, int)> f(add);
  cout << f()(2,3);
}
于 2009-05-18T20:13:36.833 回答
7

我假设您出于测试目的需要它,并且不会将它们用作真正的代理或装饰器。所以你不需要使用 operator() 并且可以使用任何其他更不方便的调用方法。

template <typename TFunction>
class TimerWrapper
{
public:
    TimerWrapper(TFunction function, clock_t& elapsedTime):
        call(function),
        startTime_(::clock()),
        elapsedTime_(elapsedTime)
    {
    }

    ~TimerWrapper()
    {
        const clock_t endTime_ = ::clock();
        const clock_t diff = (endTime_ - startTime_);
        elapsedTime_ += diff;
    }

    TFunction call;
private:
    const clock_t startTime_;
    clock_t& elapsedTime_;
};


template <typename TFunction>
TimerWrapper<TFunction> test_time(TFunction function, clock_t& elapsedTime)
{
    return TimerWrapper<TFunction>(function, elapsedTime);
}

所以要测试你的一些函数,你应该只使用test_time函数而不是直接TimerWrapper结构

int test1()
{
    std::cout << "test1\n";
    return 0;
}

void test2(int parameter)
{
    std::cout << "test2 with parameter " << parameter << "\n";
}

int main()
{
    clock_t elapsedTime = 0;
    test_time(test1, elapsedTime).call();
    test_time(test2, elapsedTime).call(20);
    double result = test_time(sqrt, elapsedTime).call(9.0);

    std::cout << "result = " << result << std::endl;
    std::cout << elapsedTime << std::endl;

    return 0;
}
于 2009-05-18T21:07:55.943 回答
5

如果您查看包含的 std::tr1::function 的实现,您可能会找到答案。

在 c++11 中,std:: 函数是用可变参数模板实现的。使用此类模板,您的计时器类可能看起来像

template<typename>
class Timer;

template<typename R, typename... T>
class Timer<R(T...)>
{
    typedef R (*function_type)(T...);

    function_type function;
public:
    Timer(function_type f)
    {
        function = f;
    }

    R operator() (T&&... a)
    {
        // timer starts here
        R r = function(std::forward<T>(a)...);
        // timer ends here
        return r;
    }
};

float some_function(int x, double y)
{
    return static_cast<float>( static_cast<double>(x) * y );
}


Timer<float(int,double)> timed_function(some_function); // create a timed function

float r = timed_function(3,6.0); // call the timed function
于 2013-05-29T02:31:16.927 回答
3

Stroustrup 展示了一种函数包装器(注入)技能,可重载operator->. 关键思想是:operator->会重复调用,直到遇到原生指针类型,所以让我们Timer::operator->返回一个临时对象,临时对象返回它的指针。然后会发生以下情况:

  1. 创建了临时 obj(称为 ctor)。
  2. 调用的目标函数。
  3. temp obj 已破坏(调用 dtor)。

您可以在 ctor 和 dtor 中注入任何代码。像这样。

template < class F >
class Holder {
public:
    Holder  (F v) : f(v) { std::cout << "Start!" << std::endl ; }
    ~Holder ()           { std::cout << "Stop!"  << std::endl ; }
    Holder* operator->() { return this ; }
    F f ;
} ;

template < class F >
class Timer {
public:
    Timer ( F v ) : f(v) {}
    Holder<F> operator->() { Holder<F> h(f) ; return h ; }
    F f ;
} ;

int foo ( int a, int b ) { std::cout << "foo()" << std::endl ; }

int main ()
{
    Timer<int(*)(int,int)> timer(foo) ;
    timer->f(1,2) ;
}

实施和使用都很容易。

于 2009-05-19T05:10:32.373 回答
2

使用宏和模板的解决方案:例如你想换行

double foo( double i ) { printf("foo %f\n",i); return i; }
double r = WRAP( foo( 10.1 ) );

在调用 foo() 之前和之后,应该调用包装函数 beginWrap() 和 endWrap()。(endWrap() 是一个模板函数。)

void beginWrap() { printf("beginWrap()\n"); }
template <class T> T endWrap(const T& t) { printf("endWrap()\n"); return t; }

#define WRAP(f) endWrap( (beginWrap(), f) );

使用逗号运算符的优先级来确保首先调用 beginWrap()。f 的结果被传递给 endWrap() ,它只是返回它。所以输出是:

beginWrap()
foo 10.100000
endWrap()

结果 r 包含 10.1。

于 2017-01-31T14:18:34.060 回答
1

我不太清楚你在看什么。但是,对于给定的例子,它很简单:

void operator() (int x)
{
   clock_t start_time = ::clock();    // time before calling
   fct_(x);                           // call function
   clock_t end_time = ::clock();      // time when done

   elapsed_time_ += (end_time - start_time) / CLOCKS_PER_SEC;
}

注意:这将以秒为单位测量时间。如果您想拥有高精度计时器,您可能必须检查操作系统的特定功能(如Windows 上的GetTickCountQueryPerformanceCounter)。

如果你想拥有一个通用的函数包装器,你应该看看Boost.Bind这将有很大帮助。

于 2009-05-18T19:51:53.867 回答
1

如果您希望创建一个可以包装和调用任意函数的泛型类,那么您将面临巨大的挑战。在这种情况下,您必须使仿函数(operator())返回 double 并将 int 作为参数。然后,您创建了一系列可以调用具有相同签名的所有函数的类。只要您想添加更多类型的函数,就需要更多该签名的函子,例如

MyClass goo(double a, double b)
{
   // ..
}

template<class Function>
class Timer {

public:

  Timer(Function& fct)
  : fct_(fct) {}

  MyClass operator()(double a, double b){

  }

};

编辑:一些拼写错误

于 2009-05-18T19:53:46.840 回答
1

如果您的编译器支持可变参数宏,我会试试这个:

class Timer {
  Timer();// when created notes start time
  ~ Timer();// when destroyed notes end time, computes elapsed time 
}

#define TIME_MACRO(fn, ...) { Timer t; fn(_VA_ARGS_); } 

所以,要使用它,你可以这样做:

void test_me(int a, float b);

TIME_MACRO(test_me(a,b));

这是即兴的,您需要尝试让返回类型起作用(我认为您必须在 TIME_MACRO 调用中添加一个类型名称,然后让它生成一个临时变量)。

于 2009-05-18T20:52:59.657 回答
0

在 C++ 中,函数是一等公民,您可以从字面上将函数作为值传递。

由于您希望它采用 int 并返回双精度:

Timer(double (*pt2Function)(int input)) {...
于 2009-05-18T19:52:36.517 回答
0

这是我的做法,使用函数指针而不是模板:

// pointer to a function of the form:   double foo(int x);
typedef double  (*MyFunc) (int);


// your function
double foo (int x) {
  // do something
  return 1.5 * x;
}


class Timer {
 public:

  Timer (MyFunc ptr)
    : m_ptr (ptr)
  { }

  double operator() (int x) {
    return m_ptr (x);
  }

 private:
  MyFunc m_ptr;
};

我将其更改为不引用函数,而只是一个普通的函数指针。用法保持不变:

  Timer t(&foo);
  // call function directly
  foo(i);
  // call it through the wrapper
  t(i);
于 2009-05-18T20:30:08.500 回答