0

Sorry if the title is too generic but I do not know how to phrase it differently. Here is my issue:

The goal is to have wrapper classes in order to use different libraries with the same generic API. I have an abstract class which define all kind of computations possible. Then, I have several child class which implement this computations using different library (one class only use one specific library).

There is one argument given to a lot of this classes methods which is an Eigen::VectorXd, now for one of the library this vector need to be arranged differently than with the others.

So basically for one of the child class I will need to write something like this in a lot of methods:

void ChildB::computeXXXX(const Eigen::VectorXd &A, unsigned int b,const Eigen::Vector3d &c)
{
    const Eigen::VectorXd A_formatB = convertToChildB(A);
    libraryB::computeXXX(A_formatB, b, c)
    // do stuff
    //....
}

It also get a little tricky when this argument is passed to other methods of the class from inside the class, I need to take care of always using the good format. As you can see, my naive solution could lead to a lot of mistakes and headaches. So my question is: is there a smart/automatic way to do this ? Like for all call to libraryB::something I want the argument named "A" (it is always the same name) to be re arranged before.

Bernad D.
  • 3
  • 2

1 Answers1

0

You write the code, you keep the format and layout of data. So it's responsibility of your code. But you can make it less error prone by using e.g. decorator pattern.

You put the conversion code into decorator. It allows keep infrastructure service code away from main logic. The classes that uses different libraries under the hood accept data in it's specific format. For client code you decorate the certain object with certain decorator.

You can add factory method to interface to create specific decorator in polymorphic way associated with certain type of objects.

4xy
  • 3,494
  • 2
  • 20
  • 35