blocxx
Public Types | Public Member Functions | Private Attributes | List of all members
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. More...
 
 File (const File &x)
 Copy constructor. More...
 
 ~File ()
 Destructor. More...
 
Fileoperator= (const File &x)
 Assignment operator. More...
 
void swap (File &x)
 
size_t read (void *bfr, size_t numberOfBytes, Int64 offset=-1L) const
 Read from the underlying file. More...
 
size_t write (const void *bfr, size_t numberOfBytes, Int64 offset=-1L)
 Write to the underlying file. More...
 
Int64 seek (Int64 offset, int whence) const
 Seek to a given offset within the file. More...
 
Int64 tell () const
 
void rewind () const
 Position the file pointer to the beginning of the file. More...
 
UInt64 size () const
 Current size of file. More...
 
int close ()
 Close the underlying file object. More...
 
int flush ()
 Flush any buffered data to the file. More...
 
int getLock (ELockType type=E_WRITE_LOCK)
 Acquire a kernel lock on the file. More...
 
int tryLock (ELockType type=E_WRITE_LOCK)
 Acquire a kernel lock on the file. More...
 
int unlock ()
 Release a lock on the file. More...
 
 operator safe_bool () const
 
bool operator! () const
 
bool operator== (const File &rhs)
 Equality operator. More...
 
 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 84 of file File.hpp.

Member Typedef Documentation

◆ safe_bool

Returns
true if this is a valid File object.

Definition at line 292 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 117 of file File.hpp.

Constructor & Destructor Documentation

◆ File() [1/3]

BLOCXX_NAMESPACE::File::File ( )
inline

Create a NULL File object.

Definition at line 136 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 166 of file PosixFile.cpp.

References E_WRITE_LOCK, and m_hdl.

◆ ~File()

BLOCXX_NAMESPACE::File::~File ( )

Destructor.

Definition at line 210 of file PosixFile.cpp.

◆ File() [3/3]

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

Definition at line 304 of file File.hpp.

Member Function Documentation

◆ close()

int BLOCXX_NAMESPACE::File::close ( )
inline

Close the underlying file object.

Returns
0 on success. Otherwise -1.

Definition at line 231 of file File.hpp.

Referenced by BLOCXX_NAMESPACE::ServerSocketImpl::close(), BLOCXX_NAMESPACE::MultiProcessFileAppender::doProcessLogMessage(), and BLOCXX_NAMESPACE::TempFileBuffer::rewind().

◆ flush()

int BLOCXX_NAMESPACE::File::flush ( )
inline

Flush any buffered data to the file.

Returns
0 on success. Otherwise -1.

Definition at line 245 of file File.hpp.

◆ 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 189 of file PosixFile.cpp.

◆ operator safe_bool()

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

Definition at line 292 of file File.hpp.

◆ operator!()

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

Definition at line 292 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 153 of file File.hpp.

◆ 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 299 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 173 of file File.hpp.

References BLOCXX_INVALID_FILEHANDLE, and BLOCXX_NAMESPACE::FileSystem::close().

◆ rewind()

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

Position the file pointer to the beginning of the file.

Definition at line 216 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 201 of file File.hpp.

◆ size()

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

Current size of file.

Definition at line 223 of file File.hpp.

◆ swap()

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

◆ 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 209 of file File.hpp.

◆ 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 196 of file PosixFile.cpp.

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 203 of file PosixFile.cpp.

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 187 of file File.hpp.

Member Data Documentation

◆ m_hdl

FileHandle BLOCXX_NAMESPACE::File::m_hdl
private

Definition at line 310 of file File.hpp.

Referenced by File().


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