RTI Connext Modern C++ API  Version 6.1.0
rti::sub::LoanedSample< T > Class Template Reference

The element type of a dds::sub::LoanedSamples collection. More...

#include <LoanedSample.hpp>

Public Types

typedef T DataType
 The data type. More...
typedef dds::sub::SampleInfo InfoType
 dds::sub::SampleInfo More...

Public Member Functions

const DataTypedata () const
 Gets the data. More...
const InfoTypeinfo () const
 Gets the sample info. More...
 operator const DataType & () const
 Allows implicit conversion to the data type. More...
bool operator== (const LoanedSample &other) const
 Compares the data and info. More...

Related Functions

(Note that these are not member functions.)

template<typename T >
dds::sub::Sample< T > copy_to_sample (const rti::sub::LoanedSample< T > &ls)
 Copies the contents of a rti::sub::LoanedSample into a dds::sub::Sample. More...
template<typename T >
std::ostream & operator<< (std::ostream &out, const LoanedSample< T > &sample)
 Calls the operator on the data or prints [invalid data]. More...

Detailed Description

template<typename T>
class rti::sub::LoanedSample< T >

The element type of a dds::sub::LoanedSamples collection.

This class encapsulates loaned, read-only data and SampleInfo from a DataReader.

LoanedSample instances are always the element of a dds::sub::LoanedSamples collection and have to be returned through that collection. A LoanedSample instance is a lightweight handle to data owned by the DataReader from where you received a LoanedSamples collection.

This type is not exactly a reference type, value type or move-only type. Copying a LoanedSample simply creates a new handle to the same loaned data.

The difference between LoanedSample and dds::sub::Sample is that the latter is a value type that the application owns. A Sample can be constructed by copying the data and meta-data referenced by a LoanedSample.

In most cases the only thing applications care about is that the elements of a dds::sub::LoanedSamples collection have two methods, data() and info(). For example:

for (auto sample : samples) {
if (sample.info().valid()) {
std::cout << sample.data() << std::endl;
See also

Member Typedef Documentation

◆ DataType

template<typename T>
typedef T rti::sub::LoanedSample< T >::DataType

The data type.

◆ InfoType

Member Function Documentation

◆ data()

template<typename T>
const DataType& rti::sub::LoanedSample< T >::data ( ) const

◆ info()

◆ operator const DataType &()

template<typename T>
rti::sub::LoanedSample< T >::operator const DataType & ( ) const

Allows implicit conversion to the data type.

One use of this conversion operator is to simplify the usage of generic algorithms that iterate on a LoanedSamples collection.

For example, the following example copies all the data of in a LoanedSamples collection into a vector of the data type. The call to std::copy works as-is thanks to this conversion.

LoanedSamples<KeyedType> samples = reader.take();
std::vector<KeyedType> data_vector;
std::copy(samples.begin(), samples.end(), std::back_inserter(data_vector));

Note: the example above may throw dds::core::PreconditionNotMetError if any of the samples has invalid data. See valid_data() to iterate only over samples with valid data.

dds::core::PreconditionNotMetErrorif !info().valid().

References rti::sub::LoanedSample< T >::data().

◆ operator==()

template<typename T>
bool rti::sub::LoanedSample< T >::operator== ( const LoanedSample< T > &  other) const

Friends And Related Function Documentation

◆ copy_to_sample()

template<typename T >
dds::sub::Sample< T > copy_to_sample ( const rti::sub::LoanedSample< T > &  ls)

Copies the contents of a rti::sub::LoanedSample into a dds::sub::Sample.


std::vector<Sample<Foo> > sample_vector;

◆ operator<<()

template<typename T >
std::ostream & operator<< ( std::ostream &  out,
const LoanedSample< T > &  sample 

Calls the operator on the data or prints [invalid data].