Helios++
Helios software for LiDAR simulations
SimpleFileReader.h
1 #pragma once
2 
3 #include <filems/read/comps/FileReader.h>
4 
5 #include <sstream>
6 #include <fstream>
7 #include <memory>
8 
9 namespace helios { namespace filems{
10 
11 using std::string;
12 using std::ifstream;
13 using std::ios_base;
14 using std::make_shared;
15 
24 template <typename ReadArg>
25 class SimpleFileReader : public FileReader<ReadArg>{
26 protected:
27  // *** USING *** //
28  // *************** //
31 
32  // *** ATTRIBUTES *** //
33  // ******************** //
37  ifstream ifs;
42  ios_base::openmode openMode;
43 
44 public:
45  // *** CONSTRUCTION / DESTRUCTION *** //
46  // ************************************ //
54  string const &path,
55  ios_base::openmode openMode = ios_base::in
56  ) :
57  FileReader<ReadArg>(path),
58  ifs(path, openMode),
60  {
61  if(!ifs.is_open()){
62  std::stringstream ss;
63  ss << "SimpleFileReader::SimpleFileReader("
64  << "string const &, ios__base::openmode"
65  << ") failed to open file at path:\n\""
66  << path << "\"";
67  throw std::ios_base::failure(ss.str());
68  }
69  }
70  virtual ~SimpleFileReader() = default;
71 
72  // *** READ METHODS *** //
73  // ********************** //
81  ReadArg read() override {return readingStrategy->read();};
82 
83  // *** GETTERs and SETTERs *** //
84  // ***************************** //
91  void setPath(string const &path) override {
93  ifs.close();
94  ifs = ifstream(path, openMode);
95  makeStrategy();
96  }
97 };
98 
99 }}
Abstract class defining the fundamentals of any file reader.
Definition: FileReader.h:20
virtual void makeStrategy()=0
Build the strategy for the file reader.
shared_ptr< ReadingStrategy< ReadArg > > readingStrategy
The reading strategy to be used by the file reader.
Definition: FileReader.h:32
string path
Path to the file to be read.
Definition: FileReader.h:27
virtual void setPath(string const &path)
Set the path to the file to be read.
Definition: FileReader.h:77
Abstract class defining the fundamental of any file reader that uses standard file input stream as re...
Definition: SimpleFileReader.h:25
void setPath(string const &path) override
Set the path to the file to be read, also opening the input stream for the new file and updating the ...
Definition: SimpleFileReader.h:91
ReadArg read() override
Read from file simply by applying the reading strategy. Therefore, there is no concurrency handling m...
Definition: SimpleFileReader.h:81
ifstream ifs
The input file stream to read from.
Definition: SimpleFileReader.h:37
SimpleFileReader(string const &path, ios_base::openmode openMode=ios_base::in)
Default constructor for simple file reader.
Definition: SimpleFileReader.h:53
ios_base::openmode openMode
The open mode flags for the input file stream.
Definition: SimpleFileReader.h:42