diff --git a/src/corelib/tools/qmargins.h b/src/corelib/tools/qmargins.h index f8fba1e9d6..b0f61eeb66 100644 --- a/src/corelib/tools/qmargins.h +++ b/src/corelib/tools/qmargins.h @@ -94,6 +94,22 @@ private: { return !(m1 == m2); } + + template = true, + std::enable_if_t, QMargins>, bool> = true> + friend constexpr decltype(auto) get(M &&m) noexcept + { + if constexpr (I == 0) + return (std::forward(m).m_left); + else if constexpr (I == 1) + return (std::forward(m).m_top); + else if constexpr (I == 2) + return (std::forward(m).m_right); + else if constexpr (I == 3) + return (std::forward(m).m_bottom); + } }; Q_DECLARE_TYPEINFO(QMargins, Q_MOVABLE_TYPE); @@ -327,6 +343,22 @@ private: { return !(lhs == rhs); } + + template = true, + std::enable_if_t, QMarginsF>, bool> = true> + friend constexpr decltype(auto) get(M &&m) noexcept + { + if constexpr (I == 0) + return (std::forward(m).m_left); + else if constexpr (I == 1) + return (std::forward(m).m_top); + else if constexpr (I == 2) + return (std::forward(m).m_right); + else if constexpr (I == 3) + return (std::forward(m).m_bottom); + } }; Q_DECLARE_TYPEINFO(QMarginsF, Q_MOVABLE_TYPE); @@ -494,4 +526,32 @@ Q_CORE_EXPORT QDebug operator<<(QDebug, const QMarginsF &); QT_END_NAMESPACE +/***************************************************************************** + QMargins/QMarginsF tuple protocol + *****************************************************************************/ + +namespace std { + template <> + class tuple_size : public integral_constant {}; + template <> + class tuple_element<0, QT_PREPEND_NAMESPACE(QMargins)> { public: using type = int; }; + template <> + class tuple_element<1, QT_PREPEND_NAMESPACE(QMargins)> { public: using type = int; }; + template <> + class tuple_element<2, QT_PREPEND_NAMESPACE(QMargins)> { public: using type = int; }; + template <> + class tuple_element<3, QT_PREPEND_NAMESPACE(QMargins)> { public: using type = int; }; + + template <> + class tuple_size : public integral_constant {}; + template <> + class tuple_element<0, QT_PREPEND_NAMESPACE(QMarginsF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; + template <> + class tuple_element<1, QT_PREPEND_NAMESPACE(QMarginsF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; + template <> + class tuple_element<2, QT_PREPEND_NAMESPACE(QMarginsF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; + template <> + class tuple_element<3, QT_PREPEND_NAMESPACE(QMarginsF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; +} + #endif // QMARGINS_H diff --git a/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp b/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp index 6b89ba0b81..8d71ccf7ab 100644 --- a/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp +++ b/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp @@ -52,6 +52,8 @@ private slots: #ifndef QT_NO_DEBUG_STREAM void debugStreamCheckF(); #endif + + void structuredBinding(); }; // Testing get/set functions @@ -283,5 +285,59 @@ void tst_QMargins::debugStreamCheckF() } #endif +void tst_QMargins::structuredBinding() +{ + { + QMargins m(1, 2, 3, 4); + auto [left, top, right, bottom] = m; + QCOMPARE(left, 1); + QCOMPARE(top, 2); + QCOMPARE(right, 3); + QCOMPARE(bottom, 4); + } + { + QMargins m(1, 2, 3, 4); + auto &[left, top, right, bottom] = m; + QCOMPARE(left, 1); + QCOMPARE(top, 2); + QCOMPARE(right, 3); + QCOMPARE(bottom, 4); + + left = 10; + top = 20; + right = 30; + bottom = 40; + QCOMPARE(m.left(), 10); + QCOMPARE(m.top(), 20); + QCOMPARE(m.right(), 30); + QCOMPARE(m.bottom(), 40); + } + { + QMarginsF m(1.0, 2.0, 3.0, 4.0); + auto [left, top, right, bottom] = m; + QCOMPARE(left, 1.0); + QCOMPARE(top, 2.0); + QCOMPARE(right, 3.0); + QCOMPARE(bottom, 4.0); + } + { + QMarginsF m(1.0, 2.0, 3.0, 4.0); + auto &[left, top, right, bottom] = m; + QCOMPARE(left, 1.0); + QCOMPARE(top, 2.0); + QCOMPARE(right, 3.0); + QCOMPARE(bottom, 4.0); + + left = 10.0; + top = 20.0; + right = 30.0; + bottom = 40.0; + QCOMPARE(m.left(), 10.0); + QCOMPARE(m.top(), 20.0); + QCOMPARE(m.right(), 30.0); + QCOMPARE(m.bottom(), 40.0); + } +} + QTEST_APPLESS_MAIN(tst_QMargins) #include "tst_qmargins.moc"