Helios++
Helios software for LiDAR simulations
HDA_RecordBuffer.h
1 #ifdef DATA_ANALYTICS
2 #pragma once
3 
4 #include <vector>
5 #include <string>
6 #include <fstream>
7 #include <functional>
8 
9 namespace helios { namespace analytics {
10 
19 template <typename T>
20 class HDA_RecordBuffer {
21 protected:
22  // *** ATTRIBUTES *** //
23  // ******************** //
27  std::vector<T> buff;
31  size_t maxSize;
35  std::string outpath;
39  std::ofstream ofs;
43  std::string sep;
48  std::function<void(void)> write;
49 
50 public:
51  // *** CONSTRUCTION / DESTRUCTION *** //
52  // ************************************ //
59  HDA_RecordBuffer(
60  std::string const &outpath,
61  size_t const maxSize=256,
62  std::string const &sep=","
63  ) :
64  maxSize(maxSize),
65  outpath(outpath),
66  ofs(outpath, std::ios_base::out),
67  sep(sep)
68  {
69  write = [&](void) -> void {this->firstWrite();};
70  }
71 
72  virtual ~HDA_RecordBuffer(){
73  if(isOpen()) close();
74  }
75 
76  // *** RECORD BUFFER METHODS *** //
77  // ******************************* //
83  inline bool isOpen() {return ofs.is_open();}
88  inline void firstWrite() {
89  size_t numElems = buff.size();
90  ofs << buff[0];
91  for(size_t i = 1 ; i < numElems ; ++i){
92  ofs << sep << buff[i];
93  }
94  this->write = [&] (void) -> void {this->nextWrite();};
95  }
100  inline void nextWrite() {
101  for(T const & elem : buff) ofs << sep << elem;
102  }
106  inline void flush(){
107  if(buff.size() > 0){
108  this->write();
109  buff.clear();
110  }
111  }
116  inline void close(){
117  flush();
118  if(isOpen()) ofs.close();
119  }
126  inline void push(T const & elem){
127  if(buff.size() >= maxSize) flush();
128  buff.push_back(elem);
129  }
130 
131 
132 };
133 
134 }}
135 
136 #endif