Helios++
Helios software for LiDAR simulations
MultiSyncFileWriter.h
1 #pragma once
2 
3 #include <filems/write/comps/SyncFileWriter.h>
4 #include <filems/write/strategies/WriteStrategy.h>
5 
6 #include <mutex>
7 #include <memory>
8 
9 namespace helios { namespace filems{
10 
11 
20 template <typename ... WriteArgs>
21 class MultiSyncFileWriter : public SyncFileWriter<WriteArgs ...>{
22 protected:
23  // *** ATTRIBUTES *** //
24  // ******************** //
28  std::vector<std::string> path;
33  std::vector<std::mutex> mutex;
37  std::vector<std::shared_ptr<WriteStrategy<WriteArgs ...>>> writeStrategy;
38 
39  // *** CONSTRUCTION / DESTRUCTION *** //
40  // *********************************** //
46  MultiSyncFileWriter() : SyncFileWriter<WriteArgs ...>() {}
47  explicit MultiSyncFileWriter(std::vector<std::string> const &path) :
48  SyncFileWriter<WriteArgs ...>(),
49  path(path),
50  mutex(path.size())
51  {}
52  virtual ~MultiSyncFileWriter(){}
53 
54  // *** W R I T E *** //
55  // ******************* //
60  void write(WriteArgs ... writeArgs) override {
61  // Obtain the index of file to be written from the write arguments
62  size_t const idx = indexFromWriteArgs(writeArgs ...);
63 
64  // Get the mutex to have exclusive access
65  std::lock_guard<std::mutex> lock(mutex[idx]);
66 
67  // Write data function
68  try {
69  writeStrategy[idx]->write(writeArgs ...);
70  }
71  catch(std::exception &e){
72  std::stringstream ss;
73  ss << "MultiSyncFileWriter failed to write. EXCEPTION: \n\t"
74  << e.what();
75  logging::WARN(ss.str());
76  }
77  }
85  virtual size_t indexFromWriteArgs(WriteArgs ... writeArgs) = 0;
86 
87  // *** GETTERS and SETTERS *** //
88  // ***************************** //
94  std::string getPath(size_t const idx) const override {return path[idx];}
95 };
96 
97 
98 }}
Abstract class defining common behavior for all synchronous writers that work with multiple files at ...
Definition: MultiSyncFileWriter.h:21
MultiSyncFileWriter()
Default constructor for synchronous multi-file writer.
Definition: MultiSyncFileWriter.h:46
std::vector< std::mutex > mutex
One mutex per file to be written (i-th mutex corresponds to i-th file)
Definition: MultiSyncFileWriter.h:33
std::vector< std::string > path
Paths to the files to be written.
Definition: MultiSyncFileWriter.h:28
virtual size_t indexFromWriteArgs(WriteArgs ... writeArgs)=0
Any concrete implementation extending MultiSyncFileWriter must override this function to specify how ...
std::string getPath(size_t const idx) const override
Obtain the path to the idx-th file.
Definition: MultiSyncFileWriter.h:94
void write(WriteArgs ... writeArgs) override
Synchronously write to a single file.
Definition: MultiSyncFileWriter.h:60
std::vector< std::shared_ptr< WriteStrategy< WriteArgs ... > > > writeStrategy
The write strategies specifying how to write data, one per file.
Definition: MultiSyncFileWriter.h:37
Abstract class defining common behavior for all synchronous file writers.
Definition: SyncFileWriter.h:18
Interface that must be implemented by any class which supports write implementations for file writers...
Definition: WriteStrategy.h:14