00001 /* 00002 * 00003 * 00004 * Distributed under the OpenDDS License. 00005 * See: http://www.opendds.org/license.html 00006 */ 00007 00008 #ifndef DATA_COLLECTOR_H 00009 #define DATA_COLLECTOR_H 00010 00011 // Needed here to avoid the pragma below when necessary. 00012 #include /**/ "ace/pre.h" 00013 #include /**/ "ace/config-all.h" 00014 00015 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00016 #pragma once 00017 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00018 00019 #include "dds/DCPS/PoolAllocator.h" 00020 #include "dds/DCPS/SafetyProfileStreams.h" 00021 00022 00023 namespace OpenDDS { 00024 namespace DCPS { 00025 00026 /** 00027 * @class DataCollector<DatumType> 00028 * 00029 * @brief Collect data in a buffer. 00030 * 00031 * This class implements a rudimentary data collection mechanism. It is 00032 * extendable to allow data to be collected as a feature of a more 00033 * complex class. 00034 */ 00035 template<typename DatumType> 00036 class DataCollector { 00037 public: 00038 /// Selectors for behavior when buffer fills. 00039 enum OnFull { KeepOldest, KeepNewest, Unbounded}; 00040 00041 /** 00042 * Construct with optional buffer size and full behavior. 00043 * 00044 * @param bound - amount of data to store or reserve as buffer. 00045 * @param onFull - behavior of collector when bound is reached. 00046 * 00047 * OnFull == KeepOldest: The buffer is limited to the amount of data 00048 * specified by the bouund parameter and only 00049 * the data collected first is retained. 00050 * OnFull == KeepNewest: The buffer is limited to the amount of data 00051 * specified by the bouund parameter and only 00052 * the most recently collected data is 00053 * retained. 00054 * OnFull == Unbounded: The buffer contains all collected data and 00055 * the bound parameter is used as an initial 00056 * reservation amount. 00057 * 00058 * Collection is either bounded or unbounded. In the case of a 00059 * bounded collection, either the first data collected or the most 00060 * recent data collected is retained. When an unbounded collection 00061 * is specified, then the bound parameter is used as a capacity hint 00062 * and that amount of data is reserved initially. 00063 */ 00064 DataCollector(unsigned int bound = 0, OnFull onFull = KeepOldest); 00065 00066 /// Allow the class to be extended. 00067 virtual ~DataCollector(); 00068 00069 /// Implement data collection. 00070 void collect(const DatumType& datum); 00071 00072 /// Amount of data actually stored. 00073 unsigned int size() const; 00074 00075 #ifndef OPENDDS_SAFETY_PROFILE 00076 /// Convenience operator for collecting data by inserting it into the 00077 /// collector. 00078 DataCollector<DatumType>& operator<<(DatumType datum); 00079 00080 /// Implement insertion of collected data onto an ostream. 00081 std::ostream& insert(std::ostream& str) const; 00082 #endif //OPENDDS_SAFETY_PROFILE 00083 00084 private: 00085 /// The collected data goes here. 00086 OPENDDS_VECTOR(DatumType) buffer_; 00087 00088 /// Where to write the next datum collected. 00089 unsigned int writeAt_; 00090 00091 /// Total or initial capacity of buffer. 00092 unsigned int bound_; 00093 00094 /// Flag indicating that we have collected as much or more data than 00095 /// we can store. 00096 bool full_; 00097 00098 /// Selector for behavior when buffer fills. 00099 OnFull onFull_; 00100 }; 00101 00102 #ifndef OPENDDS_SAFETY_PROFILE 00103 /// Insert collected data onto an ostream. 00104 template<typename DatumType> 00105 std::ostream& operator<<( 00106 std::ostream& str, 00107 const DataCollector<DatumType>& value); 00108 #endif //OPENDDS_SAFETY_PROFILE 00109 00110 } // namespace DCPS 00111 } // namespace OpenDDS 00112 00113 00114 #if defined (__ACE_INLINE__) 00115 #include "DataCollector_T.inl" 00116 #endif /* __ACE_INLINE__ */ 00117 00118 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) 00119 #include "DataCollector_T.cpp" 00120 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ 00121 00122 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00123 #pragma implementation ("DataCollector_T.cpp") 00124 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00125 00126 #include /**/ "ace/post.h" 00127 00128 #endif /* DATA_COLLECTOR_H */