Helios++
Helios software for LiDAR simulations
SingleSyncFileWriter.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 SingleSyncFileWriter : public SyncFileWriter<WriteArgs ...>{
22 protected:
23  // *** ATTRIBUTES *** //
24  // ******************** //
28  std::string path;
32  std::mutex mutex;
36  std::shared_ptr<WriteStrategy<WriteArgs ...>> writeStrategy = nullptr;
37 
38 
39 public:
40  // *** CONSTRUCTION / DESTRUCTION *** //
41  // ************************************ //
45  SingleSyncFileWriter() : SyncFileWriter<WriteArgs ...>() {}
46 
53  explicit SingleSyncFileWriter(const std::string & path) :
54  SyncFileWriter<WriteArgs ...>(),
55  path(path)
56  {}
57  virtual ~SingleSyncFileWriter(){}
58 
59  // *** W R I T E *** //
60  // ******************* //
65  void write(WriteArgs ... writeArgs) override {
66  // Get the mutex to have exclusive access
67  std::lock_guard<std::mutex> lock(mutex);
68 
69  // Write data function
70  try {
71  writeStrategy->write(writeArgs ...);
72  }
73  catch(std::exception &e){
74  std::stringstream ss;
75  ss << "SingleSyncFileWriter failed to write. EXCEPTION: \n\t"
76  << e.what();
77  logging::WARN(ss.str());
78  }
79  }
80 
81  // *** GETTERS and SETTERS *** //
82  // ***************************** //
87  std::string getPath(size_t const idx) const override {return path;}
88 };
89 
90 }}
Abstract class defining common behavior for all synchronous writers that work with a single file at a...
Definition: SingleSyncFileWriter.h:21
std::mutex mutex
Mutex to synchronize concurrent write operations.
Definition: SingleSyncFileWriter.h:32
std::string path
Path to file to be written.
Definition: SingleSyncFileWriter.h:28
std::shared_ptr< WriteStrategy< WriteArgs ... > > writeStrategy
The write strategy specifying how to write data to file.
Definition: SingleSyncFileWriter.h:36
void write(WriteArgs ... writeArgs) override
Synchronously write to a single file.
Definition: SingleSyncFileWriter.h:65
SingleSyncFileWriter()
Default constructor for synchronous single-file writer.
Definition: SingleSyncFileWriter.h:45
std::string getPath(size_t const idx) const override
Obtain the path to the file.
Definition: SingleSyncFileWriter.h:87
SingleSyncFileWriter(const std::string &path)
Instantiate a SingleSyncFileWriter which writes to file at given path.
Definition: SingleSyncFileWriter.h:53
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