I'm wrapping an existing library without introducing too much overhead such that the wrapper library could run as fast as the existing one. I'm doing this in order to make the interface(syntax) compatible with my old codes.
Say, the existing class is called BASE, which is templated class. I'm wrapping it as follows.
template<class T>
class Wrapper{
public:
Wrapper() : base(){};
/* ... */
Wrapper<T> dosomething ( const Wrapper<T>& a )
{
Wrapper<T> output;
output.base = CallExistingClass(a.base);
return output;
}
private:
BASE<T> base;
};
For any member function without return type, use this or *this produce very efficient code. However, when a return type of
Wrapper<T>
is required, calling wrapped library is always 5-10 times slower. Since it is simply a wrapper class, all the manipulation I need is extracting member variable "base"(such as "a.base"), do something with a.base using the functions from existing class, then transfer the results to "output.base" and return "output".
I have to make sure the wrapper class match the old syntax of my old code, return by pointer is not an option here. The work-around I can think of is to return-by-reference with static variable.For example, declare this way and return-by-reference
Wrapper<T>& dosomething ( const Wrapper<T>& a)
{
static Wrapper<T> output;
output.base = CallExistingClass(a.base);
return output;
}
I wonder if there is faster way to do this without incurring overhead/temporary? Look forwarding to any useful comments.