blocxx
BLOCXX_NAMESPACE::File Class Reference

The purpose of the File class is to provide an abstraction layer over the platform dependant functionality related to a file. More...

#include <File.hpp>

Public Types

enum  ELockType { E_READ_LOCK , E_WRITE_LOCK }
 
typedef FileHandle File::* safe_bool
 

Public Member Functions

 File ()
 Create a NULL File object.
 
 File (const File &x)
 Copy constructor.
 
 ~File ()
 Destructor.
 
Fileoperator= (const File &x)
 Assignment operator.
 
void swap (File &x)
 
size_t read (void *bfr, size_t numberOfBytes, Int64 offset=-1L) const
 Read from the underlying file.
 
size_t write (const void *bfr, size_t numberOfBytes, Int64 offset=-1L)
 Write to the underlying file.
 
Int64 seek (Int64 offset, int whence) const
 Seek to a given offset within the file.
 
Int64 tell () const
 
void rewind () const
 Position the file pointer to the beginning of the file.
 
UInt64 size () const
 Current size of file.
 
int close ()
 Close the underlying file object.
 
int flush ()
 Flush any buffered data to the file.
 
int getLock (ELockType type=E_WRITE_LOCK)
 Acquire a kernel lock on the file.
 
int tryLock (ELockType type=E_WRITE_LOCK)
 Acquire a kernel lock on the file.
 
int unlock ()
 Release a lock on the file.
 
 operator safe_bool () const
 
bool operator! () const
 
bool operator== (const File &rhs)
 Equality operator.
 
 File (FileHandle hdl)
 

Private Attributes

FileHandle m_hdl
 

Detailed Description

The purpose of the File class is to provide an abstraction layer over the platform dependant functionality related to a file.

Definition at line 54 of file File.hpp.

Member Typedef Documentation

◆ safe_bool

Returns
true if this is a valid File object.

Definition at line 232 of file File.hpp.

Member Enumeration Documentation

◆ ELockType

Enumerator
E_READ_LOCK 

Flag to place a read / shared lock.

More than one process may hold a shared lock for a given file at a given time.

E_WRITE_LOCK 

Flag to place an write / exclusive lock.

Only one process may hold an exclusive lock for a given file at a given time.

Definition at line 57 of file File.hpp.

Constructor & Destructor Documentation

◆ File() [1/3]

BLOCXX_NAMESPACE::File::File ( )
inline

Create a NULL File object.

Definition at line 76 of file File.hpp.

◆ File() [2/3]

BLOCXX_NAMESPACE::File::File ( const File & x)

Copy constructor.

Parameters
xThe File object to copy.

Definition at line 136 of file PosixFile.cpp.

◆ ~File()

BLOCXX_NAMESPACE::File::~File ( )

Destructor.

Definition at line 180 of file PosixFile.cpp.

References BLOCXX_LOG_ERROR, close(), and m_hdl.

◆ File() [3/3]

BLOCXX_NAMESPACE::File::File ( FileHandle hdl)
inline

Definition at line 244 of file File.hpp.

Member Function Documentation

◆ close()

int BLOCXX_NAMESPACE::File::close ( )
inline

◆ flush()

int BLOCXX_NAMESPACE::File::flush ( )
inline

Flush any buffered data to the file.

Returns
0 on success. Otherwise -1.

Definition at line 185 of file File.hpp.

Referenced by BLOCXX_NAMESPACE::MultiProcessFileAppender::doProcessLogMessage().

◆ getLock()

int BLOCXX_NAMESPACE::File::getLock ( ELockType type = E_WRITE_LOCK)

Acquire a kernel lock on the file.

This call may block.

The lock may be released by calling unlock(). The lock will also be released on when the file is closed or when locking application exits. This is only an advisory lock and will not prevent non-coordinated access to the file.

Parameters
typeWhether to acquire a read (shared) or a write (exclusive) lock. Default is a write lock.
Returns
0 on sucess. On error, -1 is returned, and errno is set appropriately. errno may be one of: EBADF, EDEADLK, EINVAL, ENOLCK.

Definition at line 159 of file PosixFile.cpp.

References E_WRITE_LOCK, and m_hdl.

◆ operator safe_bool()

BLOCXX_NAMESPACE::File::operator safe_bool ( ) const
inline

Definition at line 232 of file File.hpp.

◆ operator!()

bool BLOCXX_NAMESPACE::File::operator! ( ) const
inline

Definition at line 232 of file File.hpp.

◆ operator=()

File & BLOCXX_NAMESPACE::File::operator= ( const File & x)
inline

Assignment operator.

Parameters
xThe File object to copy.
Returns
A reference to this File object.

Definition at line 93 of file File.hpp.

References swap().

◆ operator==()

bool BLOCXX_NAMESPACE::File::operator== ( const File & rhs)
inline

Equality operator.

