Class FileHandleManager::FileGuard

Nested Relationships

This class is a nested type of Class FileHandleManager.

Class Documentation

class FileGuard

RAII guard for automatic file handle management.

The FileGuard class provides automatic file handle acquisition and release using RAII principles. It ensures that file handles are properly released even if exceptions occur during file processing.

Features

  • Automatic handle release in destructor

  • Move semantics for efficient transfer

  • Non-copyable to prevent handle duplication

  • Status checking to verify successful acquisition

Note

FileGuard objects should be short-lived and created immediately before file operations to minimize handle holding time

Public Functions

explicit FileGuard(FileHandleManager *mgr)

Constructor - attempts to acquire file handle.

Attempts to acquire a file handle from the manager. Check is_acquired() to determine if the acquisition was successful.

Parameters:

mgr – Pointer to the FileHandleManager

~FileGuard()

Destructor - automatically releases file handle.

Releases the file handle back to the manager if it was successfully acquired. This ensures proper cleanup even in exception scenarios.

FileGuard(const FileGuard&) = delete

Copy constructor (deleted)

FileGuard is non-copyable to prevent accidental handle duplication which could lead to resource leaks or double-release errors.

FileGuard &operator=(const FileGuard&) = delete

Copy assignment operator (deleted)

FileGuard is non-copyable to prevent accidental handle duplication which could lead to resource leaks or double-release errors.

FileGuard(FileGuard &&other) noexcept

Move constructor for efficient transfer.

Transfers ownership of the file handle from another FileGuard. The source guard becomes invalid after the move.

Parameters:

otherFileGuard to move from

FileGuard &operator=(FileGuard &&other) noexcept

Move assignment operator for efficient transfer.

Transfers ownership of the file handle from another FileGuard. Releases any currently held handle before acquiring the new one.

Parameters:

otherFileGuard to move from

Returns:

Reference to this FileGuard

inline bool is_acquired() const

Check if file handle was successfully acquired.

Always check this before attempting file operations to ensure a handle is available. If false, defer the operation or retry later.

Returns:

true if handle is available for use, false otherwise