Miam-Player  0.8.0
A nice music player
QtLP_Private::QtLockedFile Class Reference

The QtLockedFile class extends QFile with advisory locking functions. More...

#include <qtlockedfile.h>

Inheritance diagram for QtLP_Private::QtLockedFile:

Public Types

enum  LockMode { NoLock = 0, ReadLock, WriteLock }
 This enum describes the available lock modes. More...
 

Public Member Functions

 QtLockedFile ()
 Constructs an unlocked QtLockedFile object. More...
 
 QtLockedFile (const QString &name)
 Constructs an unlocked QtLockedFile object with file name. More...
 
 ~QtLockedFile ()
 Destroys the QtLockedFile object. More...
 
bool open (OpenMode mode)
 Opens the file in OpenMode mode. More...
 
bool lock (LockMode mode, bool block=true)
 Obtains a lock of type mode. More...
 
bool unlock ()
 Releases a lock. More...
 
bool isLocked () const
 Returns true if this object has a in read or write lock; otherwise returns false. More...
 
LockMode lockMode () const
 Returns the type of lock currently held by this object, or QtLockedFile::NoLock. More...
 

Detailed Description

The QtLockedFile class extends QFile with advisory locking functions.

A file may be locked in read or write mode. Multiple instances of QtLockedFile, created in multiple processes running on the same machine, may have a file locked in read mode. Exactly one instance may have it locked in write mode. A read and a write lock cannot exist simultaneously on the same file.

The file locks are advisory. This means that nothing prevents another process from manipulating a locked file using QFile or file system functions offered by the OS. Serialization is only guaranteed if all processes that access the file use QLockedFile. Also, while holding a lock on a file, a process must not open the same file again (through any API), or locks can be unexpectedly lost.

The lock provided by an instance of QtLockedFile is released whenever the program terminates. This is true even when the program crashes and no destructors are called.

Member Enumeration Documentation

This enum describes the available lock modes.

ReadLock A read lock. WriteLock A write lock. NoLock Neither a read lock nor a write lock.

Enumerator
NoLock 
ReadLock 
WriteLock 

Constructor & Destructor Documentation

QtLockedFile::QtLockedFile ( )

Constructs an unlocked QtLockedFile object.

This constructor behaves in the same way as QFile::QFile().

See also
QFile::QFile()
QtLockedFile::QtLockedFile ( const QString &  name)

Constructs an unlocked QtLockedFile object with file name.

This constructor behaves in the same way as QFile::QFile(const QString&).

See also
QFile::QFile()
QtLockedFile::~QtLockedFile ( )

Destroys the QtLockedFile object.

If any locks were held, they are released.

Member Function Documentation

bool QtLockedFile::isLocked ( ) const

Returns true if this object has a in read or write lock; otherwise returns false.

See also
lockMode()
bool QtLockedFile::lock ( LockMode  mode,
bool  block = true 
)

Obtains a lock of type mode.

The file must be opened before it can be locked.

If block is true, this function will block until the lock is aquired. If block is false, this function returns false immediately if the lock cannot be aquired.

If this object already has a lock of type mode, this function returns true immediately. If this object has a lock of a different type than mode, the lock is first released and then a new lock is obtained.

This function returns true if, after it executes, the file is locked by this object, and false otherwise.

See also
unlock(), isLocked(), lockMode()
QtLockedFile::LockMode QtLockedFile::lockMode ( ) const

Returns the type of lock currently held by this object, or QtLockedFile::NoLock.

See also
isLocked()
bool QtLockedFile::open ( OpenMode  mode)

Opens the file in OpenMode mode.

This is identical to QFile::open(), with the one exception that the Truncate mode flag is disallowed. Truncation would conflict with the advisory file locking, since the file would be modified before the write lock is obtained. If truncation is required, use resize(0) after obtaining the write lock.

Returns true if successful; otherwise false.

See also
QFile::open(), QFile::resize()
bool QtLockedFile::unlock ( )

Releases a lock.

If the object has no lock, this function returns immediately.

This function returns true if, after it executes, the file is not locked by this object, and false otherwise.

See also
lock(), isLocked(), lockMode()

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