Parameters
rhsThe File object to compare this object to.
Returns
true if this File represents the same file as rhs.

Definition at line 239 of file File.hpp.

◆ read()

size_t BLOCXX_NAMESPACE::File::read ( void * bfr,
size_t numberOfBytes,
Int64 offset = -1L ) const
inline

Read from the underlying file.

Parameters
bfrThe location where the read operation will place what is read.
numberOfBytesThe number of bytes to read.
offsetThe offset to seek to in the file before the read operation is done or -1 to use the current offset.
Returns
The number of bytes read. If an error occurs, size_t(-1) is returned and 0 on EOF.

Definition at line 113 of file File.hpp.

Referenced by BLOCXX_NAMESPACE::TempFileBuffer::buffer_from_device(), and BLOCXX_NAMESPACE::TempFileEnumerationImplBase::readSize().

◆ rewind()

void BLOCXX_NAMESPACE::File::rewind ( ) const
inline

Position the file pointer to the beginning of the file.

Definition at line 156 of file File.hpp.

◆ seek()

Int64 BLOCXX_NAMESPACE::File::seek ( Int64 offset,
int whence ) const
inline

Seek to a given offset within the file.

Parameters
offsetThe offset to seek to relative to the whence parm.
whenceCan be one of the follwing values: SEEK_SET - Seek relative to the beginning of the file. SEEK_CUR - Seek relative to the current position. SEEK_END - Seek relative to the end of the file (bwd).
Returns
The current location in the file relative to the beginning of the file on success. Other -1.

Definition at line 141 of file File.hpp.

Referenced by BLOCXX_NAMESPACE::TempFileBuffer::overflow(), BLOCXX_NAMESPACE::TempFileEnumerationImplBase::readSize(), BLOCXX_NAMESPACE::TempFileBuffer::rewind(), and BLOCXX_NAMESPACE::TempFileBuffer::underflow().

◆ size()

UInt64 BLOCXX_NAMESPACE::File::size ( ) const
inline

◆ swap()

void BLOCXX_NAMESPACE::File::swap ( File & x)
inline

Definition at line 98 of file File.hpp.

References m_hdl.

Referenced by BLOCXX_NAMESPACE::MultiProcessFileAppender::doProcessLogMessage(), and operator=().

◆ tell()

Int64 BLOCXX_NAMESPACE::File::tell ( ) const
inline
Returns
The current position in the file relative to the beginning of the file on success. Otherwise -1.

Definition at line 149 of file File.hpp.

Referenced by BLOCXX_NAMESPACE::TempFileBuffer::overflow(), and BLOCXX_NAMESPACE::TempFileBuffer::underflow().

◆ tryLock()

int BLOCXX_NAMESPACE::File::tryLock ( ELockType type = E_WRITE_LOCK)

Acquire a kernel lock on the file.

This call won't block.

The lock may be released by calling unlock(). The lock will also be released when the locking application exits. This is only an advisory lock and will not prevent non-coordinated access to the file.

Parameters
typeWhether to try to acquire a read (shared) or a write (exclusive) lock. Default is a write lock.
Returns
0 on sucess. On error, -1 is returned, and errno is set appropriately. errno may be one of: EACCES, EAGAIN, EBADF, EDEADLK, EINVAL, ENOLCK. EACCES or EAGAIN signifies the file is already locked.

Definition at line 166 of file PosixFile.cpp.

References E_WRITE_LOCK, and m_hdl.

Referenced by BLOCXX_NAMESPACE::ServerSocketImpl::doListenUDS().

◆ unlock()

int BLOCXX_NAMESPACE::File::unlock ( )

Release a lock on the file.

This call will not block.

Returns
0 on sucess. On error, -1 is returned, and errno is set appropriately. errno may be one of: EAGAIN, EBADF, EDEADLK, EINVAL, ENOLCK.

Definition at line 173 of file PosixFile.cpp.

References m_hdl.

Referenced by BLOCXX_NAMESPACE::ServerSocketImpl::close().

◆ write()

size_t BLOCXX_NAMESPACE::File::write ( const void * bfr,
size_t numberOfBytes,
Int64 offset = -1L )
inline

Write to the underlying file.

Parameters
bfrThe locaction to get the contents to write.
numberOfBytesThe number of bytes to write.
offsetThe offset to seek to in the file before the write operation is done or -1 to use the current offset.
Returns
The number of bytes written. If an error occurs, size_t(-1) is returned.

Definition at line 127 of file File.hpp.

Referenced by BLOCXX_NAMESPACE::TempFileBuffer::buffer_to_device(), and BLOCXX_NAMESPACE::MultiProcessFileAppender::doProcessLogMessage().

Member Data Documentation

◆ m_hdl

FileHandle BLOCXX_NAMESPACE::File::m_hdl
private

Definition at line 250 of file File.hpp.

Referenced by getLock(), swap(), tryLock(), unlock(), and ~File().


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