aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2019-10-15 11:16:04 -0700
committerThiago Macieira <thiago.macieira@intel.com>2019-10-16 22:40:01 -0700
commite2b6135b25c06cd93a0275d40d1ef736a50a169d (patch)
tree4c49ff22a6a893d43a3d44a07c93092fbbfdf427
parent1d3c08b5e25613a738766ddc3a0d3c7ca5b59e8e (diff)
QFlagPointer/QBiPointer: insert static assertions in addition to dynamic
Static assertions weren't really a thing back in 2012 when Aaron wrote this code. Change-Id: I8d95fbaf90e842b9b44dfffd15cde4c154e22810 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--src/qml/qml/ftw/qflagpointer_p.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/qml/qml/ftw/qflagpointer_p.h b/src/qml/qml/ftw/qflagpointer_p.h
index 71b41cd30b..a10e57aeca 100644
--- a/src/qml/qml/ftw/qflagpointer_p.h
+++ b/src/qml/qml/ftw/qflagpointer_p.h
@@ -55,6 +55,17 @@
QT_BEGIN_NAMESPACE
+namespace QtPrivate {
+template <typename T> struct QFlagPointerAlignment
+{
+ enum : size_t { Value = Q_ALIGNOF(T) };
+};
+template <> struct QFlagPointerAlignment<void>
+{
+ enum : size_t { Value = ~size_t(0) };
+};
+}
+
template<typename T>
class QFlagPointer {
public:
@@ -133,6 +144,7 @@ template<typename T>
QFlagPointer<T>::QFlagPointer(T *v)
: ptr_value(quintptr(v))
{
+ Q_STATIC_ASSERT_X(Q_ALIGNOF(T) >= 4, "Type T does not have sufficient alignment");
Q_ASSERT((ptr_value & FlagsMask) == 0);
}
@@ -247,6 +259,8 @@ template<typename T, typename T2>
QBiPointer<T, T2>::QBiPointer(T *v)
: ptr_value(quintptr(v))
{
+ Q_STATIC_ASSERT_X(QtPrivate::QFlagPointerAlignment<T>::Value >= 4,
+ "Type T does not have sufficient alignment");
Q_ASSERT((quintptr(v) & FlagsMask) == 0);
}
@@ -254,6 +268,8 @@ template<typename T, typename T2>
QBiPointer<T, T2>::QBiPointer(T2 *v)
: ptr_value(quintptr(v) | Flag2Bit)
{
+ Q_STATIC_ASSERT_X(QtPrivate::QFlagPointerAlignment<T2>::Value >= 4,
+ "Type T2 does not have sufficient alignment");
Q_ASSERT((quintptr(v) & FlagsMask) == 0);
}