1// Copyright (C) 2020 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#ifndef QT3DCORE_QHANDLE_P_H
4#define QT3DCORE_QHANDLE_P_H
5
6//
7// W A R N I N G
8// -------------
9//
10// This file is not part of the Qt API. It exists for the convenience
11// of other Qt classes. This header file may change from version to
12// version without notice, or even be removed.
13//
14// We mean it.
15//
16
17#include <Qt3DCore/qt3dcore_global.h>
18#include <QtCore/QDebug>
19#include <QtCore/qhashfunctions.h>
20#include <private/qglobal_p.h>
21
22QT_BEGIN_NAMESPACE
23
24namespace Qt3DCore {
25
26template <typename T>
27class QHandle
28{
29public:
30 struct Data {
31 union {
32 quintptr counter;
33 Data *nextFree;
34 };
35 };
36 QHandle()
37 : d(nullptr),
38 counter(0)
39 {}
40 QHandle(Data *d)
41 : d(d),
42 counter(d->counter)
43 {
44 }
45 QHandle(const QHandle &other)
46 : d(other.d),
47 counter(other.counter)
48 {
49 }
50 QHandle &operator=(const QHandle &other)
51 {
52 d = other.d;
53 counter = other.counter;
54 return *this;
55 }
56
57 inline T *operator->() const;
58 T *data() const;
59
60 quintptr handle() const { return reinterpret_cast<quintptr>(d); }
61 bool isNull() const { return !d; }
62
63 Data *data_ptr() const { return d; }
64
65 bool operator==(const QHandle &other) const { return d == other.d && counter == other.counter; }
66 bool operator!=(const QHandle &other) const { return !operator==(other); }
67private:
68 Data *d;
69 quintptr counter;
70};
71
72
73template <typename T>
74QDebug operator<<(QDebug dbg, const QHandle<T> &h)
75{
76 QDebugStateSaver saver(dbg);
77 QString binNumber = QString::number(h.handle(), 2).rightJustified(32, QChar::fromLatin1(c: '0'));
78 dbg.nospace() << " m_handle = " << h.handle()
79 << " = " << binNumber;
80 return dbg;
81}
82
83template <typename T>
84size_t qHash(const QHandle<T> &h, size_t seed)
85{
86 using QT_PREPEND_NAMESPACE(qHash);
87 return qHash(h.handle(), seed);
88}
89
90template <typename T>
91size_t qHash(const QHandle<T> &h)
92{
93 using QT_PREPEND_NAMESPACE(qHash);
94 return qHash(h.handle());
95}
96
97} // Qt3DCore
98
99// simpler than fighting the Q_DECLARE_TYPEINFO macro, use QString as a dummy to get movable semantics
100template <typename T>
101class QTypeInfo<Qt3DCore::QHandle<T> >
102 : public QTypeInfoMerger<Qt3DCore::QHandle<T>, QString> {};
103
104QT_END_NAMESPACE
105
106#endif // QT3DCORE_QRHANDLE_H
107

source code of qt3d/src/core/resources/qhandle_p.h