template<typename T>
class dds::sub::LoanedSamples< T >
<<move-only-type>> Provides temporary access to a collection of samples (data and info) from a DataReader.
- Template Parameters
-
T | The topic-type. It has to match the type of the DataReader. |
This STL-like container encapsulates a collection of loaned, read-only data samples (data and info) from a DataReader.
To obtain a LoanedSamples you need to call one of the read/take operations from a DataReader. The samples have to be eventually returned to the DataReader. The destructor takes care of that, and the return_loan() function lets you do it explicitly if needed.
As a move-only type copying a LoanedSamples is not allowed. If you want to have more than one reference to a collection of loaned sample, see SharedSamples. If you need to return a LoanedSamples from a function or assign it to another variable, use dds::core::move()
(or std::move()
<<C++11>>).
Iterators and overloaded subscript operators let you access the samples in this container, which are of the type rti::sub::LoanedSample.
This code demonstrates how to access the info and data of each sample in a DataReader:
auto samples = reader.
take();
for (const auto& sample : samples) {
if (sample.info().valid()) {
std::cout << sample.data() << std::endl;
}
}
- See also
- Reading data samples for more examples
- Examples:
- Foo_subscriber.cxx.
template<typename T >
ValidLoanedSamples< T > valid_data |
( |
LoanedSamples< T > && |
samples | ) |
|
|
related |
<<C++11>> <<extension>> Returns a collection that provides access only to samples with valid data
- Template Parameters
-
T | The topic-type. It has to match the type of the DataReader. |
This function transforms a LoanedSamples collection into another collection whose iterators only access valid-data samples, skipping any sample such that !sample.info().valid().
This operation is O(1) and will not copy the data samples or allocated any additional memory.
The typical way to use this function is to directly call it on the return value of a read()/take() operation and use it in a for-loop. For example:
for (const auto& sample : valid_samples) {
std::cout << sample.data() << std::endl;
}
- Parameters
-
samples | The collection of LoanedSamples to transform into a ValidLoanedSamples. It must be an rvalue, so valid actual parameters are the result of one of the read/take operations: Or an std::move'd existing collection: |
- Returns
- A forward-iterable collection that provides access only to samples with valid data. Note that this collection doesn't provide random access.
- Postcondition
samples
is invalid cannot be used after this call
- See also
- rti::sub::valid_data(const SampleIterator<T>&), which applies to an iterator rather to the whole collection
-
Reading data samples
template<typename T >
ValidSampleIterator< T > valid_data |
( |
const SampleIterator< T > & |
sample_iterator | ) |
|
|
related |
<<extension>> Returns an iterator that skips invalid samples
Given a regular sample iterator, this functions creates another iterator it
that behaves exactly the same except that it++
moves to the next valid sample (or to the end of the collection). That is, if it
doesn't point to the end of the collection, it->info.valid()
is always true.
This is useful when your application doesn't need to deal with samples containing meta-information only.
For example, the following code copies all the data in a LoanedSamples collection skipping any invalid samples (otherwise, attempting to copy the data from an invalid sample would throw an exception, see rti::sub::LoanedSample::operator const DataType& ()).
std::vector<KeyedType> data_vector;
std::copy(
std::back_inserter(data_vector));
Note that valid_data
(samples.begin()) won't point to the first element if that element is not a valid sample.
A similar utility is the functor rti::sub::IsValidData.
- See also
- dds::sub::LoanedSamples
-
rti::sub::IsValidData
-
dds::sub::SampleInfo::valid()
-
rti::sub::valid_data(LoanedSamples<T>&&), which applies to the whole collection instead of an iterator
-
Reading data samples