Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
|
#include <quniquehandle_p.h>
Public Types | |
using | Type = typename HandleTraits::Type |
Public Member Functions | |
QUniqueHandle ()=default | |
QUniqueHandle (const Type &handle) noexcept | |
QUniqueHandle (QUniqueHandle &&other) noexcept | |
~QUniqueHandle () noexcept | |
QUniqueHandle & | operator= (QUniqueHandle &&rhs) noexcept |
QUniqueHandle (const QUniqueHandle &)=delete | |
QUniqueHandle & | operator= (const QUniqueHandle &)=delete |
bool | isValid () const noexcept |
operator bool () const noexcept | |
Type | get () const noexcept |
void | reset (const Type &handle) noexcept |
Type | release () noexcept |
Type * | operator& () noexcept |
void | close () noexcept |
Friends | |
bool | operator== (const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept |
bool | operator!= (const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept |
bool | operator< (const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept |
bool | operator<= (const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept |
bool | operator> (const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept |
bool | operator>= (const QUniqueHandle &lhs, const QUniqueHandle &rhs) noexcept |
QUniqueHandle is a general purpose RAII wrapper intended for interfacing with resource-allocating C-style APIs, for example operating system APIs, database engine APIs, or any other scenario where resources are allocated and released, and where pointer semantics does not seem a perfect fit.
QUniqueHandle does not support copying, because it is intended to maintain ownership of resources that can not be copied. This makes it safer to use than naked handle types, since ownership is maintained by design.
The underlying handle object is described using a client supplied HandleTraits object that is implemented per resource type. The traits struct must describe two properties of a handle:
1) What value is considered invalid 2) How to close a resource.
Example 1:
struct InvalidHandleTraits { using Type = HANDLE; static Type invalidValue() { return INVALID_HANDLE_VALUE; } static bool close(Type handle) { return CloseHandle(handle) != 0; } } using FileHandle = QUniqueHandle<InvalidHandleTraits>;
Usage:
Takes ownership of returned handle. FileHandle handle{ CreateFile(...) };
if (!handle.isValid()) { qDebug() << GetLastError() return; }
...
Example 2:
struct SqLiteTraits { using Type = sqlite3*; static Type invalidValue() { return nullptr; } static bool close(Type handle) { sqlite3_close(handle); return true; } } using DbHandle = QUniqueHandle<SqLiteTraits>;
Usage:
DbHandle h;
Take ownership of returned handle. int result = sqlite3_open(":memory:", &h);
...
NOTE: The QUniqueHandle assumes that closing a resource is guaranteed to succeed, and provides no support for handling failure to close a resource. It is therefore only recommended for use cases where failure to close a resource is either not an error, or an unrecoverable error.
Definition at line 107 of file quniquehandle_p.h.
using QUniqueHandle< HandleTraits >::Type = typename HandleTraits::Type |
Definition at line 110 of file quniquehandle_p.h.
|
default |
|
inlineexplicitnoexcept |
Definition at line 114 of file quniquehandle_p.h.
|
inlinenoexcept |
Definition at line 118 of file quniquehandle_p.h.
References other().
|
inlinenoexcept |
Definition at line 122 of file quniquehandle_p.h.
References QUniqueHandle< HandleTraits >::close().
|
delete |
|
inlinenoexcept |
Definition at line 176 of file quniquehandle_p.h.
References QUniqueHandle< HandleTraits >::isValid(), and Q_ASSERT.
Referenced by QUniqueHandle< HandleTraits >::~QUniqueHandle(), and QUniqueHandle< HandleTraits >::reset().
|
inlinenoexcept |
Definition at line 149 of file quniquehandle_p.h.
Referenced by QGstreamerVideoDevices::QGstreamerVideoDevices(), QGstreamerVideoDevices::~QGstreamerVideoDevices(), QGString::asStringView(), MFPlayerSession::close(), Scheduler::flush(), MFPlayerSession::Invoke(), QGstImpl::QSharedHandle< HandleTraits >::operator=(), QGstreamerAudioDecoder::processBusMessage(), QGstreamerMediaPlayer::processBusMessage(), QGstreamerMediaEncoder::processBusMessage(), Scheduler::scheduleSample(), Scheduler::startScheduler(), Scheduler::stopScheduler(), and QGString::toQString().
|
inlinenoexcept |
Definition at line 139 of file quniquehandle_p.h.
Referenced by QUniqueHandle< HandleTraits >::close(), QUniqueHandle< HandleTraits >::operator bool(), and QUniqueHandle< HandleTraits >::operator&().
|
inlineexplicitnoexcept |
Definition at line 144 of file quniquehandle_p.h.
References QUniqueHandle< HandleTraits >::isValid().
|
inlinenoexcept |
Definition at line 170 of file quniquehandle_p.h.
References QUniqueHandle< HandleTraits >::isValid(), and Q_ASSERT.
|
delete |
|
inlinenoexcept |
Definition at line 127 of file quniquehandle_p.h.
|
inlinenoexcept |
Definition at line 163 of file quniquehandle_p.h.
|
inlinenoexcept |
Definition at line 154 of file quniquehandle_p.h.
References QUniqueHandle< HandleTraits >::close().
|
friend |
Definition at line 192 of file quniquehandle_p.h.
|
friend |
Definition at line 197 of file quniquehandle_p.h.
|
friend |
Definition at line 202 of file quniquehandle_p.h.
|
friend |
Definition at line 187 of file quniquehandle_p.h.
|
friend |
Definition at line 207 of file quniquehandle_p.h.
|
friend |
Definition at line 212 of file quniquehandle_p.h.