Skip to content

Commit

Permalink
Core: add some commonly used QUniqueHandle types
Browse files Browse the repository at this point in the history
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
timblechmann committed Oct 2, 2024
1 parent c35e6ff commit 0563862
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/corelib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions src/corelib/tools/quniquehandle_types.cpp
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
81 changes: 81 additions & 0 deletions src/corelib/tools/quniquehandle_types_p.h
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

0 comments on commit 0563862

Please sign in to comment.