Executing specific code for each method call in C ++

I have a C ++ class that I want to test. Therefore, I would like all methods to print their parameters and return just before exiting.

The latter looks somewhat light. If I believe () for everything, a macro

#define return(a) cout << (a) << endl; return (a) 

would do this (maybe erroneously) if I put all returns in brackets (or whatever that is called). If I want to do this, just comment out the definition.

However, print entries seem more difficult. Is there a way I can do this using C ++ structures or with a hand gown?

+7
c ++ methods
source share
6 answers

However, print inputs look more complicated. Is there a way I can do this using C ++ structures or using workaroud hack?

Not.


Update: I am going to lose some patience in my answer, suggesting you possibly achieve what you need by applying Design by Contract .

+4
source share

Several options come to mind:

  • Use a debugger.
  • Use the decorator pattern as suggested by Space_C0wb0y. However, this can be a lot of manual input, as you will have to duplicate all the methods in the decorated class and add the entry yourself. Perhaps you could automate the creation of a decorator object by running doxygen for your class and then analyzing its output ...
  • Use aspect-oriented programming . (Logging is what you want to do, this is a common use of AOP.) Wikipedia lists several AOP implementations for C ++: Aspect C ++ , XWeaver, and Feature C ++ .
+8
source share

It looks like you want to use a debugging utility. This will allow you to see all the necessary parameters.

+2
source share

If you don't mind inserting any code manually, you can create a class that:

  • writes a record to a method in the constructor
  • provides a method to reset arbitrary parameters
  • provides a way to record status
  • Log output with recorded status in the destructor

Usage will look something like this:

 unsigned long long factorial(unsigned long long n) { Inspector inspect("factorial", __FILE__, __LINE__); inspect.parameter("n", n); if (n < 2) { return inspect.result(1); } return inspect.result(n * fact(n-1)); } 

Of course, you can write macros to declare the inspector and check the parameters. If you work with a compiler that supports macros of variable variables, then you can get a result similar to:

 unsigned long long factorial(unsigned long long n) { INJECT_INSPECTOR(n); if (n < 2) { return INSPECT_RETURN(1); } return INSPECT_RETURN(n * fact(n-1)); } 

I'm not sure that you can get a cleaner solution without resorting to the AOP environment or some special code generation tool.

+2
source share

If your methods are all virtual , you can use a decorator template to achieve this in a very elegant way.

EDIT: From your comment above (you want to get statistics for statistics) I conclude that you should definitely use the decorator template. It is designed for this kind of thing.

+1
source share

I would just use the logging library (or some macros) and insert manual logging calls. If your class does not have an excessive number of methods, it probably develops faster than it develops and debugs more complex solutions.

0
source share

All Articles