bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
bitsim::utils::io::FileHandlerBase< HandlerType > Class Template Referenceabstract

Abstract base class for file handling operations. More...

#include <file_handler_base.h>

Inheritance diagram for bitsim::utils::io::FileHandlerBase< HandlerType >:
Collaboration diagram for bitsim::utils::io::FileHandlerBase< HandlerType >:

Public Types

using handler_type = HandlerType

Public Member Functions

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.

Protected Member Functions

 FileHandlerBase (const std::string &file_name, FileFormats::Type t)
 Protected constructor.

Protected Attributes

std::string file_name_
 The name of the file to read from or write to.
const FileFormats::Type t_
 The format/type of the file.
handler_type f_
 The underlying file stream/handler.

Detailed Description

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
HandlerTypeThe 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

class MyFileReader : public FileHandlerBase<std::ifstream> {
public:
void open() override {
f_.open(file_name_, std::ios::in);
}
};
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.

Member Typedef Documentation

◆ handler_type

template<typename HandlerType>
using bitsim::utils::io::FileHandlerBase< HandlerType >::handler_type = HandlerType

Constructor & Destructor Documentation

◆ ~FileHandlerBase()

template<typename HandlerType>
bitsim::utils::io::FileHandlerBase< HandlerType >::~FileHandlerBase ( )
virtual

Destructor. Ensures that the file is properly closed.

Postcondition
The underlying file stream is closed.

◆ FileHandlerBase()

template<typename HandlerType>
bitsim::utils::io::FileHandlerBase< HandlerType >::FileHandlerBase ( const std::string & file_name,
FileFormats::Type t )
protected

Protected constructor.

Initializes the file handler with a file name and format.

Parameters
file_nameThe name (or path) of the file to be handled.
tThe file format/type.
Precondition
file_name should not be empty.
Postcondition
The handler is initialized but the file is not open.

Member Function Documentation

◆ close()

template<typename HandlerType>
void bitsim::utils::io::FileHandlerBase< HandlerType >::close ( )
virtual

Closes the file if it is currently open.

If the underlying file stream is open, this function attempts to close it. If the file is already closed, no action is taken.

Postcondition
The file stream is not open (is_open() == false).
Exceptions
Maypropagate exceptions thrown by handler_type::close() if the underlying stream supports throwing exceptions.
Note
Calling this function multiple times is safe.

◆ get_file_path()

template<typename HandlerType>
std::filesystem::path bitsim::utils::io::FileHandlerBase< HandlerType >::get_file_path ( ) const
inlinenoexcept

Returns the file path associated with this handler.

Returns
The file name as a std::filesystem::path object.

◆ get_file_stream() [1/2]

template<typename HandlerType>
const handler_type & bitsim::utils::io::FileHandlerBase< HandlerType >::get_file_stream ( ) const
inlinenoexcept

Provides read-only access to the underlying file stream.

Returns
Const reference to the underlying file handler.

◆ get_file_stream() [2/2]

template<typename HandlerType>
handler_type & bitsim::utils::io::FileHandlerBase< HandlerType >::get_file_stream ( )
inlinenoexcept

Provides mutable access to the underlying file stream.

Returns
Reference to the underlying file handler.
Precondition
The file may or may not be open.

◆ get_filename()

template<typename HandlerType>
std::string bitsim::utils::io::FileHandlerBase< HandlerType >::get_filename ( ) const
inlinenoexcept

Returns the file name associated with this handler.

Returns
The file name as a std::string.

◆ get_type()

template<typename HandlerType>
FileFormats::Type bitsim::utils::io::FileHandlerBase< HandlerType >::get_type ( ) const
inlinenoexcept

Returns the type/format of the file.

Returns
The file format as a FileFormats::Type value.
Postcondition
Does not modify object state.

◆ is_open()

template<typename HandlerType>
bool bitsim::utils::io::FileHandlerBase< HandlerType >::is_open ( ) const
inlinenoexcept

Checks whether the file is currently open.

Returns
true if the file stream is open, false otherwise.

◆ open()

template<typename HandlerType>
virtual void bitsim::utils::io::FileHandlerBase< HandlerType >::open ( )
pure virtual

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.

Member Data Documentation

◆ f_

template<typename HandlerType>
handler_type bitsim::utils::io::FileHandlerBase< HandlerType >::f_
protected

The underlying file stream/handler.

This object represents the low-level file interface (e.g., std::ifstream, std::ofstream, etc.).

◆ file_name_

template<typename HandlerType>
std::string bitsim::utils::io::FileHandlerBase< HandlerType >::file_name_
protected

The name of the file to read from or write to.

Represents the path used when opening the file.

◆ t_

template<typename HandlerType>
const FileFormats::Type bitsim::utils::io::FileHandlerBase< HandlerType >::t_
protected

The format/type of the file.

Immutable after construction.


The documentation for this class was generated from the following file: