QGraphicsAnchorLayout: make createSlack() return a proper struct
... instead of std::pair. This is in preparation of breaking QSimplexVariable up into QSimplexVariable (with protected dtor) and trivial subclass QConcreteSimplexVariable, to statically ensure that we're not deleting derived classes (AnchorData) through QSimplexVariable pointers (which is UB, and subject to a Coverity complaint). This is basically the same program we did for QBrushData, culminating in 3bbc9e29ef59683351cf35c19a8bd4a030615c64. This second patch modernizes the code a bit and, by scoping the variable holding the return value of createSlack() tighter, allows the use of auto and therefore isolates the code from the changes in the final patch of the series. Pick-to: 6.8 6.5 Coverity-Id: 390828 Change-Id: I15b51a3118c7ef33e8351a3e198abaebf4300d61 Reviewed-by: Mate Barany <mate.barany@qt.io> (cherry picked from commit 3aa431e3fe42d7eb943f6b9920ad47b074856abe) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
e02348117c
commit
6d2421d812
@ -2761,9 +2761,13 @@ bool QGraphicsAnchorLayoutPrivate::solveMinMax(const QList<QSimplexConstraint *>
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum slackType { Grower = -1, Shrinker = 1 };
|
enum slackType { Grower = -1, Shrinker = 1 };
|
||||||
static std::pair<QSimplexVariable *, QSimplexConstraint *> createSlack(QSimplexConstraint *sizeConstraint,
|
static auto createSlack(QSimplexConstraint *sizeConstraint, qreal interval, slackType type)
|
||||||
qreal interval, slackType type)
|
|
||||||
{
|
{
|
||||||
|
struct R {
|
||||||
|
QSimplexVariable *slack;
|
||||||
|
QSimplexConstraint *limit;
|
||||||
|
};
|
||||||
|
|
||||||
QSimplexVariable *slack = new QSimplexVariable;
|
QSimplexVariable *slack = new QSimplexVariable;
|
||||||
sizeConstraint->variables.insert(slack, type);
|
sizeConstraint->variables.insert(slack, type);
|
||||||
|
|
||||||
@ -2772,7 +2776,7 @@ static std::pair<QSimplexVariable *, QSimplexConstraint *> createSlack(QSimplexC
|
|||||||
limit->ratio = QSimplexConstraint::LessOrEqual;
|
limit->ratio = QSimplexConstraint::LessOrEqual;
|
||||||
limit->constant = interval;
|
limit->constant = interval;
|
||||||
|
|
||||||
return std::pair(slack, limit);
|
return R{slack, limit};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QGraphicsAnchorLayoutPrivate::solvePreferred(const QList<QSimplexConstraint *> &constraints,
|
bool QGraphicsAnchorLayoutPrivate::solvePreferred(const QList<QSimplexConstraint *> &constraints,
|
||||||
@ -2815,48 +2819,47 @@ bool QGraphicsAnchorLayoutPrivate::solvePreferred(const QList<QSimplexConstraint
|
|||||||
sizeConstraint->constant = ad->prefSize + g_offset;
|
sizeConstraint->constant = ad->prefSize + g_offset;
|
||||||
|
|
||||||
// Can easily shrink
|
// Can easily shrink
|
||||||
std::pair<QSimplexVariable *, QSimplexConstraint *> slack;
|
|
||||||
const qreal softShrinkInterval = ad->prefSize - ad->minPrefSize;
|
const qreal softShrinkInterval = ad->prefSize - ad->minPrefSize;
|
||||||
if (softShrinkInterval) {
|
if (softShrinkInterval) {
|
||||||
slack = createSlack(sizeConstraint, softShrinkInterval, Shrinker);
|
auto r = createSlack(sizeConstraint, softShrinkInterval, Shrinker);
|
||||||
preferredVariables += slack.first;
|
preferredVariables += r.slack;
|
||||||
preferredConstraints += slack.second;
|
preferredConstraints += r.limit;
|
||||||
|
|
||||||
// Add to objective with ratio == 1 (soft)
|
// Add to objective with ratio == 1 (soft)
|
||||||
objective.variables.insert(slack.first, 1.0);
|
objective.variables.insert(r.slack, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can easily grow
|
// Can easily grow
|
||||||
const qreal softGrowInterval = ad->maxPrefSize - ad->prefSize;
|
const qreal softGrowInterval = ad->maxPrefSize - ad->prefSize;
|
||||||
if (softGrowInterval) {
|
if (softGrowInterval) {
|
||||||
slack = createSlack(sizeConstraint, softGrowInterval, Grower);
|
auto r = createSlack(sizeConstraint, softGrowInterval, Grower);
|
||||||
preferredVariables += slack.first;
|
preferredVariables += r.slack;
|
||||||
preferredConstraints += slack.second;
|
preferredConstraints += r.limit;
|
||||||
|
|
||||||
// Add to objective with ratio == 1 (soft)
|
// Add to objective with ratio == 1 (soft)
|
||||||
objective.variables.insert(slack.first, 1.0);
|
objective.variables.insert(r.slack, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can shrink if really necessary
|
// Can shrink if really necessary
|
||||||
const qreal hardShrinkInterval = ad->minPrefSize - ad->minSize;
|
const qreal hardShrinkInterval = ad->minPrefSize - ad->minSize;
|
||||||
if (hardShrinkInterval) {
|
if (hardShrinkInterval) {
|
||||||
slack = createSlack(sizeConstraint, hardShrinkInterval, Shrinker);
|
auto r = createSlack(sizeConstraint, hardShrinkInterval, Shrinker);
|
||||||
preferredVariables += slack.first;
|
preferredVariables += r.slack;
|
||||||
preferredConstraints += slack.second;
|
preferredConstraints += r.limit;
|
||||||
|
|
||||||
// Add to objective with ratio == N (hard)
|
// Add to objective with ratio == N (hard)
|
||||||
objective.variables.insert(slack.first, variables.size());
|
objective.variables.insert(r.slack, variables.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can grow if really necessary
|
// Can grow if really necessary
|
||||||
const qreal hardGrowInterval = ad->maxSize - ad->maxPrefSize;
|
const qreal hardGrowInterval = ad->maxSize - ad->maxPrefSize;
|
||||||
if (hardGrowInterval) {
|
if (hardGrowInterval) {
|
||||||
slack = createSlack(sizeConstraint, hardGrowInterval, Grower);
|
auto r = createSlack(sizeConstraint, hardGrowInterval, Grower);
|
||||||
preferredVariables += slack.first;
|
preferredVariables += r.slack;
|
||||||
preferredConstraints += slack.second;
|
preferredConstraints += r.limit;
|
||||||
|
|
||||||
// Add to objective with ratio == N (hard)
|
// Add to objective with ratio == N (hard)
|
||||||
objective.variables.insert(slack.first, variables.size());
|
objective.variables.insert(r.slack, variables.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user