GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
grem::InputFileHandle Class Reference

Generic boxed unique handle to a readable file in any filesystem. More...

#include <GREM/core/system/File.hpp>

Public Member Functions

 InputFileHandle () noexcept=default
 Construct a null file handle without an associated file.
GREM_ALWAYS_INLINE InputFile * get () noexcept
 Get a pointer to the underlying file interface.
GREM_ALWAYS_INLINE const InputFile * get () const noexcept
 Get a pointer to the underlying file interface.
GREM_ALWAYS_INLINE operator InputFile & ()
 Get a reference to the underlying file interface.
GREM_ALWAYS_INLINE operator const InputFile & () const
 Get a reference to the underlying file interface.
GREM_ALWAYS_INLINE operator Reader () &
 Get a reader for the underlying file interface.
GREM_ALWAYS_INLINE operator bool () const noexcept
 Check if the file handle has an open file associated with it.
GREM_ALWAYS_INLINE void close ()
 Close the associated file so that it can no longer be accessed through this handle, and reset the handle to a closed file handle without an associated file.
GREM_ALWAYS_INLINE bool isOpen () const noexcept
 Check if the file handle has an open file associated with it.
GREM_ALWAYS_INLINE bool eof () const noexcept
 Check if the end of the file has been reached.
GREM_ALWAYS_INLINE size_t size () const noexcept
 Get the readable length of the full file contents, in bytes.
GREM_ALWAYS_INLINE size_t tellg () const noexcept
 Get the current reading position of the file.
GREM_ALWAYS_INLINE void seekg (size_t position)
 Set the file reading position to an absolute offset from the beginning of the file.
GREM_ALWAYS_INLINE void skipg (ptrdiff_t offset)
 Advance the file reading position by a relative offset, which may be negative in order to go backwards.
GREM_ALWAYS_INLINE Allocation< byte > readBytesIntoAllocation (size_t maxLength=Limits< size_t >::MAX)
 Read from an open file into an allocation of bytes.
GREM_ALWAYS_INLINE ArrayList< byte > readBytesIntoArrayList (size_t maxLength=Limits< size_t >::MAX)
 Read from an open file into an array list of bytes.
GREM_ALWAYS_INLINE Buffer< byte > readBytesIntoBuffer (size_t maxLength=Limits< size_t >::MAX)
 Read from an open file into a buffer of bytes.
GREM_ALWAYS_INLINE String readBytesIntoString (size_t maxLength=Limits< size_t >::MAX)
 Read from an open file into a string of bytes.
GREM_ALWAYS_INLINE Allocation< char > readBytesIntoCString (size_t maxLength=Limits< size_t >::MAX)
 Read from an open file into an allocated null-terminated string of bytes.
GREM_ALWAYS_INLINE size_t readSome (Span< byte > data)
 Read some amount of data from an open file, starting at the current reading position.
GREM_ALWAYS_INLINE size_t readUntilEOF (Span< byte > data)
 Read data from an open file into a buffer, starting at the current reading position.
GREM_ALWAYS_INLINE void read (Span< byte > data)
 Read an exact amount of data from an open file, starting at the current reading position.
GREM_ALWAYS_INLINE bool tryRead (Span< byte > data)
 Try to read an exact amount of data from an open file, starting at the current reading position.

Static Public Member Functions

template<typename Implementation, typename... Args>
static GREM_ALWAYS_INLINE InputFileHandle create (Args &&... args)
 Create a boxed file with a specific implementation.

Detailed Description

Generic boxed unique handle to a readable file in any filesystem.

Constructor & Destructor Documentation

◆ InputFileHandle()

grem::InputFileHandle::InputFileHandle ( )
defaultnoexcept

Construct a null file handle without an associated file.

Member Function Documentation

◆ create()

template<typename Implementation, typename... Args>
GREM_ALWAYS_INLINE InputFileHandle grem::InputFileHandle::create ( Args &&... args)
inlinestaticnodiscard

Create a boxed file with a specific implementation.

Template Parameters
Implementationconcrete file type.
Parameters
argsarguments to forward to the concrete file constructor.
Returns
the new boxed file.
Exceptions
anyexception thrown by the concrete file's constructor.
std::bad_allocon allocation failure.

◆ get() [1/2]

GREM_ALWAYS_INLINE InputFile * grem::InputFileHandle::get ( )
inlinenodiscardnoexcept

Get a pointer to the underlying file interface.

Returns
a non-owning pointer to the underlying file interface.

◆ get() [2/2]

GREM_ALWAYS_INLINE const InputFile * grem::InputFileHandle::get ( ) const
inlinenodiscardnoexcept

Get a pointer to the underlying file interface.

Returns
a non-owning read-only pointer to the underlying file interface.

◆ operator InputFile &()

GREM_ALWAYS_INLINE grem::InputFileHandle::operator InputFile & ( )
inlinenodiscard

Get a reference to the underlying file interface.

Returns
a reference to the underlying file interface.
Warning
The file handle must not be null.

◆ operator const InputFile &()

GREM_ALWAYS_INLINE grem::InputFileHandle::operator const InputFile & ( ) const
inlinenodiscard

Get a reference to the underlying file interface.

Returns
a read-only reference to the underlying file interface.
Warning
The file handle must not be null.

◆ operator Reader()

GREM_ALWAYS_INLINE grem::InputFileHandle::operator Reader ( ) &
inlinenodiscard

Get a reader for the underlying file interface.

Returns
a reader for the underlying file interface.
Warning
The file handle must not be null.

◆ operator bool()

GREM_ALWAYS_INLINE grem::InputFileHandle::operator bool ( ) const
inlineexplicitnoexcept

Check if the file handle has an open file associated with it.

Returns
true if there is an associated open file, false otherwise.

◆ close()

GREM_ALWAYS_INLINE void grem::InputFileHandle::close ( )
inline

Close the associated file so that it can no longer be accessed through this handle, and reset the handle to a closed file handle without an associated file.

Exceptions
File::Erroron failure to close the file.
std::bad_allocon allocation failure.
Note
This function has no effect if the handle has no open file associated with it.

◆ isOpen()

GREM_ALWAYS_INLINE bool grem::InputFileHandle::isOpen ( ) const
inlinenodiscardnoexcept

Check if the file handle has an open file associated with it.

Returns
true if there is an associated open file, false otherwise.

◆ eof()

GREM_ALWAYS_INLINE bool grem::InputFileHandle::eof ( ) const
inlinenodiscardnoexcept

Check if the end of the file has been reached.

Returns
true if the end of the file has been reached, false otherwise.

◆ size()

GREM_ALWAYS_INLINE size_t grem::InputFileHandle::size ( ) const
inlinenodiscardnoexcept

Get the readable length of the full file contents, in bytes.

Returns
the length of the file, or File::npos if the length cannot be determined.

◆ tellg()

GREM_ALWAYS_INLINE size_t grem::InputFileHandle::tellg ( ) const
inlinenodiscardnoexcept

Get the current reading position of the file.

Returns
the current file reading position, or File::npos if it cannot be determined.

◆ seekg()

GREM_ALWAYS_INLINE void grem::InputFileHandle::seekg ( size_t position)
inline

Set the file reading position to an absolute offset from the beginning of the file.

Parameters
positionthe new reading position to set.
Exceptions
File::Erroron failure to seek to the given position.
Note
Attempting to seek to a position outside the readable length of the file will cause the function to fail.

◆ skipg()

GREM_ALWAYS_INLINE void grem::InputFileHandle::skipg ( ptrdiff_t offset)
inline

Advance the file reading position by a relative offset, which may be negative in order to go backwards.

Parameters
offsetthe relative offset to advance the reading position by.
Exceptions
File::Erroron failure to skip by the given offset.
Note
Attempting to seek to a position outside the readable length of the file will cause the function to fail.

◆ readBytesIntoAllocation()

GREM_ALWAYS_INLINE Allocation< byte > grem::InputFileHandle::readBytesIntoAllocation ( size_t maxLength = Limits<size_t>::MAX)
inlinenodiscard

Read from an open file into an allocation of bytes.

Parameters
maxLengthmaximum number of bytes to read.
Returns
an allocation containing the file contents read from the current read position until the end of the file, or until maxLength bytes, whichever comes first.
Exceptions
File::Erroron failure to read the file contents.
std::length_errorif the maximum file size was exceeded.
std::bad_allocon allocation failure.

◆ readBytesIntoArrayList()

GREM_ALWAYS_INLINE ArrayList< byte > grem::InputFileHandle::readBytesIntoArrayList ( size_t maxLength = Limits<size_t>::MAX)
inlinenodiscard

Read from an open file into an array list of bytes.

Parameters
maxLengthmaximum number of bytes to read.
Returns
an array list of the file contents read from the current read position until the end of the file, or until maxLength bytes, whichever comes first.
Exceptions
File::Erroron failure to read the file contents.
std::length_errorif the maximum file size was exceeded.
std::bad_allocon allocation failure.

◆ readBytesIntoBuffer()

GREM_ALWAYS_INLINE Buffer< byte > grem::InputFileHandle::readBytesIntoBuffer ( size_t maxLength = Limits<size_t>::MAX)
inlinenodiscard

Read from an open file into a buffer of bytes.

Parameters
maxLengthmaximum number of bytes to read.
Returns
a buffer of the file contents read from the current read position until the end of the file, or until maxLength bytes, whichever comes first.
Exceptions
File::Erroron failure to read the file contents.
std::length_errorif the maximum file size was exceeded.
std::bad_allocon allocation failure.

◆ readBytesIntoString()

GREM_ALWAYS_INLINE String grem::InputFileHandle::readBytesIntoString ( size_t maxLength = Limits<size_t>::MAX)
inlinenodiscard

Read from an open file into a string of bytes.

Parameters
maxLengthmaximum number of bytes to read.
Returns
a string of the file contents read from the current read position until the end of the file, or until maxLength bytes, whichever comes first.
Exceptions
File::Erroron failure to read the file contents.
std::length_errorif the maximum file size was exceeded.
std::bad_allocon allocation failure.

◆ readBytesIntoCString()

GREM_ALWAYS_INLINE Allocation< char > grem::InputFileHandle::readBytesIntoCString ( size_t maxLength = Limits<size_t>::MAX)
inlinenodiscard

Read from an open file into an allocated null-terminated string of bytes.

Parameters
maxLengthmaximum number of bytes to read.
Returns
an allocated null-terminated string of the file contents read from the current read position until the end of the file, or until maxLength bytes, whichever comes first.
Exceptions
File::Erroron failure to read the file contents.
std::length_errorif the maximum file size was exceeded.
std::bad_allocon allocation failure.

◆ readSome()

GREM_ALWAYS_INLINE size_t grem::InputFileHandle::readSome ( Span< byte > data)
inlinenodiscard

Read some amount of data from an open file, starting at the current reading position.

The reading position is advanced to the end of the bytes that were read.

Parameters
datawritable span over the buffer to read file data into. The size of the span determines the number of bytes that are attempted to be read.
Returns
the number of bytes that were read, which may be any non-negative integer less than or equal to the size of the given span, including 0 if the reading position was already at the end of the file, though always a positive value otherwise.
Note
The number of bytes read could be smaller than requested, even if more bytes are available. To read as many bytes as possible, use readUntilEOF() instead.
Exceptions
File::Erroron failure to read from the file.
See also
readUntilEOF()
read()
tryRead()

◆ readUntilEOF()

GREM_ALWAYS_INLINE size_t grem::InputFileHandle::readUntilEOF ( Span< byte > data)
inlinenodiscard

Read data from an open file into a buffer, starting at the current reading position.

The reading position is advanced to the end of the bytes that were read.

Parameters
datawritable span over the buffer to read file data into. The size of the span determines the number of bytes that are attempted to be read.
Returns
the number of bytes that were successfully read, i.e. data.size() or (fileSize - previousReadingPosition), whichever is smaller. This includes 0 if and only if the reading position was already at the end of the file.
Note
If the end of the file is reached before the entire span's worth of data could be read, this will be reflected in the returned number of read bytes. To make sure all requested data was read, use read() instead.
Exceptions
File::Erroron failure to read from the file.
See also
readSome()
read()
tryRead()

◆ read()

GREM_ALWAYS_INLINE void grem::InputFileHandle::read ( Span< byte > data)
inline

Read an exact amount of data from an open file, starting at the current reading position.

The reading position is advanced to the end of the bytes that were read.

Parameters
datawritable span to read file data into. The size of the span determines the number of bytes that are read.
Exceptions
File::Erroron failure to read from the file, or if not all requested data was read.
See also
readUntilEOF()
tryRead()

◆ tryRead()

GREM_ALWAYS_INLINE bool grem::InputFileHandle::tryRead ( Span< byte > data)
inlinenodiscard

Try to read an exact amount of data from an open file, starting at the current reading position.

The reading position is advanced to the end of the bytes that were read.

Parameters
datawritable span to read file data into. The size of the span determines the number of bytes that are attempted to be read.
Returns
true if all bytes were successfully read without hitting the end of the file, false if the end of the file was reached before all data could be read.
Exceptions
File::Erroron failure to read from the file.
See also
readUntilEOF()
read()

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