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*

Change-Id: I4d6af274bcc7f84ae009d0cb3a2f3aec1b6bcaf9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 0563862e23eda4a7b8def8c8afacf732b9a992ef)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tim Blechmann 2024-08-30 12:46:04 +08:00 committed by Qt Cherry-pick Bot
parent 42439087eb
commit 29983de830
3 changed files with 134 additions and 0 deletions

View File

@ -311,6 +311,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

View File

@ -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 <QtCore/private/quniquehandle_types_p.h>
#include "qplatformdefs.h" // For QT_CLOSE
#ifdef Q_OS_WIN
# include <QtCore/qt_windows.h>
#endif
#ifdef Q_OS_UNIX
# include <QtCore/private/qcore_unix_p.h> // 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<QtUniqueHandleTraits::FileDescriptorHandleTraits>;
#endif
QT_END_NAMESPACE

View File

@ -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 <QtCore/qglobal.h>
#include <QtCore/private/quniquehandle_p.h>
#include <cstdio>
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<QtUniqueHandleTraits::InvalidHandleTraits>;
using QUniqueWin32NullHandle = QUniqueHandle<QtUniqueHandleTraits::NullHandleTraits>;
#endif
#ifdef Q_OS_UNIX
using QUniqueFileDescriptorHandle = QUniqueHandle<QtUniqueHandleTraits::FileDescriptorHandleTraits>;
#endif
using QUniqueFILEHandle = QUniqueHandle<QtUniqueHandleTraits::FILEHandleTraits>;
QT_END_NAMESPACE
#endif