The files testInter.h and testInter.cxx contain definitions for a new class called TestInteraction, and its member functions.
Like all interaction classes in VR-Link, it is derived from DtInteractionWithEncDec, rather than directly from DtInteraction. DtInteraction is rather general, and allows for a wide variety of subclass implementations, including those that do not involve our concept of encoders and decoders. DtInteractionWithEncDec is set up to deal with encoders and decoders, and we will be taking advantage of this.
TestInteraction includes inspector and mutator functions to access the values of the Number and Parameter attributes. We represent them in the same way as in the FOM, but this is not strictly necessary. We could perform a non-trivial conversion in FOM mapping code instead.
The TestInteraction class hard-codes its own FOM mapping information, rather than relying on the default behavior, which is to obtain it from the FOM Mapper. We do this here, because it make things simpler, by saving us the step of configuring the FOM Mapper with mappings for our new class. But this choice means that it would be more difficult to change the way this kind of interaction is represented in the FOM.
This choice manifests itself in the fact that we override the virtual functions interactionClassToUse(), createEncoder() and createDecoder(). The base class implementation of interactionClassToUse() asks the FOM Mapper for the name of a FOM class to use. But TestInteraction's implementation just returns the class name Test. The base versions of createEncoder() and createDecoder() ask the FOM Mapper for instances of the kind of encoder and decoder that have been registered with it for use with the Test interaction class. But TestInteraction's implementations of these functions just return a new'ed TestEncoder and TestDecoder instance respectively.
In addition, within TestInteraction::addCallback(), we inform the DtExerciseConn's interaction factory that it should create an instance of the TestInteraction class to represent incoming interactions of FOM class Test. This is achieved using DtInteractionFactory's addCreator() member function.
Notice that the TestInteraction has decided that it will use instances of the classes TestEncoder and TestDecoder to map between its representation of its parameters, and the FOM representation.
#ifndef TestInteraction_H_
#define TestInteraction_H_
#if DtHLA
class TestInteraction;
typedef void (*TestInteractionCb)(TestInteraction* inter, void* usr);
class TestInteraction : public DtInteractionWithEncDec
{
public:
TestInteraction();
virtual ~TestInteraction();
TestInteraction(const TestInteraction& orig);
TestInteraction& operator=(const TestInteraction& orig);
virtual const char* name() const;
virtual void printData() const;
virtual void setNumber(int num);
virtual int number() const;
virtual void setVector(
const DtVector& vec);
public:
TestInteractionCb cb, void* usr);
TestInteractionCb cb, void* usr);
protected:
virtual DtInteractionDecoder* createDecoder(
DtExerciseConn* exConn)
const;
virtual DtInteractionEncoder* createEncoder(
DtExerciseConn* exConn)
const;
protected:
int myNum;
};
#endif
#endif
#if DtHLA
#include "testEnc.h"
#include "testDec.h"
#include <iostream>
TestInteraction::TestInteraction() :
DtInteractionWithEncDec(),
myNum(0),
myVec()
{
}
TestInteraction::~TestInteraction()
{
}
TestInteraction::TestInteraction(const TestInteraction& orig) :
DtInteractionWithEncDec(orig)
{
myNum = orig.number();
myVec = orig.vector();
}
TestInteraction& TestInteraction::operator=(const TestInteraction& orig)
{
if (this == &orig)
{
return *this;
}
DtInteractionWithEncDec::operator=(orig);
myNum = orig.number();
myVec = orig.vector();
return *this;
}
const char* TestInteraction::name() const
{
return "TestInteraction";
}
void TestInteraction::printData() const
{
std::cout << "Number: " << number() << '\n';
std::cout << "Vector: " << vector().string() << std::endl;
}
void TestInteraction::setNumber(int num)
{
myNum = num;
}
int TestInteraction::number() const
{
return myNum;
}
void TestInteraction::setVector(
const DtVector& vec)
{
myVec = vec;
}
const DtVector& TestInteraction::vector()
const
{
return myVec;
}
{
return new TestInteraction();
}
TestInteractionCb cb, void* usr)
{
conn->addInteractionCallbackByName(
"Test", (DtReceiveInteractionCb) cb, usr);
conn->interactionFactory()->addCreator("Test",
TestInteraction::create);
}
TestInteractionCb cb, void* usr)
{
conn->removeInteractionCallbackByName(
"Test", (DtReceiveInteractionCb) cb, usr);
}
char* TestInteraction::interactionClassToUse(
DtExerciseConn* exConn)
const
{
return "Test";
}
DtInteractionDecoder* TestInteraction::createDecoder(
{
return new TestDecoder(exConn, classDesc());
}
DtInteractionEncoder* TestInteraction::createEncoder(
{
return new TestEncoder(exConn, classDesc());
}
#endif