I try to reduce code duplication in a class which implements the pimpl idiom.
Imagine I have a header Foo.h
. For better readability, I have reduced the methods.
class FooImp;
class Foo
{
public:
void A(int x);
void B(int x, double b);
private:
FooImp* _imp;
};
Then I have an implementation file like the following:
class FooImp
{
public:
void A(int x) {}
void B(int x, double b) {}
};
class Logger{};
class Measure{};
void Foo::A(int x)
{
Measure m;
_imp->A(x);
Logger log;
}
void Foo::B(int x, double b)
{
Measure m;
_imp->B(x, b);
Logger log;
}
So around the function call, I measure something, log something and so on... The construct is always the same. I would like to have a macro that reduces the code overhead here if possible. I imagine something like this:
#define PIMPL_FUNCTION(x, ...) \
void Foo::x \
{ \
Measure m; \
ptr->x(...); \
Logger log; \
} \
PIMPL_FUNCTION(A(int x), x);
PIMPL_FUNCTION(B(int x, double b), x, b)
Of course, this code snippet does not compile. I am unsure if what I am trying is even possible with macros. I am open to any ideas or suggestions.
A little background. All these methods are going to be API-calls for a library and the number of methods is going to increase.