I have a class called ToolConfigurationVector and a class called JointVariableVector.
I need to be able to convert between those two classes (they are different representations of the same data)
A ToolConfigurationVector consists only of 6 primitive Datatypes. This is always the case.
A JointVariableVector is a representation that is specific to a Robotarm, represented by the class Arm.
So for one subclass of Arm (say SimpleArm) the representation and thus the conversion to ToolConfigurationVector is completely different to representation and conversion of another subclass of Arm (say ComplexArm)
Later, I want to use those classes like this:
Arm * someArm = new ComplexArm();
// ...
JointVariableVector jvv = someArm.currentJointVariableVector();
ToolConfigurationVector tcv = jvv.toToolConfigurationVector(); // no problem using virtual function
JointVariableVector how = tcv.toJointVariableVector(); // how?
I don’t want to include any information about which subclass of Arm is in use into ToolConfigurationVector, the information should be stored somehow in JointVariableVector
Of course, I could use templates like this:
JointVariableVector<ComplexArm> how = ...
But this is not applicable in my case, I have to solve this using ONLY
someArmtcvToolConfigurationVectorJointVariableVector
I could of course give Arm a virtual method like jointVariableVectorFromToolConfigurationVector(ToolConfigurationVector v) but I cannot make it static, and because it has nothing to do with the actual Arm object implementing it as method (without static) seems wrong to me.
What can I do to get an elegant solution? Of course, this solution should be efficient, too (in terms of needed classes, objects and execution time).
From
jvv.toToolConfigurationVector(); // no problem using virtual functionit indicates thatJointVariableVectorholds a pointer or reference to anArmsubclass instance, or it is using something like thepImplpattern, in order to engage in behavior virtual in theArmsubclass that it was created from.If you want
ToolConfigurationVector::toJointVariableVectorto create a class which is virtual in what subclass ofArmit is specific to, either (A)ToolConfigurationVectormust be virtual in which subclass ofArmtheJointVariableVectorwill be virtual in that it produces, or (B)ToolConfigurationVector::toJointVariableVectorneeds to be supplied anArmto create theJointVariableVectorwhich will be virtual in theArmsubclass. Ie, pass anArmtotoJointVariableVector.Alternatively, the
JointVariableVectorcould be some kind of virgin object which later needs to be blessed withArmcontext before it makes sense, but that makes a mess.So, to be clear,
JointVariableVectoreither has a pointer to anArm(because its representation is a function of saidArm), or is apImplpattern class, based off your syntax (where instances ofJointVariableVector, and not pointers to same, invoke virtual behavior). How saidJointVariableVectoris built is a function of theArmit comes from. TheToolConfigurationVectorloses that information. To rebuild aJointVariableVectoryou must then inject theArmback into the situation.If
JointVariableVectoris only a function of the type of theArm(and not on the instance), then you can maketoJointVariableVectora template function on the type ofArmit is creating aJointVariableVectorfor. To reduceToolConfigurationVectordependency onArmsubclasses, haveToolConfigurationVectordispatch the call to a free template function on the type ofArm, which you can then implement adjacent to eachArmsubclass and change theJointVariableVectorproduced.Which would give you the syntax: