choose implementation at link time
Hi,
I've got a pretty generic problem.
Suppose that there's some functionality that can be implemented in several independent ways, for example you need a database and there are several technologies to implement it (MySQL, SQLite, etc.). The different implementations share a common interface though, for example a function to delete or update a database record.
Now, you'd like to make your source code as unaware as possible from the implementation you choose. The easy way to do this is to create an interface (in C++ terms, an abstract class with pure virtual functions) and for each implementation have a class that implements the interface, and to have one single point where you decide which kind of object to use. The Factory design pattern is applicable here.
However, I'm looking for a way to do this at link time: have different implementations, for example provided as libraries, among which you choose one to link with the rest of the application. The source code of "the rest of the application" would be unaware of exactly which implementation has been chosen.
Does anyone have any ideas on how to do this?
If anything is unclear in my explanation above, please let me know.
Cheers
Re: choose implementation at link time
Easy... well, in depth, but easy. Use dynamic libraries, but instead of linking to them, load them at runtime. Create a library for all of your sql implementations and then load the ones you need at runtime.
Code:
void * library;
func delete;
func insert;
switch (dbType){
case SQL:
library = dlopen("sql.so", RTLD_LAZY);
break;
case SQL_LITE:
library = dlopen("sqp_lite.so", RTLD_LAZY);
break;
default:
library = dlopen("mysql.so", RTLD_LAZY);
break;
}
delete = (func)dlsym(library, "delete");
insert = (func)dlsym(library, "insert");
Loading libraries at runtime isn't something most programmers (at least the ones I know,) do, but it's a very efficient way of doing things and I do it all the time.
dlopen, dlsym, and dlclose are for *nix, on Windows the functions that you want are LoadLibrary, GetPrcoAddress, and CloseLibrary
:)
Re: choose implementation at link time
Great! Thanks.
I've been google-ing about and this indeed seems to be what I need.
Re: choose implementation at link time
The only solution I can think of are preprocessor conditionals (#ifdef/#else/#elif/#endif), though I seriously don“t recommend using them.
Re: choose implementation at link time
Another way could be to create several targets, each one linking to it's specific lib.
Or even easier (depending on lib code size), add all lib source files to the project, mark them as not part of any build then re-select the files in a specific project and revert them to being built (repeat for all "libs"). This way you're guranteed that all source is built with the same settings and your libs can also easily share common parts of the code.
The drawback is that you end up with an exe for each specific target but that's not any worse than having a dll for each specific target.