I'm developing a new version of a CLI utility which generates accessors and would like to add a decorate feature.
In order to implement that, I would like to know what is the best way to implement a decorator in C++ and eventually C++11.
For example, with such an interface:
class IHello
{
public:
virtual void hello(std::string name) = 0;
};
I have two possibility, either I copy the parameter name
again to pass it to the object, or I created an rvalue reference with std::move
semantic.
I thus have two different decorator. The first passing arguments by copy:
class HelloCopy : public IHello
{
public:
HelloCopy(IHello& instance)
:instance (instance)
{
}
virtual void hello(std::string name) override
{
this->instance.hello(name);
}
private:
IHello& instance;
};
The second passing argument by rvalue-reference:
class HelloRValue : public IHello
{
public:
HelloRValue(IHello& instance)
:instance (instance)
{
}
virtual void hello(std::string name) override
{
this->instance.hello(std::move(name));
}
private:
IHello& instance;
};
My question is: What is the best (most efficient) way to implement a decorator?
I could also make the decorated method's argument and rvalue reference, but as I want to comply with the interface (hence the explicit override), I can't change it.