OpenDDS  Snapshot(2023/04/28-20:55)
DataCollector_T.cpp
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #include "DataCollector_T.h"
9 #include "SafetyProfileStreams.h"
10 
11 #if !defined (__ACE_INLINE__)
12 #include "DataCollector_T.inl"
13 #endif /* __ACE_INLINE__ */
14 
16 
17 namespace OpenDDS {
18 namespace DCPS {
19 
20 template<typename DatumType>
22 {
23 }
24 
25 template<typename DatumType>
26 void
27 DataCollector<DatumType>::collect(const DatumType& datum)
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 }
49 
50 template<typename DatumType>
51 unsigned int
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 }
60 
61 #ifndef OPENDDS_SAFETY_PROFILE
62 template<typename DatumType>
63 std::ostream&
64 DataCollector<DatumType>::insert(std::ostream& str) const
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 }
95 #endif //OPENDDS_SAFETY_PROFILE
96 
97 } // namespace DCPS
98 } // namespace OpenDDS
99 
unsigned int size() const
Amount of data actually stored.
void collect(const DatumType &datum)
Implement data collection.
virtual ~DataCollector()
Allow the class to be extended.
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
std::ostream & insert(std::ostream &str) const
Implement insertion of collected data onto an ostream.