OpenDDS  Snapshot(2023/04/07-19:43)
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
OpenDDS::DCPS::DataCollector< DatumType > Class Template Reference

#include <DataCollector_T.h>

Public Types

enum  OnFull { KeepOldest, KeepNewest, Unbounded }
 Selectors for behavior when buffer fills. More...
 

Public Member Functions

 DataCollector (unsigned int bound=0, OnFull onFull=KeepOldest)
 
virtual ~DataCollector ()
 Allow the class to be extended. More...
 
void collect (const DatumType &datum)
 Implement data collection. More...
 
unsigned int size () const
 Amount of data actually stored. More...
 
DataCollector< DatumType > & operator<< (DatumType datum)
 
std::ostreaminsert (std::ostream &str) const
 Implement insertion of collected data onto an ostream. More...
 

Private Member Functions

 OPENDDS_VECTOR (DatumType) buffer_
 The collected data goes here. More...
 

Private Attributes

unsigned int writeAt_
 Where to write the next datum collected. More...
 
unsigned int bound_
 Total or initial capacity of buffer. More...
 
bool full_
 
OnFull onFull_
 Selector for behavior when buffer fills. More...
 

Detailed Description

template<typename DatumType>
class OpenDDS::DCPS::DataCollector< DatumType >

Definition at line 37 of file DataCollector_T.h.

Member Enumeration Documentation

◆ OnFull

template<typename DatumType>
enum OpenDDS::DCPS::DataCollector::OnFull

Selectors for behavior when buffer fills.

Enumerator
KeepOldest 
KeepNewest 
Unbounded 

Definition at line 40 of file DataCollector_T.h.

Constructor & Destructor Documentation

◆ DataCollector()

template<typename DatumType >
ACE_INLINE OpenDDS::DCPS::DataCollector< DatumType >::DataCollector ( unsigned int  bound = 0,
OnFull  onFull = KeepOldest 
)

Construct with optional buffer size and full behavior.

Parameters
bound- amount of data to store or reserve as buffer.
onFull- behavior of collector when bound is reached.

OnFull == KeepOldest: The buffer is limited to the amount of data specified by the bound parameter and only the data collected first is retained. OnFull == KeepNewest: The buffer is limited to the amount of data specified by the bound parameter and only the most recently collected data is retained. OnFull == Unbounded: The buffer contains all collected data and the bound parameter is used as an initial reservation amount.

Collection is either bounded or unbounded. In the case of a bounded collection, either the first data collected or the most recent data collected is retained. When an unbounded collection is specified, then the bound parameter is used as a capacity hint and that amount of data is reserved initially.

Definition at line 26 of file DataCollector_T.inl.

27  : buffer_(bound),
28  writeAt_(0),
29  bound_(bound),
30  full_(false),
31  onFull_(onFull)
32 {
33  if (this->onFull_ == Unbounded) {
34  this->buffer_.clear();
35  }
36 }
OnFull onFull_
Selector for behavior when buffer fills.
unsigned int writeAt_
Where to write the next datum collected.
unsigned int bound_
Total or initial capacity of buffer.

◆ ~DataCollector()

template<typename DatumType >
OpenDDS::DCPS::DataCollector< DatumType >::~DataCollector ( )
virtual

Allow the class to be extended.

Definition at line 21 of file DataCollector_T.cpp.

22 {
23 }

Member Function Documentation

◆ collect()

template<typename DatumType>
void OpenDDS::DCPS::DataCollector< DatumType >::collect ( const DatumType &  datum)

Implement data collection.

Definition at line 27 of file DataCollector_T.cpp.

