bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
file_handler_base.h
Go to the documentation of this file.
1#ifndef BITSIM_UTILS_IO_FILE_HANDLER_BASE_H
2#define BITSIM_UTILS_IO_FILE_HANDLER_BASE_H
3
5#include "boost/noncopyable.hpp"
6
7#include <filesystem>
8#include <string>
9
10namespace bitsim
11{
12namespace utils::io
13{
59template <typename HandlerType> class FileHandlerBase : private boost::noncopyable
60{
61 public:
62 using handler_type = HandlerType;
63
70
77 FileFormats::Type get_type() const noexcept { return t_; }
78
85 handler_type &get_file_stream() noexcept { return f_; }
86
92 const handler_type &get_file_stream() const noexcept { return f_; }
93
99 std::string get_filename() const noexcept { return file_name_; }
100
106 std::filesystem::path get_file_path() const noexcept { return file_name_; }
107
113 bool is_open() const noexcept { return f_.is_open(); }
114
126 virtual void close();
127
141 virtual void open() = 0;
142
143 protected:
155 FileHandlerBase(const std::string &file_name, FileFormats::Type t);
156
162 std::string file_name_;
163
170
178};
179
180template <typename HandlerType>
182 : file_name_(file_name), t_(t)
183{
184}
185
186template <typename HandlerType> FileHandlerBase<HandlerType>::~FileHandlerBase() { close(); }
187
188template <typename HandlerType> void FileHandlerBase<HandlerType>::close()
189{
190
191 if (f_.is_open())
192 f_.close();
193}
194
195} // namespace utils::io
196} // namespace bitsim
197
198#endif // BITSIM_UTILS_IO_FILE_HANDLER_BASE_H
std::string get_filename() const noexcept
Returns the file name associated with this handler.
Definition file_handler_base.h:99
std::string file_name_
The name of the file to read from or write to.
Definition file_handler_base.h:162
handler_type f_
The underlying file stream/handler.
Definition file_handler_base.h:177
std::filesystem::path get_file_path() const noexcept
Returns the file path associated with this handler.
Definition file_handler_base.h:106
FileHandlerBase(const std::string &file_name, FileFormats::Type t)
Protected constructor.
Definition file_handler_base.h:181
const FileFormats::Type t_
The format/type of the file.
Definition file_handler_base.h:169
const handler_type & get_file_stream() const noexcept
Provides read-only access to the underlying file stream.
Definition file_handler_base.h:92
bool is_open() const noexcept
Checks whether the file is currently open.
Definition file_handler_base.h:113
FileFormats::Type get_type() const noexcept
Returns the type/format of the file.
Definition file_handler_base.h:77
virtual void close()
Closes the file if it is currently open.
Definition file_handler_base.h:188
virtual ~FileHandlerBase()
Destructor. Ensures that the file is properly closed.
Definition file_handler_base.h:186
handler_type & get_file_stream() noexcept
Provides mutable access to the underlying file stream.
Definition file_handler_base.h:85
virtual void open()=0
Opens the file.
HandlerType handler_type
Definition file_handler_base.h:62
Definition csv_file_reader.cpp:12
Definition dc_motor_backend_base.h:9
Type
Definition file_formats.h:24