1 | // Copyright (C) 2018 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QRECURSIONWATCHER_P_H |
5 | #define QRECURSIONWATCHER_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtCore/private/qglobal_p.h> |
19 | |
20 | QT_BEGIN_NAMESPACE |
21 | |
22 | class QRecursionNode; |
23 | class QRecursionNode { |
24 | public: |
25 | inline QRecursionNode(); |
26 | bool *_r; |
27 | }; |
28 | |
29 | template<class T, QRecursionNode T::*Node> |
30 | class QRecursionWatcher { |
31 | public: |
32 | inline QRecursionWatcher(T *); |
33 | inline ~QRecursionWatcher(); |
34 | inline bool hasRecursed() const; |
35 | private: |
36 | T *_t; |
37 | bool _r; |
38 | }; |
39 | |
40 | QRecursionNode::QRecursionNode() |
41 | : _r(nullptr) |
42 | { |
43 | } |
44 | |
45 | template<class T, QRecursionNode T::*Node> |
46 | QRecursionWatcher<T, Node>::QRecursionWatcher(T *t) |
47 | : _t(t), _r(false) |
48 | { |
49 | if ((_t->*Node)._r) *(_t->*Node)._r = true; |
50 | (_t->*Node)._r = &_r; |
51 | } |
52 | |
53 | template<class T, QRecursionNode T::*Node> |
54 | QRecursionWatcher<T, Node>::~QRecursionWatcher() |
55 | { |
56 | if ((_t->*Node)._r == &_r) (_t->*Node)._r = nullptr; |
57 | } |
58 | |
59 | template<class T, QRecursionNode T::*Node> |
60 | bool QRecursionWatcher<T, Node>::hasRecursed() const |
61 | { |
62 | return _r; |
63 | } |
64 | |
65 | QT_END_NAMESPACE |
66 | |
67 | #endif // QRECURSIONWATCHER_P_H |
68 | |