There is a class defined in a library:
class fromLib{};
used as argument to invoke a method implemented within the library :
method_from_lib(const fromLib& from_lib){};
I want wrap this method within an interface
class Methods{
virtual invoke_method(const& genericArg)
}
where class genericArg is generic representation of fromLib.
A simple implementation would be:
class MethodImpl: public Methods {
invoke_method(const genericArg& arg) {
method_from_lib(arg); // this is not going to work since the argument is not an instance of `fromLib` nor convertible nor does it inherit from it.
};
}
In an ideal world, I would have made the class fromLib directly inherit from genericArg . However, this is not possible given that this class is from a library that I really dont want to touch.
How would I achieve a wrapper around fromLib which is also an implemetation of genericArg.
The motivation for this is the following:
A library provides some method with the signature method_from_lib(const fromLib& from_lib){}; that we might or might not use. We potentially want to either use that method or some other implementation. If we were to use some other implementation the argument to the function would also need to change since the argument fromLib is tightly coupled to the implementation provided by the library.
Therefore, providing an interface for the method itself is straightforward, however, the purpose of this question is to how we can generalize the argument fromLib into some interface. So that our interface would be like
class Methods{ virtual invoke_method(const& genericArg) }
When we want to implement the case with using the library, the implementation is fairly easy we just invoke the method from the library i.e.
invoke_method(const TYPE& arg) {
method_from_lib(arg);
};
Now as you see implementing this method either requires the TYPE to be genericArg or some class that inherits from it. The problem here is that method_from_lib expects the type fromLib which we cannot directly make as a child from genericArg since that type is contained in a thirdparty library