-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <thiago.macieira@intel.com>
- Loading branch information
1 parent
c35e6ff
commit 0563862
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |