CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2010
    Posts
    11

    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

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    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


  3. #3
    Join Date
    Apr 2010
    Posts
    11

    Re: choose implementation at link time

    Great! Thanks.

    I've been google-ing about and this indeed seems to be what I need.

  4. #4
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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.
    - Guido

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured