jeudi 2 mars 2017

How to wrap a C++ library

I'm developing an application which is using a library and I would like to wrap this library so that it does not goes to deep into my application code. Thanks to that I could change the library I'm using just by re-implementing my wrapper classes.

Suppose that I have a library LibA. It gives me 2 objects to work with, LibAObj1 and LibAObj2. LibAObj2 has a method using LibAObj1.

Here can be a simple definition of their declaration

class LibAObj1 {};

class LibAObj2 
{
     void action(LibAObj1 &obj);
};

Now I would like to define an interface that my application can use to wrap those objects in my application code

For instance:

class ItfLibAObj1 {};

class ItfLibAObj2 
{
public:
     void action(ItfLibAObj1 &obj) = 0;
};

The problem comes whenever I want to implement my interface ItfLibAObj2.

class ImplLibAObj2 : public ItfLibAObj2
{
public:
    void action(ItfLibAObj1 &obj)
    {
        <how to get my LibAObj1>?
        obj.action(LibAObj1);
    }
private:
    LibObj2 obj;       
}

The question is actually in the pseudo code. How to get my LibAObj1 contained in my ItfLibAObj1 reference? I could add a getter function in LibAObj1 interface to return a void pointer that I would cast but I don't find that elegant in C++.

Is there any kind of design pattern I could use to solve my problem? Or do I just have a design issue?

Note that I'm not wishing to select which library to use at run time.

Thanks a lot for your help.

Kind regards

Aucun commentaire:

Enregistrer un commentaire