From 0563862e23eda4a7b8def8c8afacf732b9a992ef Mon Sep 17 00:00:00 2001 From: Tim Blechmann Date: Fri, 30 Aug 2024 12:46:04 +0800 Subject: [PATCH] Core: add some commonly used QUniqueHandle types QUniqueHandle allows us to write RAII types for non-trivial classes. We can add some specialisations for 2 types of win32 handles, file descriptors and FILE* Pick-to: 6.8 Change-Id: I4d6af274bcc7f84ae009d0cb3a2f3aec1b6bcaf9 Reviewed-by: Thiago Macieira --- src/corelib/CMakeLists.txt | 1 + src/corelib/tools/quniquehandle_types.cpp | 52 +++++++++++++++ src/corelib/tools/quniquehandle_types_p.h | 81 +++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/corelib/tools/quniquehandle_types.cpp create mode 100644 src/corelib/tools/quniquehandle_types_p.h diff --git a/src/corelib/CMakeLists.txt b/src/corelib/CMakeLists.txt index cb466d389b8..ac82ca87827 100644 --- a/src/corelib/CMakeLists.txt +++ b/src/corelib/CMakeLists.txt @@ -316,6 +316,7 @@ qt_internal_add_module(Core tools/qtools_p.h tools/qtyperevision.cpp tools/qtyperevision.h tools/quniquehandle_p.h + tools/quniquehandle_types.cpp tools/quniquehandle_types_p.h tools/qvarlengtharray.h tools/qvector.h tools/qversionnumber.cpp tools/qversionnumber.h diff --git a/src/corelib/tools/quniquehandle_types.cpp b/src/corelib/tools/quniquehandle_types.cpp new file mode 100644 index 00000000000..59b9ebe179c --- /dev/null +++ b/src/corelib/tools/quniquehandle_types.cpp @@ -0,0 +1,52 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include + +#include "qplatformdefs.h" // For QT_CLOSE + +#ifdef Q_OS_WIN +# include +#endif + +#ifdef Q_OS_UNIX +# include // for qt_safe_close +#endif + +QT_BEGIN_NAMESPACE + +namespace QtUniqueHandleTraits { + +#ifdef Q_OS_WIN + +bool InvalidHandleTraits::close(Type handle) +{ + return ::CloseHandle(handle); +} + +bool NullHandleTraits::close(Type handle) +{ + return ::CloseHandle(handle); +} + +#endif + +bool FileDescriptorHandleTraits::close(Type handle) +{ + return QT_CLOSE(handle) == 0; +} + +bool FILEHandleTraits::close(Type handle) +{ + return ::fclose(handle); +} + +} // namespace QtUniqueHandleTraits + +#ifdef Q_OS_UNIX + +using QUniqueFileDescriptorHandle = QUniqueHandle; + +#endif + +QT_END_NAMESPACE diff --git a/src/corelib/tools/quniquehandle_types_p.h b/src/corelib/tools/quniquehandle_types_p.h new file mode 100644 index 00000000000..bd82b053afb --- /dev/null +++ b/src/corelib/tools/quniquehandle_types_p.h @@ -0,0 +1,81 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QUNIQUEHANDLE_TYPES_P_H +#define QUNIQUEHANDLE_TYPES_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include + +#include + +QT_BEGIN_NAMESPACE + +namespace QtUniqueHandleTraits { + +#ifdef Q_OS_WIN + +struct InvalidHandleTraits +{ + using Type = Qt::HANDLE; + static Type invalidValue() noexcept + { + return Qt::HANDLE(-1); // AKA INVALID_HANDLE_VALUE + } + Q_CORE_EXPORT static bool close(Type handle); +}; + +struct NullHandleTraits +{ + using Type = Qt::HANDLE; + static Type invalidValue() noexcept { return nullptr; } + Q_CORE_EXPORT static bool close(Type handle); +}; + +#endif + +struct FileDescriptorHandleTraits +{ + using Type = int; + static constexpr Type invalidValue() noexcept { return -1; } + Q_CORE_EXPORT static bool close(Type handle); +}; + +struct FILEHandleTraits +{ + using Type = FILE *; + static constexpr Type invalidValue() noexcept { return nullptr; } + Q_CORE_EXPORT static bool close(Type handle); +}; + +} // namespace QtUniqueHandleTraits + +#ifdef Q_OS_WIN + +using QUniqueWin32Handle = QUniqueHandle; +using QUniqueWin32NullHandle = QUniqueHandle; + +#endif + +#ifdef Q_OS_UNIX + +using QUniqueFileDescriptorHandle = QUniqueHandle; + +#endif + +using QUniqueFILEHandle = QUniqueHandle; + +QT_END_NAMESPACE + +#endif