Greg
Greg

Reputation: 2229

Run Code Before Every Function Call for a Class in C++

I would like to run some code (perhaps a function) right before every function call for a class and all functions of the classes that inherit from that class. I'd like to do this without actually editing every function, Is such a thing even possible?

I would settle for having a function called as the first instruction of every function call instead of it being called right before.

Upvotes: 10

Views: 4826

Answers (9)

ovenror
ovenror

Reputation: 552

Using g++, you could use the option -pg for the respective compilation units, which makes the compiler generate a call to the function mcount at the start of every function. mcount is usually provided by profiling tools like gprof, but you can also implement it yourself. You should however make sure that

  • mcount has C linkage (and is not C++-style name-mangled), i.e. by implementing it as a C function and compiling with a pure C compiler like gcc.
  • the compilation unit containing mcount is not compiled with -pg.

Upvotes: 0

oz10
oz10

Reputation: 158464

You could also do this with the Curiously recurring template pattern (CRTP).

Upvotes: 0

ossandcad
ossandcad

Reputation: 540

The following might be a bit of an overkill - but how about?

http://msdn.microsoft.com/en-us/library/c63a9b7h.aspx

Upvotes: 3

Nick
Nick

Reputation: 6846

Another thing you could consider is using something like the [boost/C++0X] shared_ptr wrapper, where you call your custom function on the '->' overload before returning the class instance pointer. It involves modifying usage but not the underlying class, and I've used it a couple times to achieve the same effect. Just another thought.

Upvotes: 2

Raymond Martineau
Raymond Martineau

Reputation: 6039

The best you can do is to declare a set of virtual functions as protected and have the developers inheriting from the class override the virtual functions. The interface used by the base class can be public, which executes the desired code before passing information to the protected virtual method.

For example:

class Base {
  public:
    void MyMethod(void) { /* Insert code here */ YourMethod(); }
  protected:
    virtual void YourMethod(void) {}
};

If the developer knows that he has a specific subclass, he can still bypass your code simply by using a dynamic_cast, and using his own method set. As such, you may want to follow the other suggestions already posted that do not involve the base C++ language.

Upvotes: 1

ovanes
ovanes

Reputation: 5673

I would suggest using the Non Virtual Interface idiom. All public functions are non-virtual. All virtual functions are protected or private. Public members delegate the calls to virtual members and are usually implemented as inline functions.

This is the way IOStreams are implemented in STL. You can read more about it at C++ Wikibooks.

Intent: To modularize/refactor common before and after code fragments (e.g., invariant checking, acquiring/releasing locks) for an entire class hierarchy at one location.

Regards,
Ovanes

Upvotes: 3

Jay
Jay

Reputation: 14471

This sounds like what a profiler does. Have you looked at the source for any profiling tools?

Upvotes: 0

Janusz
Janusz

Reputation: 189554

The somewhat inconvenient way where to build a wrapper class that takes an object of your base type and calls the surrounding function and then the function that you wanted to call. This would be something like a decorator.

Upvotes: 1

notnoop
notnoop

Reputation: 59307

AspectC++ is what you want. I haven't used it myself, but Aspect-Oriented Programming paradigm tries to solve this exact problem.

Upvotes: 9

Related Questions