xentara-utils v2.0.3
The Xentara Utility Library
Loading...
Searching...
No Matches
xentara::utils::posix::File Class Referencefinal

A Posix file descriptor that closes itself on destruct. More...

#include <xentara/utils/posix/File.hpp>

+ Inheritance diagram for xentara::utils::posix::File:

Public Types

using NativeType = int
 A native Posix file descriptor.
 

Public Member Functions

constexpr File () noexcept=default
 Default constructor.
 
constexpr File (NativeType fileDescriptor) noexcept
 Conversion from a native file descriptor.
 
constexpr File (File &&other) noexcept
 Move constructor.
 
 ~File ()
 Destructor. Silently closes the file.
 
auto operator= (File &&rhs) noexcept -> File &
 Move assignment operator.
 
constexpr operator NativeType () const noexcept
 Conversion to a native file descriptor.
 
constexpr operator bool () const noexcept
 Checks whether the object contains a file descriptor.
 
auto operator= (NativeType fileDescriptor) noexcept -> File &
 Assignment from a native file descriptor.
 
constexpr auto release () noexcept -> NativeType
 Removes the file descriptor and passes ownership to the caller.
 
auto close () noexcept -> bool
 Closes the file descriptor, if any.
 

Static Public Attributes

static constexpr int kInvalid { -1 }
 An invalid file descriptor.
 

Detailed Description

A Posix file descriptor that closes itself on destruct.

This class can be moved but not copied.

Note
This class is not available under Windows

Member Typedef Documentation

◆ NativeType

A native Posix file descriptor.

Constructor & Destructor Documentation

◆ File() [1/3]

constexpr xentara::utils::posix::File::File ( )
constexprdefaultnoexcept

Default constructor.

Creates an empty object that does not contain a file descriptor.

◆ File() [2/3]

constexpr xentara::utils::posix::File::File ( NativeType  fileDescriptor)
constexprnoexcept

Conversion from a native file descriptor.

Parameters
fileDescriptorThe file descriptor, or -1 for none.

◆ File() [3/3]

constexpr xentara::utils::posix::File::File ( File &&  other)
constexprnoexcept

Move constructor.

Takes ownership of the other object's file descriptor, if any.

Parameters
otherThe object to move. Will be reset and no longer contain a file descriptor afterwards.

◆ ~File()

xentara::utils::posix::File::~File ( )

Destructor. Silently closes the file.

Member Function Documentation

◆ close()

auto xentara::utils::posix::File::close ( ) -> bool
noexcept

Closes the file descriptor, if any.

Returns
Returns true on success, or false on failure. The file descriptor will be closed in either case, though.

◆ operator bool()

constexpr xentara::utils::posix::File::operator bool ( ) const
explicitconstexprnoexcept

Checks whether the object contains a file descriptor.

◆ operator NativeType()

constexpr xentara::utils::posix::File::operator NativeType ( ) const
constexprnoexcept

Conversion to a native file descriptor.

Returns
The contained file descriptor, or -1 for none.

◆ operator=() [1/2]

auto xentara::utils::posix::File::operator= ( File &&  rhs) -> File &
noexcept

Move assignment operator.

Takes ownership of the other object's file descriptor, if any. Any existing file descriptor is silently closed.

Parameters
rhsThe object to move. Will be reset and no longer contain a file descriptor afterwards.

◆ operator=() [2/2]

auto xentara::utils::posix::File::operator= ( NativeType  fileDescriptor) -> File &
noexcept

Assignment from a native file descriptor.

Any existing file descriptor is silently closed.

Parameters
fileDescriptorThe new file descriptor, or -1 for none.

◆ release()

constexpr auto xentara::utils::posix::File::release ( ) -> NativeType
constexprnoexcept

Removes the file descriptor and passes ownership to the caller.

After this call, this object will no longer contain a file descriptor.

Returns
The file descriptor, or -1 for none. The caller must assume ownership of the file descriptor and close it when it is no longer needed.

Member Data Documentation

◆ kInvalid

constexpr int xentara::utils::posix::File::kInvalid { -1 }
staticconstexpr

An invalid file descriptor.