xentara-utils v2.0.4
The Xentara Utility Library
|
A Windows handle that closes itself on destruct. More...
#include <xentara/utils/windows/Handle.hpp>
Classes | |
class | Setter |
Helper class for calling functions that take an output parameter. More... | |
Public Types | |
using | NativeType = HANDLE |
A native Windows handle. | |
Public Member Functions | |
constexpr | Handle () noexcept=default |
Default constructor. | |
constexpr | Handle (HANDLE nativeHandle) noexcept |
Conversion from a native handle. | |
constexpr | Handle (Handle &&other) noexcept |
Move constructor. | |
~Handle () | |
Destructor. Silently closes the handle. | |
auto | operator= (Handle &&rhs) noexcept -> Handle & |
Move assignment operator. | |
operator HANDLE () const | |
Conversion to a native handle. | |
constexpr | operator bool () const noexcept |
Checks whether the object contains a handle. | |
auto | operator= (HANDLE nativeHandle) noexcept -> Handle & |
Assignment from a native handle. | |
constexpr auto | release () noexcept -> HANDLE |
Removes the handle and passes ownership to the caller. | |
auto | close () noexcept -> bool |
Closes the handle, if any. | |
auto | setter () noexcept -> Setter |
Gets a setter for use as an output parameter for C functions. | |
auto | performOverlappedIo (functional::function_ref< auto(HANDLE, OVERLAPPED *) → BOOL > operation, HANDLE event, std::optional< std::chrono::nanoseconds > timeout=std::nullopt) -> std::optional< DWORD > |
Performs an overlapped I/O operation. | |
auto | performOverlappedIo (functional::function_ref< auto(HANDLE, OVERLAPPED *) → BOOL > operation, std::span< HANDLE > waitObjects, std::optional< std::chrono::nanoseconds > timeout=std::nullopt) -> std::optional< OverlappedResult > |
Performs an overlapped I/O operation. | |
Static Public Attributes | |
static const HANDLE | kInvalid { INVALID_HANDLE_VALUE } |
An invalid handle. | |
A Windows handle that closes itself on destruct.
This class can be moved but not copied.
Windows file functions, like CreateFile2() return INVALID_HANDLE_VALUE (-1) if they fail. Others functions, like CreateEvent() return NULL (nullptr). This class always uses INVALID_HANDLE_VALUE to indicate an invalid handle, and will replace native handle values of nullptr with INVALID_HANDLE_VALUE.
using xentara::utils::windows::Handle::NativeType = HANDLE |
A native Windows handle.
|
constexprdefaultnoexcept |
Default constructor.
Creates an empty object that does not contain a handle.
|
constexprnoexcept |
Conversion from a native handle.
nativeHandle | The native handle, or INVALID_HANDLE_VALUE for none. nullptr is also accepted as an invalid handle, and will be treated the same as INVALID_HANDLE_VALUE. |
|
constexprnoexcept |
Move constructor.
Takes ownership of the other object’s handle, if any.
other | The object to move. Will be reset and no longer contain a handle afterwards. |
xentara::utils::windows::Handle::~Handle | ( | ) |
Destructor. Silently closes the handle.
|
noexcept |
Closes the handle, if any.
|
explicitconstexprnoexcept |
Checks whether the object contains a handle.
xentara::utils::windows::Handle::operator HANDLE | ( | ) | const |
Conversion to a native handle.
Move assignment operator.
Takes ownership of the other handle’s value. Any existing handle is silently closed.
rhs | The object to move. Will be reset and no longer contain a handle afterwards. |
|
noexcept |
Assignment from a native handle.
Any existing handle is silently closed.
nativeHandle | The native handle, or INVALID_HANDLE_VALUE for none. nullptr is also accepted as an invalid handle, and will be treated the same as INVALID_HANDLE_VALUE. |
auto xentara::utils::windows::Handle::performOverlappedIo | ( | functional::function_ref< auto(HANDLE, OVERLAPPED *) → BOOL > | operation, |
HANDLE | event, | ||
std::optional< std::chrono::nanoseconds > | timeout = std::nullopt |
||
) | -> std::optional< DWORD > |
Performs an overlapped I/O operation.
This function creates an OVERLAPPED structure and calls a function object with it. The function object must call a Windows overlapped I/O function like ReadFile() or WriteFile(), passing the overlapped structure to it, and return the result. The function must also preserve the error code stored in GetLastError(), specifically the error code ERROR_IO_PENDING, which the function uses to determine if waiting is necessary.
operation | The operation to perform. operation will be called with the handle and an overlapped structure as parameters. It must return TRUE on success or FALSE on error. If FALSE is returned, the last error code must be set to an appropriate error. If an overlapped I/O operation is pending, the function must return FALSE and set the last error to ERROR_IO_PENDING in the manner of overlapped I/O functions like ReadFile() and WriteFile(). |
event | The event to use in the OVERLAPPED structure. |
timeout | The timeout, or std::nullopt for infinite. Negative timeouts are treated the same as a timeout of 0. |
Returns the number of bytes transferred on success, or std::nullopt on error. To get extended error information, call GetLastError(). On timeout, the last error is set to ERROR_OPERATION_ABORTED.
The meaning of “number of bytes transferred” depends on the overlapped operation. See the documentation of the lpNumberOfBytesTransferred parameter of GetOverlappedResult().
auto xentara::utils::windows::Handle::performOverlappedIo | ( | functional::function_ref< auto(HANDLE, OVERLAPPED *) → BOOL > | operation, |
std::span< HANDLE > | waitObjects, | ||
std::optional< std::chrono::nanoseconds > | timeout = std::nullopt |
||
) | -> std::optional< OverlappedResult > |
Performs an overlapped I/O operation.
This function creates an OVERLAPPED structure and calls a function object with it. The function object must call a Windows overlapped I/O function like ReadFile() or WriteFile(), passing the overlapped structure to it, and return the result. The function must also preserve the error code stored in GetLastError(), specifically the error code ERROR_IO_PENDING, which the function uses to determine if waiting is necessary.
operation | The operation to perform. operation will be called with the handle and an overlapped structure as parameters. It must return TRUE on success or FALSE on error. If FALSE is returned, the last error code must be set to an appropriate error. If an overlapped I/O operation is pending, the function must return FALSE and set the last error to ERROR_IO_PENDING in the manner of overlapped I/O functions like ReadFile() and WriteFile(). |
waitObjects | The objects to wait for. The first position in the span must contain the event to use in the OVERLAPPED structure. |
timeout | The timeout, or std::nullopt for infinite. Negative timeouts are treated the same as a timeout of 0. |
std::invalid_argument | waitObjects is empty, or contains more objects than Windows can handle. |
|
constexprnoexcept |
Removes the handle and passes ownership to the caller.
After this call, this object will no longer contain a handle.
|
noexcept |
Gets a setter for use as an output parameter for C functions.
This function returns a helper object that can be passed to a function that takes an LPHANDLE as output parameter, like e.g. DuplicateHandle(). This provides a safe way of creating kernel objects using such a function without running the risk of leaking the created objects.
DuplicateHandle() can be called like this, for example:
|
static |
An invalid handle.