diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2020-06-01 11:54:45 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2021-07-13 19:58:08 +0200 |
commit | 85d27945ac3f4a82e659f8df294b75b6f302f2db (patch) | |
tree | 467587fa82cef70352deb8743a1efc00cdeec032 /src/gui/painting/qpainter_p.h | |
parent | e08f6d601de4faea869dcfd05a7e133366328981 (diff) |
QPainter: replace manual memory management [2/5]: state/states
Use unique_ptr to indicate ownership. Use std::stack to enforce
stack-ness (QStack doesn't). Requires the addition of a clear()
function, for which we spin a SmallStack wrapper. It also helps to not
have to repeat the value_type...
Because we use unique_ptr, `state` and `states` are now exclusive
(before, `state` was always `states.back()`). Correspondingly, rename
`states` → `savedStates`.
As a drive-by, this fixes an off-by-one error in a qWarning() (checked
`> 1`, but reported one saved state too many).
Change-Id: I8faade59300401be802ddc52c64ce31b8f23dc52
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/gui/painting/qpainter_p.h')
-rw-r--r-- | src/gui/painting/qpainter_p.h | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/gui/painting/qpainter_p.h b/src/gui/painting/qpainter_p.h index 71e90c9717..8994bcd3e7 100644 --- a/src/gui/painting/qpainter_p.h +++ b/src/gui/painting/qpainter_p.h @@ -66,6 +66,7 @@ #include <private/qpen_p.h> #include <memory> +#include <stack> QT_BEGIN_NAMESPACE @@ -205,8 +206,12 @@ public: QPainter *q_ptr; QPainterPrivate **d_ptrs; - QPainterState *state; - QVarLengthArray<QPainterState *, 8> states; + std::unique_ptr<QPainterState> state; + template <typename T, std::size_t N = 8> + struct SmallStack : std::stack<T, QVarLengthArray<T, N>> { + void clear() { this->c.clear(); } + }; + SmallStack<std::unique_ptr<QPainterState>> savedStates; mutable std::unique_ptr<QPainterDummyState> dummyState; @@ -230,6 +235,7 @@ public: void updateEmulationSpecifier(QPainterState *s); void updateStateImpl(QPainterState *state); void updateState(QPainterState *state); + void updateState(std::unique_ptr<QPainterState> &state) { updateState(state.get()); } void draw_helper(const QPainterPath &path, DrawOperation operation = StrokeAndFillDraw); void drawStretchedGradient(const QPainterPath &path, DrawOperation operation); |