Next, we must write methods in the
Instance class
to implement the virtual functions declared in the parent
TypeInstance class.
For the derived class, its constructor must associate a reference
TopicDataType object with the
parent class. The
DataBufferPubSubType class (which was generated by
fastrtpsgen)
is a specialization of
TopicDataType, so a reference to this generated class can be
passed into the constructor, which can assign it to the parent object:
DataBufferType::Instance::Instance( DataBufferPubSubType& type )
: TypeInstance( type, &m_data )
{
m_data.name( "value" );
}
The
initSample() member function is responsible for initializing data samples that will be exchanged
through a publication object. In our implementation, we use the assignment operator defined in the
DataBuffer class (also generated by
fastrtpsgen) to assign the data in the
DataBufferType instance to the sample (which is referenced by a generic pointer):
void DataBufferType::Instance::initSample( void* sample,
const pips_guid_t* guid ) const
{
DataBuffer* buf = ( DataBuffer* )sample;
*buf = m_data;
// TO-DO: Set the key here if using keyed types.
}
An assignment operator is required to assign the data in a sample to a
DataBuffer instance.
This allows subscribers (i.e., data readers) to update type instances with the received sample data:
TypeInstance& DataBufferType::Instance::operator=( const void* sample_data )
{
if ( sample_data ) {
m_data = *( DataBuffer* )sample_data;
}
return *this;
}