From 0e22001a3bb070d4e9956e89543ec0e5ac6f23f8 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Wed, 24 Feb 2021 19:44:09 +0100 Subject: Add more support for structured bindings After QPoint(F), it's now the time of QSize(F) and QVectorND, which can be unambiguously decomposed. [ChangeLog][QtCore][QSize] QSize is now usable in a structured binding declaration. [ChangeLog][QtCore][QSizeF] QSizeF is now usable in a structured binding declaration. [ChangeLog][QtGui][QVector2D] QVector2D is now usable in a structured binding declaration. [ChangeLog][QtGui][QVector3D] QVector3D is now usable in a structured binding declaration. [ChangeLog][QtGui][QVector4D] QVector4D is now usable in a structured binding declaration. Change-Id: I67bb152f4210f2be27607179cd2ec522174cc483 Reviewed-by: Volker Hilsheimer --- src/gui/math3d/qvectornd.h | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'src/gui/math3d') diff --git a/src/gui/math3d/qvectornd.h b/src/gui/math3d/qvectornd.h index 153f3743d8..dec98bd936 100644 --- a/src/gui/math3d/qvectornd.h +++ b/src/gui/math3d/qvectornd.h @@ -177,6 +177,18 @@ private: friend class QVector3D; friend class QVector4D; + + template = true, + std::enable_if_t, QVector2D>, bool> = true> + friend constexpr decltype(auto) get(V &&vec) noexcept + { + if constexpr (I == 0) + return (std::forward(vec).v[0]); + else if constexpr (I == 1) + return (std::forward(vec).v[1]); + } }; Q_DECLARE_TYPEINFO(QVector2D, Q_PRIMITIVE_TYPE); @@ -327,6 +339,20 @@ private: friend QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix); friend QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector); #endif + + template = true, + std::enable_if_t, QVector3D>, bool> = true> + friend constexpr decltype(auto) get(V &&vec) noexcept + { + if constexpr (I == 0) + return (std::forward(vec).v[0]); + else if constexpr (I == 1) + return (std::forward(vec).v[1]); + else if constexpr (I == 2) + return (std::forward(vec).v[2]); + } }; Q_DECLARE_TYPEINFO(QVector3D, Q_PRIMITIVE_TYPE); @@ -470,6 +496,22 @@ private: friend QVector4D operator*(const QVector4D& vector, const QMatrix4x4& matrix); friend QVector4D operator*(const QMatrix4x4& matrix, const QVector4D& vector); #endif + + template = true, + std::enable_if_t, QVector4D>, bool> = true> + friend constexpr decltype(auto) get(V &&vec) noexcept + { + if constexpr (I == 0) + return (std::forward(vec).v[0]); + else if constexpr (I == 1) + return (std::forward(vec).v[1]); + else if constexpr (I == 2) + return (std::forward(vec).v[2]); + else if constexpr (I == 3) + return (std::forward(vec).v[3]); + } }; Q_DECLARE_TYPEINFO(QVector4D, Q_PRIMITIVE_TYPE); @@ -1050,4 +1092,41 @@ Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector4D &); QT_END_NAMESPACE +/***************************** Tuple protocol *****************************/ + +namespace std { +#ifndef QT_NO_VECTOR2D + template <> + class tuple_size : public integral_constant {}; + template <> + class tuple_element<0, QT_PREPEND_NAMESPACE(QVector2D)> { public: using type = float; }; + template <> + class tuple_element<1, QT_PREPEND_NAMESPACE(QVector2D)> { public: using type = float; }; +#endif // QT_NO_VECTOR2D + +#ifndef QT_NO_VECTOR3D + template <> + class tuple_size : public integral_constant {}; + template <> + class tuple_element<0, QT_PREPEND_NAMESPACE(QVector3D)> { public: using type = float; }; + template <> + class tuple_element<1, QT_PREPEND_NAMESPACE(QVector3D)> { public: using type = float; }; + template <> + class tuple_element<2, QT_PREPEND_NAMESPACE(QVector3D)> { public: using type = float; }; +#endif // QT_NO_VECTOR3D + +#ifndef QT_NO_VECTOR4D + template <> + class tuple_size : public integral_constant {}; + template <> + class tuple_element<0, QT_PREPEND_NAMESPACE(QVector4D)> { public: using type = float; }; + template <> + class tuple_element<1, QT_PREPEND_NAMESPACE(QVector4D)> { public: using type = float; }; + template <> + class tuple_element<2, QT_PREPEND_NAMESPACE(QVector4D)> { public: using type = float; }; + template <> + class tuple_element<3, QT_PREPEND_NAMESPACE(QVector4D)> { public: using type = float; }; +#endif // QT_NO_VECTOR4D +} + #endif // QVECTORND_H -- cgit v1.2.3