28 {
29  if (this->onFull_ == Unbounded) {
30  this->buffer_.push_back(datum);
31 
32  } else {
33  // writeAt == bound only when we either have no buffer (bound == 0) or
34  // when we are full with no wrapping (writeAt == bound)
35  if (this->writeAt_ != this->bound_) {
36  this->buffer_[ this->writeAt_++] = datum;
37 
38  // This datum filled the buffer.
39  if (this->writeAt_ == this->bound_) {
40  this->full_ = true;
41 
42  if (this->onFull_ == KeepNewest) {
43  this->writeAt_ = 0;
44  }
45  }
46  }
47  }
48 }
OnFull onFull_
Selector for behavior when buffer fills.
unsigned int writeAt_
Where to write the next datum collected.
unsigned int bound_
Total or initial capacity of buffer.

◆ insert()

template<typename DatumType >
std::ostream & OpenDDS::DCPS::DataCollector< DatumType >::insert ( std::ostream str) const

Implement insertion of collected data onto an ostream.

Definition at line 64 of file DataCollector_T.cpp.

65 {
66  std::ofstream initStrState;
67  initStrState.copyfmt(str);
68 
69  str.precision(5);
70  str << std::scientific;
71 
72  // Oldest data first.
73  if (this->full_) {
74  for (unsigned int index = this->writeAt_; index < this->bound_; ++index) {
75  str << this->buffer_[ index] << std::endl;
76  }
77  }
78 
79  // Bounded case.
80  int end = this->writeAt_;
81 
82  if (end == 0) {
83  // Unbounded case.
84  end = static_cast<int>(this->buffer_.size());
85  }
86 
87  // Newest data last.
88  for (int index = 0; index < end; ++index) {
89  str << this->buffer_[ index] << std::endl;
90  }
91 
92  str.copyfmt(initStrState);
93  return str;
94 }
unsigned int writeAt_
Where to write the next datum collected.
unsigned int bound_
Total or initial capacity of buffer.

◆ OPENDDS_VECTOR()

template<typename DatumType>
OpenDDS::DCPS::DataCollector< DatumType >::OPENDDS_VECTOR ( DatumType  )
private

The collected data goes here.

◆ operator<<()

template<typename DatumType>
ACE_INLINE DataCollector< DatumType > & OpenDDS::DCPS::DataCollector< DatumType >::operator<< ( DatumType  datum)

Convenience operator for collecting data by inserting it into the collector.

Definition at line 42 of file DataCollector_T.inl.

43 {
44  this->collect(datum);
45  return *this;
46 }
void collect(const DatumType &datum)
Implement data collection.

◆ size()

template<typename DatumType >
unsigned int OpenDDS::DCPS::DataCollector< DatumType >::size ( void  ) const

Amount of data actually stored.

Definition at line 52 of file DataCollector_T.cpp.

Referenced by OpenDDS::DCPS::WriterStats::raw_data().

53 {
54  if (this->onFull_ == Unbounded) return static_cast<unsigned int>(this->buffer_.size());
55 
56  else if (this->full_) return this->bound_;
57 
58  else return this->writeAt_;
59 }
OnFull onFull_
Selector for behavior when buffer fills.
unsigned int writeAt_
Where to write the next datum collected.
unsigned int bound_
Total or initial capacity of buffer.

Member Data Documentation

◆ bound_

template<typename DatumType>
unsigned int OpenDDS::DCPS::DataCollector< DatumType >::bound_
private

Total or initial capacity of buffer.

Definition at line 93 of file DataCollector_T.h.

◆ full_

template<typename DatumType>
bool OpenDDS::DCPS::DataCollector< DatumType >::full_
private

Flag indicating that we have collected as much or more data than we can store.

Definition at line 97 of file DataCollector_T.h.

◆ onFull_

template<typename DatumType>
OnFull OpenDDS::DCPS::DataCollector< DatumType >::onFull_
private

Selector for behavior when buffer fills.

Definition at line 100 of file DataCollector_T.h.

◆ writeAt_

template<typename DatumType>
unsigned int OpenDDS::DCPS::DataCollector< DatumType >::writeAt_
private

Where to write the next datum collected.

Definition at line 90 of file DataCollector_T.h.


The documentation for this class was generated from the following files: