简体   繁体   中英

Pre/Post function call implementation

I was wondering if I could do pre/post function call in C++ somehow. I have a wrapper class with lots of functions, and after every wrapper function call I should call another always the same function.

So I do not want to put that postFunction() call to every single one of the functions like this:

class Foo {
    f1();
    f2();
    f3();
    .
    .
    .
    fn();
}

void Foo::f1() {
    ::f1();
    postFunction();
}

void Foo::f2() {
    ::f2();
    postFunction();
}

etc.

Instead I want that postFunction call to come automatically when I call some Foo's member function. Is it possible? It would help maintenance..

Might be a case for RAII ! Dun-dun-dunnn!

struct f1 {
  f1(Foo& foo) : foo(foo) {} // pre-function, if you need it
  void operator()(){} // main function
  ~f1() {} // post-function

private:
  Foo& foo;
}

Then you just have to make sure to create a new temporary f1 object every time you wish to call the function. Reusing it will obviously mean the pre/post functions don't get called every time.

Could even wrap it like this:

void call_f1(Foo& foo) {
  f1(foo)(); // calls constructor (pre), operator() (the function itself) and destructor (post)
}

You might experiment a bit with other ways of structuring it, but in general, see if you can't get constructors/destructors to do the heavy lifting for you.

Roman M's approach might be a good idea as well. Write one generic wrapper, which takes a functor or function pointer as its argument. That way, it can call pre/post functions before and after calling its argument

Implement a wrapper that will take a function pointer as an argument, and will call pre\\post function before the function call.

Foo::caller ( function* fp ) {
   preCall ();
   fp (...);
   postCall ();

}

听起来像是使用AspectC ++进行面向方面编程的工作

As Richie pointed out, you're likely looking for Aspect Oriented Programming.

If that doesn't work for you though, another option you have is writing yet another wrapper class. Say instead of Foo, you have an interface named IFoo. You can then write a wrapper class to ensure the proper pre and post calls are used

class IFoo { 
public:
  void Bar();  
}

class FooWrapper: public IFoo {
public:
  FooWrapper( IFoo* pFoo ) {
    m_pFoo = pFoo;
  }
  void Bar() {
    PreBar();
    m_pFoo->Bar();
    PostBar();
  }
}

I think the answer has been given by the father of C++ B.Stroustrup.

Wrapping C++ Member Function Calls

This paper presents a simple, general, and efficient solution to the old problem of ''wrapping'' calls to an object in pairs of prefix and suffix code. The solution is also non-intrusive, applies to existing classes, allows the use of several prefix/suffix pairs, and can be implemented in 15 simple lines of Standard C++. A robust version of the wrapper is also presented. The claim of efficiency is backed by measurement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM