The project that I'm working on could benefit from having the possibility to easily swap between different big number libraries: GMP, OpenSSL, etc. My current implementation defines a template abstract base class in which I implement all the required operators (just to have some syntactic sugar) and I define the required pure virtual functions.
For each library, I have a derived class like this: class BigNumberGmp : public BigNumberBase<BigNumberGmp>
. I know it kinda' breaks OOP, but the C++ covariance functionality is too restrictive and it does not allow me to return BigNumberBase objects from methods, only references, which is quite undesirable...
The problem is that I want the code that uses my custom wrappers to be able to work with any such wrapper: BigNumberGmp, BigNumberOpenSsl, etc. In order to achieve this, I defined typedef BigNumberGmp BigNumber and put it inside some conditional macros, like so:
#if defined(_LIB_GMP)
typedef BigNumberGmp BigNumber;
#endif
Also, I include the appropriate headers in a similar fashion. This implementation requires that I define the _LIB_GMP symbol in the compiler options.
As you can see, this is a rather hackish technique, that I'm not really proud of. Also, it does not hide in any way the specialized classes (BigNumberGmp, BigNumberOpenSsl, etc). I could also define the BigNumber class multiple times, wrapped in _LIB_XXX conditional macros or I could implement the required methods inside the BigNumber class multiple times, for each library, also wrapped in the _LIB_XXX conditional macros. These two latter ideas seem even worse than the typedef implementation and they will surely mess up the doxygen output, since it will not be able to figure out why I have multiple items with the same name. I want to avoid using the doxygen preprocessor, since I still depend on the _LIB_XXX defines...
Is there an elegant design pattern that I could use instead? How would you approach such a problem?