RTI Connext Modern C++ API  Version 5.3.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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.
 
typedef dds::sub::SampleInfo InfoType
 dds::sub::SampleInfo
 

Public Member Functions

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

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.
 
template<typename T >
std::ostream & operator<< (std::ostream &out, const LoanedSample< T > &sample)
 Calls the operator on the data or prints [invalid data].
 

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
dds::sub::LoanedSamples

Member Typedef Documentation

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

The data type.

Member Function Documentation

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

Gets the data.

Exceptions
dds::core::PreconditionNotMetErrorif !info().valid().
template<typename T >
const InfoType& rti::sub::LoanedSample< T >::info ( ) const
inline

Gets the sample info.

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

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_samples() to skip invalid samples in an iterator range.

Exceptions
dds::core::PreconditionNotMetErrorif !info().valid().
template<typename T >
bool rti::sub::LoanedSample< T >::operator== ( const LoanedSample< T > &  other) const
inline

Compares the data and info.

Friends And Related Function Documentation

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

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

Example:

std::vector<Sample<Foo> > sample_vector;
std::transform(
std::back_inserter(sample_vector),
rti::sub::copy_to_sample<Foo>);
template<typename T >
std::ostream & operator<< ( std::ostream &  out,
const LoanedSample< T > &  sample 
)
related

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


RTI Connext Modern C++ API Version 5.3.0 Copyright © Sun Jun 25 2017 Real-Time Innovations, Inc