|
| virtual | ~FileHandlerBase () |
| | Destructor. Ensures that the file is properly closed.
|
| FileFormats::Type | get_type () const noexcept |
| | Returns the type/format of the file.
|
| handler_type & | get_file_stream () noexcept |
| | Provides mutable access to the underlying file stream.
|
| const handler_type & | get_file_stream () const noexcept |
| | Provides read-only access to the underlying file stream.
|
| std::string | get_filename () const noexcept |
| | Returns the file name associated with this handler.
|
| std::filesystem::path | get_file_path () const noexcept |
| | Returns the file path associated with this handler.
|
| bool | is_open () const noexcept |
| | Checks whether the file is currently open.
|
| virtual void | close () |
| | Closes the file if it is currently open.
|
| virtual void | open ()=0 |
| | Opens the file.
|
template<typename HandlerType>
class bitsim::utils::io::FileHandlerBase< HandlerType >
Abstract base class for file handling operations.
This class provides a common interface and shared functionality for managing file input/output operations. It encapsulates a low-level file handler (e.g., std::ifstream, std::ofstream, or similar types) and enforces a consistent API for derived classes that implement specific file reading or writing behavior.
The class is non-copyable (via boost::noncopyable) to prevent unintended duplication of file handles.
- Template Parameters
-
| HandlerType | The underlying file stream/handler type. |
Responsibilities
- Store and expose the file name and file format.
- Manage the lifecycle of the file stream (open/close).
- Provide access to the underlying file handler.
- Ensure proper cleanup by closing the file in the destructor.
Usage
This class is intended to be inherited by concrete file handler implementations (e.g., readers or writers). Derived classes must implement the open() method to define how the file is opened.
Example
public:
}
};
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
FileHandlerBase(const std::string &file_name, FileFormats::Type t)
Protected constructor.
Definition file_handler_base.h:181
virtual void open()=0
Opens the file.
Notes
- The destructor automatically calls close() to ensure the file is properly released.
- Calling close() on an already closed file is safe.
- The class does not handle error reporting; this should be implemented in derived classes if needed.
template<typename HandlerType>
Opens the file.
This is a pure virtual function that must be implemented by derived classes. Implementations should define how the file is opened (e.g., read/write mode, binary/text mode, etc.).
- Precondition
- The file is not already open, unless the derived class explicitly supports reopening.
- Postcondition
- If successful, is_open() == true.
- Exceptions
-
| Implementation-defined. | Derived classes should document any exceptions or error-handling behavior. |
Implemented in bitsim::utils::io::FileReaderBase, bitsim::utils::io::FileWriterBase, bitsim::utils::io::JSONFileReader, example::JSONFileReader, intro_example_1::JSONFileReader, intro_example_4::JSONFileReader, and tensorboard::JSONFileReader.