Let's say I have an interface and a class implementing it, like this:
class IPrinter
{
public:
virtual void print(int i, int base = 10) = 0;
virtual void print(char c) = 0;
virtual void print(char *s) = 0;
virtual void print(float f) = 0;
virtual void print(double d) = 0;
virtual ~IPrinter() = default;
private:
...
}
class Printer : public IPrinter
{
public:
void print(int i, int base = 10) override {...}
void print(char c) override {...}
void print(char *s) override {...}
void print(float f) override {...}
void print(double d) override {...}
private:
...
}
And then I decide to add a simple decorator class, like this:
class SwitchablePrinter : public IPrinter
{
private:
IPrinter& _printer;
bool _enabled;
...
public:
SwitchablePrinter(IPrinter& p) :
_printer(p),
_enabled(true)
{
}
void print_enable(bool on) { _enabled = on; }
void print(int i, int base = 10) override
{
if (_enabled)
_printer.print(i, base);
}
void print(char c) override
{
if (_enabled)
_printer.print(c);
}
void print(char *s) override
{
if (_enabled)
_printer.print(s);
}
void print(float f) override
{
if (_enabled)
_printer.print(f);
}
void print(double d) override
{
if (_enabled)
_printer.print(d);
}
}
Now, all this is pretty simple and straightforward. The problem is that there is a lot of code duplication in the SwitchablePrinter implementation. I was wondering, that if there was a way to write a generic method 'print' for all overloaded methods in the base class, like this:
(pseudo-code)
void print({any args})
{
if (_enabled)
_printer.print({any args});
}
I think there may be a solution using templates, but I am not very experienced with using them and need advice.