summaryrefslogtreecommitdiffstats
path: root/src/corelib/statemachine
diff options
context:
space:
mode:
authorErik Verbruggen <erik.verbruggen@theqtcompany.com>2015-10-06 16:13:31 +0200
committerErik Verbruggen <erik.verbruggen@theqtcompany.com>2015-10-15 08:25:18 +0000
commit61be975574bff3e8f34d55df5131284a0134f7b4 (patch)
tree0a7d1f764853aee3bed20275e96dabfa46f907b0 /src/corelib/statemachine
parent6f34660340421670c44d726249af3dd2f0be04fa (diff)
QStateMachine: cleanup QAbstractTransition::setTargetStates
Prevent QPointer creation for every new target, and a copy of a QVector of QPointer, and two QPointer destructions, when setting new target states. The typical (only?) use-case, setting the target states right after transition creation, is also faster. Change-Id: I931783afbcea43c8a84200133f26454a4b689edc Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
Diffstat (limited to 'src/corelib/statemachine')
-rw-r--r--src/corelib/statemachine/qabstracttransition.cpp51
1 files changed, 40 insertions, 11 deletions
diff --git a/src/corelib/statemachine/qabstracttransition.cpp b/src/corelib/statemachine/qabstracttransition.cpp
index 5a7a95883b..56f2a15fdb 100644
--- a/src/corelib/statemachine/qabstracttransition.cpp
+++ b/src/corelib/statemachine/qabstracttransition.cpp
@@ -252,26 +252,55 @@ QList<QAbstractState*> QAbstractTransition::targetStates() const
void QAbstractTransition::setTargetStates(const QList<QAbstractState*> &targets)
{
Q_D(QAbstractTransition);
- QVector<QPointer<QAbstractState> > copy(d->targetStates);
- bool sameList = true;
+
+ // Verify if any of the new target states is a null-pointer:
for (int i = 0; i < targets.size(); ++i) {
- QAbstractState *target = targets.at(i);
- if (!target) {
+ if (targets.at(i) == Q_NULLPTR) {
qWarning("QAbstractTransition::setTargetStates: target state(s) cannot be null");
return;
+ }
+ }
+
+ // First clean out any target states that got destroyed, but for which we still have a QPointer
+ // around.
+ for (int i = 0; i < d->targetStates.size(); ) {
+ if (d->targetStates.at(i).isNull()) {
+ d->targetStates.remove(i);
} else {
- sameList &= copy.removeOne(target);
+ ++i;
+ }
+ }
+
+ // Easy check: if both lists are empty, we're done.
+ if (targets.isEmpty() && d->targetStates.isEmpty())
+ return;
+
+ bool sameList = true;
+
+ if (targets.size() != d->targetStates.size()) {
+ // If the sizes of the lists are different, we don't need to be smart: they're different. So
+ // we can just set the new list as the targetStates.
+ sameList = false;
+ } else {
+ QVector<QPointer<QAbstractState> > copy(d->targetStates);
+ for (int i = 0; i < targets.size(); ++i) {
+ sameList &= copy.removeOne(targets.at(i));
+ if (!sameList)
+ break; // ok, we now know the lists are not the same, so stop the loop.
}
+
+ sameList &= copy.isEmpty();
}
- sameList &= copy.isEmpty();
+ if (sameList)
+ return;
- d->targetStates.clear();
- for (int i = 0; i < targets.size(); ++i)
- d->targetStates.append(targets.at(i));
+ d->targetStates.resize(targets.size());
+ for (int i = 0; i < targets.size(); ++i) {
+ d->targetStates[i] = targets.at(i);
+ }
- if (!sameList)
- emit targetStatesChanged(QPrivateSignal());
+ emit targetStatesChanged(QPrivateSignal());
}
/*!