1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QSTATEMACHINE_P_H
41#define QSTATEMACHINE_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include "private/qstate_p.h"
55
56#include <QtCore/qcoreevent.h>
57#include <QtCore/qhash.h>
58#include <QtCore/qlist.h>
59#include <QtCore/qmutex.h>
60#include <QtCore/qpair.h>
61#include <QtCore/qpointer.h>
62#include <QtCore/qset.h>
63#include <QtCore/qvector.h>
64#include <private/qfreelist_p.h>
65
66QT_REQUIRE_CONFIG(statemachine);
67
68QT_BEGIN_NAMESPACE
69
70class QEvent;
71#if QT_CONFIG(qeventtransition)
72class QEventTransition;
73#endif
74class QSignalEventGenerator;
75class QSignalTransition;
76class QAbstractState;
77class QAbstractTransition;
78class QFinalState;
79class QHistoryState;
80class QState;
81
82#if QT_CONFIG(animation)
83class QAbstractAnimation;
84#endif
85
86struct CalculationCache;
87class QStateMachine;
88class Q_CORE_EXPORT QStateMachinePrivate : public QStatePrivate
89{
90 Q_DECLARE_PUBLIC(QStateMachine)
91public:
92 enum State {
93 NotRunning,
94 Starting,
95 Running
96 };
97 enum EventProcessingMode {
98 DirectProcessing,
99 QueuedProcessing
100 };
101 enum StopProcessingReason {
102 EventQueueEmpty,
103 Finished,
104 Stopped
105 };
106
107 QStateMachinePrivate();
108 ~QStateMachinePrivate();
109
110 static QStateMachinePrivate *get(QStateMachine *q)
111 { return q ? q->d_func() : nullptr; }
112
113 QState *findLCA(const QList<QAbstractState*> &states, bool onlyCompound = false);
114 QState *findLCCA(const QList<QAbstractState*> &states);
115
116 static bool transitionStateEntryLessThan(QAbstractTransition *t1, QAbstractTransition *t2);
117 static bool stateEntryLessThan(QAbstractState *s1, QAbstractState *s2);
118 static bool stateExitLessThan(QAbstractState *s1, QAbstractState *s2);
119
120 QAbstractState *findErrorState(QAbstractState *context);
121 void setError(QStateMachine::Error error, QAbstractState *currentContext);
122
123 // private slots
124 void _q_start();
125 void _q_process();
126#if QT_CONFIG(animation)
127 void _q_animationFinished();
128#endif
129 void _q_startDelayedEventTimer(int id, int delay);
130 void _q_killDelayedEventTimer(int id, int timerId);
131
132 QState *rootState() const;
133
134 void clearHistory();
135 QAbstractTransition *createInitialTransition() const;
136
137 void removeConflictingTransitions(QList<QAbstractTransition*> &enabledTransitions, CalculationCache *cache);
138 void microstep(QEvent *event, const QList<QAbstractTransition*> &transitionList, CalculationCache *cache);
139 QList<QAbstractTransition *> selectTransitions(QEvent *event, CalculationCache *cache);
140 virtual void noMicrostep();
141 virtual void processedPendingEvents(bool didChange);
142 virtual void beginMacrostep();
143 virtual void endMacrostep(bool didChange);
144 virtual void exitInterpreter();
145 virtual void exitStates(QEvent *event, const QList<QAbstractState *> &statesToExit_sorted,
146 const QHash<QAbstractState*, QVector<QPropertyAssignment> > &assignmentsForEnteredStates);
147 QList<QAbstractState*> computeExitSet(const QList<QAbstractTransition*> &enabledTransitions, CalculationCache *cache);
148 QSet<QAbstractState*> computeExitSet_Unordered(const QList<QAbstractTransition*> &enabledTransitions, CalculationCache *cache);
149 QSet<QAbstractState*> computeExitSet_Unordered(QAbstractTransition *t, CalculationCache *cache);
150 void executeTransitionContent(QEvent *event, const QList<QAbstractTransition*> &transitionList);
151 virtual void enterStates(QEvent *event, const QList<QAbstractState*> &exitedStates_sorted,
152 const QList<QAbstractState*> &statesToEnter_sorted,
153 const QSet<QAbstractState*> &statesForDefaultEntry,
154 QHash<QAbstractState *, QVector<QPropertyAssignment> > &propertyAssignmentsForState
155#if QT_CONFIG(animation)
156 , const QList<QAbstractAnimation*> &selectedAnimations
157#endif
158 );
159 QList<QAbstractState*> computeEntrySet(const QList<QAbstractTransition*> &enabledTransitions,
160 QSet<QAbstractState*> &statesForDefaultEntry, CalculationCache *cache);
161 QAbstractState *getTransitionDomain(QAbstractTransition *t,
162 const QList<QAbstractState *> &effectiveTargetStates,
163 CalculationCache *cache);
164 void addDescendantStatesToEnter(QAbstractState *state,
165 QSet<QAbstractState*> &statesToEnter,
166 QSet<QAbstractState*> &statesForDefaultEntry);
167 void addAncestorStatesToEnter(QAbstractState *s, QAbstractState *ancestor,
168 QSet<QAbstractState*> &statesToEnter,
169 QSet<QAbstractState*> &statesForDefaultEntry);
170
171 static QState *toStandardState(QAbstractState *state);
172 static const QState *toStandardState(const QAbstractState *state);
173 static QFinalState *toFinalState(QAbstractState *state);
174 static QHistoryState *toHistoryState(QAbstractState *state);
175
176 bool isInFinalState(QAbstractState *s) const;
177 static bool isFinal(const QAbstractState *s);
178 static bool isParallel(const QAbstractState *s);
179 bool isCompound(const QAbstractState *s) const;
180 bool isAtomic(const QAbstractState *s) const;
181
182 void goToState(QAbstractState *targetState);
183
184 void registerTransitions(QAbstractState *state);
185 void maybeRegisterTransition(QAbstractTransition *transition);
186 void registerTransition(QAbstractTransition *transition);
187 void maybeRegisterSignalTransition(QSignalTransition *transition);
188 void registerSignalTransition(QSignalTransition *transition);
189 void unregisterSignalTransition(QSignalTransition *transition);
190 void registerMultiThreadedSignalTransitions();
191#if QT_CONFIG(qeventtransition)
192 void maybeRegisterEventTransition(QEventTransition *transition);
193 void registerEventTransition(QEventTransition *transition);
194 void unregisterEventTransition(QEventTransition *transition);
195 void handleFilteredEvent(QObject *watched, QEvent *event);
196#endif
197 void unregisterTransition(QAbstractTransition *transition);
198 void unregisterAllTransitions();
199 void handleTransitionSignal(QObject *sender, int signalIndex,
200 void **args);
201
202 void postInternalEvent(QEvent *e);
203 void postExternalEvent(QEvent *e);
204 QEvent *dequeueInternalEvent();
205 QEvent *dequeueExternalEvent();
206 bool isInternalEventQueueEmpty();
207 bool isExternalEventQueueEmpty();
208 void processEvents(EventProcessingMode processingMode);
209 void cancelAllDelayedEvents();
210
211 virtual void emitStateFinished(QState *forState, QFinalState *guiltyState);
212 virtual void startupHook();
213
214#ifndef QT_NO_PROPERTIES
215 class RestorableId {
216 QPointer<QObject> guard;
217 QObject *obj;
218 QByteArray prop;
219 // two overloads because friends can't have default arguments
220 friend uint qHash(const RestorableId &key, uint seed)
221 noexcept(noexcept(qHash(key: std::declval<QByteArray>())))
222 { return qHash(key: qMakePair(x: key.obj, y: key.prop), seed); }
223 friend uint qHash(const RestorableId &key) noexcept(noexcept(qHash(key, seed: 0U)))
224 { return qHash(key, seed: 0U); }
225 friend bool operator==(const RestorableId &lhs, const RestorableId &rhs) noexcept
226 { return lhs.obj == rhs.obj && lhs.prop == rhs.prop; }
227 friend bool operator!=(const RestorableId &lhs, const RestorableId &rhs) noexcept
228 { return !operator==(lhs, rhs); }
229 public:
230 explicit RestorableId(QObject *o, QByteArray p) noexcept : guard(o), obj(o), prop(std::move(p)) {}
231 QObject *object() const noexcept { return guard; }
232 QByteArray propertyName() const noexcept { return prop; }
233 };
234 QHash<QAbstractState*, QHash<RestorableId, QVariant> > registeredRestorablesForState;
235 bool hasRestorable(QAbstractState *state, QObject *object, const QByteArray &propertyName) const;
236 QVariant savedValueForRestorable(const QList<QAbstractState*> &exitedStates_sorted,
237 QObject *object, const QByteArray &propertyName) const;
238 void registerRestorable(QAbstractState *state, QObject *object, const QByteArray &propertyName,
239 const QVariant &value);
240 void unregisterRestorables(const QList<QAbstractState*> &states, QObject *object,
241 const QByteArray &propertyName);
242 QVector<QPropertyAssignment> restorablesToPropertyList(const QHash<RestorableId, QVariant> &restorables) const;
243 QHash<RestorableId, QVariant> computePendingRestorables(const QList<QAbstractState*> &statesToExit_sorted) const;
244 QHash<QAbstractState*, QVector<QPropertyAssignment> > computePropertyAssignments(
245 const QList<QAbstractState*> &statesToEnter_sorted,
246 QHash<RestorableId, QVariant> &pendingRestorables) const;
247#endif
248
249 State state;
250 bool processing;
251 bool processingScheduled;
252 bool stop;
253 StopProcessingReason stopProcessingReason;
254 QSet<QAbstractState*> configuration;
255 QList<QEvent*> internalEventQueue;
256 QList<QEvent*> externalEventQueue;
257 QMutex internalEventMutex;
258 QMutex externalEventMutex;
259
260 QStateMachine::Error error;
261 QState::RestorePolicy globalRestorePolicy;
262
263 QString errorString;
264 QSet<QAbstractState *> pendingErrorStates;
265 QSet<QAbstractState *> pendingErrorStatesForDefaultEntry;
266
267#if QT_CONFIG(animation)
268 bool animated;
269
270 struct InitializeAnimationResult {
271 QList<QAbstractAnimation*> handledAnimations;
272 QList<QAbstractAnimation*> localResetEndValues;
273
274 void swap(InitializeAnimationResult &other) noexcept
275 {
276 qSwap(value1&: handledAnimations, value2&: other.handledAnimations);
277 qSwap(value1&: localResetEndValues, value2&: other.localResetEndValues);
278 }
279 };
280
281 InitializeAnimationResult
282 initializeAnimation(QAbstractAnimation *abstractAnimation,
283 const QPropertyAssignment &prop);
284
285 QHash<QAbstractState*, QList<QAbstractAnimation*> > animationsForState;
286 QHash<QAbstractAnimation*, QPropertyAssignment> propertyForAnimation;
287 QHash<QAbstractAnimation*, QAbstractState*> stateForAnimation;
288 QSet<QAbstractAnimation*> resetAnimationEndValues;
289
290 QList<QAbstractAnimation *> defaultAnimations;
291 QMultiHash<QAbstractState *, QAbstractAnimation *> defaultAnimationsForSource;
292 QMultiHash<QAbstractState *, QAbstractAnimation *> defaultAnimationsForTarget;
293
294 QList<QAbstractAnimation *> selectAnimations(const QList<QAbstractTransition *> &transitionList) const;
295 void terminateActiveAnimations(QAbstractState *state,
296 const QHash<QAbstractState*, QVector<QPropertyAssignment> > &assignmentsForEnteredStates);
297 void initializeAnimations(QAbstractState *state, const QList<QAbstractAnimation*> &selectedAnimations,
298 const QList<QAbstractState *> &exitedStates_sorted,
299 QHash<QAbstractState *, QVector<QPropertyAssignment> > &assignmentsForEnteredStates);
300#endif // animation
301
302 QSignalEventGenerator *signalEventGenerator;
303
304 QHash<const QObject*, QVector<int> > connections;
305 QMutex connectionsMutex;
306#if QT_CONFIG(qeventtransition)
307 QHash<QObject*, QHash<QEvent::Type, int> > qobjectEvents;
308#endif
309 QFreeList<void> delayedEventIdFreeList;
310 struct DelayedEvent {
311 QEvent *event;
312 int timerId;
313 DelayedEvent(QEvent *e, int tid)
314 : event(e), timerId(tid) {}
315 DelayedEvent()
316 : event(nullptr), timerId(0) {}
317 };
318 QHash<int, DelayedEvent> delayedEvents;
319 QHash<int, int> timerIdToDelayedEventId;
320 QMutex delayedEventsMutex;
321
322 typedef QEvent* (*f_cloneEvent)(QEvent*);
323 struct Handler {
324 f_cloneEvent cloneEvent;
325 };
326
327 static const Handler *handler;
328};
329#if QT_CONFIG(animation)
330Q_DECLARE_SHARED(QStateMachinePrivate::InitializeAnimationResult)
331#endif
332
333Q_CORE_EXPORT const QStateMachinePrivate::Handler *qcoreStateMachineHandler();
334
335QT_END_NAMESPACE
336
337#endif
338

source code of qtbase/src/corelib/statemachine/qstatemachine_p.h