RTI Connext Modern C++ API  Version 6.1.0
dds::sub::SharedSamples< T, DELEGATE > Class Template Reference

<<reference-type>> A sharable and container-safe version of LoanedSamples. More...

#include <dds/sub/SharedSamples.hpp>

Public Member Functions

 SharedSamples (dds::sub::LoanedSamples< T > &ls)
 Constructs and instance of SharedSamples removing the ownership of the loan from the LoanedSamples. More...
 
const_iterator begin () const
 Same as LoanedSamples::begin() More...
 
const_iterator end () const
 Same as LoanedSamples::end() More...
 
DELEGATE< T >::value_type operator[] (size_t index) const
 Same as LoanedSamples::operator[](size_t) More...
 
uint32_t length () const
 Returns the number of samples. More...
 

Related Functions

(Note that these are not member functions.)

template<typename T >
void unpack (const dds::sub::SharedSamples< T > &samples, std::vector< std::shared_ptr< const T > > &sample_vector)
 <<extension>> <<C++11>> Unpacks a SharedSamples collection into individual shared_ptr's in a vector More...
 
template<typename T >
std::vector< std::shared_ptr< const T > > unpack (const dds::sub::SharedSamples< T > &samples)
 <<extension>> <<C++11>> Unpacks a SharedSamples collection into individual shared_ptr's in a vector More...
 
template<typename T >
std::vector< std::shared_ptr< const T > > unpack (dds::sub::LoanedSamples< T > &&samples)
 <<extension>> <<C++11>> Unpacks a LoanedSamples collection into individual shared_ptr's in a vector More...
 

Detailed Description

template<typename T, template< typename Q > class DELEGATE = detail::SharedSamples>
class dds::sub::SharedSamples< T, DELEGATE >

<<reference-type>> A sharable and container-safe version of LoanedSamples.

A SharedSamples instance is constructed from a LoanedSamples instance, removing the ownership of the loan from the LoanedSamples.

The destruction of the LoanedSamples object or the explicit invocation of its method return_loan will have no effect on loaned data.

Constructing a SharedSamples from another SharedSamples, creates a new reference to the same loan. Loaned data will be returned automatically to the DataReader once the reference count reaches zero.

See also
LoanedSamples

Constructor & Destructor Documentation

◆ SharedSamples()

template<typename T, template< typename Q > class DELEGATE = detail::SharedSamples>
dds::sub::SharedSamples< T, DELEGATE >::SharedSamples ( dds::sub::LoanedSamples< T > &  ls)
inline

Constructs and instance of SharedSamples removing the ownership of the loan from the LoanedSamples.

The constructor is implicit to simplify statements like the following:

SharedSamples<Foo> samples = reader.take(); // reader.take() returns LoanedSamples
Parameters
lsthe loaned samples.

Member Function Documentation

◆ begin()

template<typename T, template< typename Q > class DELEGATE = detail::SharedSamples>
const_iterator dds::sub::SharedSamples< T, DELEGATE >::begin ( ) const
inline

◆ end()

template<typename T, template< typename Q > class DELEGATE = detail::SharedSamples>
const_iterator dds::sub::SharedSamples< T, DELEGATE >::end ( ) const
inline

◆ operator[]()

template<typename T, template< typename Q > class DELEGATE = detail::SharedSamples>
DELEGATE<T>::value_type dds::sub::SharedSamples< T, DELEGATE >::operator[] ( size_t  index) const
inline

◆ length()

template<typename T, template< typename Q > class DELEGATE = detail::SharedSamples>
uint32_t dds::sub::SharedSamples< T, DELEGATE >::length ( ) const
inline

Returns the number of samples.

See also
LoanedSamples::length()

Referenced by rti::sub::unpack().

Friends And Related Function Documentation

◆ unpack() [1/3]

template<typename T >
void unpack ( const dds::sub::SharedSamples< T > &  samples,
std::vector< std::shared_ptr< const T > > &  sample_vector 
)
related

<<extension>> <<C++11>> Unpacks a SharedSamples collection into individual shared_ptr's in a vector

Note
#include <rti/sub/unpack.hpp>
This is a standalone function in the namespace rti::sub

This function creates a reference (not a copy) to each sample with valid data in a SharedSamples container and pushes it back into a vector.

Each individual sample in the vector retains a reference to the original SharedSamples that controls when the loan is returned. These references can be further shared. When all the references go out of scope, the loan is returned.

This can be also useful to insert samples from different calls to read()/take() into the same vector. It is however recommended to not hold these samples indefinitely, since they use internal resources.

Example:

dds::sub::SharedSamples<Foo> shared_samples = reader.take();
std::vector<std::shared_ptr<const Foo>> sample_vector;
rti::sub::unpack(shared_samples, sample_vector);
std::cout << *sample_vector[0] << std::endl;
// ...
// Read more samples, unpack them at the end of the same vector
shared_samples = reader.take();
rti::sub::unpack(shared_samples, sample_vector);
// References to the samples can be shared freely
std::shared_ptr<const Foo> sample = sample_vector[3];
// ...
// The loans will be returned automatically
Note
To finalize a DataReader, all the shared_ptr obtained via unpack() need to have been released. Otherwise DataReader::close() will fail with dds::core::PreconditionNotMetError.
Template Parameters
TThe topic-type
Parameters
samplesThe collection of samples obtained from the DataReader
sample_vectorThe destination where the samples are pushed back.

◆ unpack() [2/3]

template<typename T >
std::vector< std::shared_ptr< const T > > unpack ( const dds::sub::SharedSamples< T > &  samples)
related

<<extension>> <<C++11>> Unpacks a SharedSamples collection into individual shared_ptr's in a vector

Note
This is a standalone function in the namespace rti::sub

This overload returns a new vector instead of adding into an existing one.

See also
unpack(const dds::sub::SharedSamples<T>&, std::vector<std::shared_ptr<const T> >&)

◆ unpack() [3/3]

template<typename T >
std::vector< std::shared_ptr< const T > > unpack ( dds::sub::LoanedSamples< T > &&  samples)
related

<<extension>> <<C++11>> Unpacks a LoanedSamples collection into individual shared_ptr's in a vector

Note
This is a standalone function in the namespace rti::sub

This overload is a shortcut for unpack(SharedSamples<T>(loaned_samples)) to simplify code like the following:

auto sample_vector = unpack(reader.take());
See also
unpack(const dds::sub::SharedSamples<T>&, std::vector<std::shared_ptr<const T> >&)