Skip to content

Commit

Permalink
QVariant: deprecate qVariantFromValue/qVariantSetValue()
Browse files Browse the repository at this point in the history
qVariantFromValue/qVariantSetValue() was marked as obsolete since Qt4.
Therefore mark them as deprecated with Qt5.14.
Since QVariant::setValue/fromValue() were using the now deprecated
functions move the implementation to them and let
qVariantFromValue/qVariantSetValue() call
QVariant::setValue/fromValue().

Fixes: QTBUG-74043
Change-Id: I46617cc4d5c1e8c162d0f1f7ae32e4cfe9ce915c
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
  • Loading branch information
chehrlic committed Mar 31, 2019
1 parent 300940a commit c19d556
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
7 changes: 2 additions & 5 deletions examples/corelib/tools/doc/src/customtype.qdoc
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,15 @@

\snippet tools/customtype/main.cpp storing a custom value

Alternatively, the QVariant::fromValue() and qVariantSetValue() functions
can be used if you are using a compiler without support for member template
Alternatively, the QVariant::fromValue() function can be used if
you are using a compiler without support for member template
functions.

The value can be retrieved using the QVariant::value() member template
function:

\snippet tools/customtype/main.cpp retrieving a custom value

Alternatively, the qVariantValue() template function can be used if
you are using a compiler without support for member template functions.

\section1 Further Reading

The custom \c Message type can also be used with direct signal-slot
Expand Down
2 changes: 2 additions & 0 deletions src/corelib/kernel/qvariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4282,6 +4282,7 @@ QDebug operator<<(QDebug dbg, const QVariant::Type p)
\sa fromValue()
*/

#if QT_DEPRECATED_SINCE(5, 14)
/*!
\fn template<typename T> QVariant qVariantFromValue(const T &value)
\relates QVariant
Expand Down Expand Up @@ -4319,6 +4320,7 @@ QDebug operator<<(QDebug dbg, const QVariant::Type p)
\sa QVariant::setValue()
*/
#endif

/*!
\fn template<typename T> T qvariant_cast(const QVariant &value)
Expand Down
58 changes: 33 additions & 25 deletions src/corelib/kernel/qvariant.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ class QUrl;
class QVariant;
class QVariantComparisonHelper;

template <typename T>
inline QVariant qVariantFromValue(const T &);

template<typename T>
inline T qvariant_cast(const QVariant &);

Expand Down Expand Up @@ -365,7 +362,7 @@ class Q_CORE_EXPORT QVariant

template<typename T>
static inline QVariant fromValue(const T &value)
{ return qVariantFromValue(value); }
{ return QVariant(qMetaTypeId<T>(), &value, QTypeInfo<T>::isPointer); }

#if QT_HAS_INCLUDE(<variant>) && __cplusplus >= 201703L
template<typename... Types>
Expand Down Expand Up @@ -516,50 +513,61 @@ class Q_CORE_EXPORT QVariant
inline const DataPtr &data_ptr() const { return d; }
};

#if QT_DEPRECATED_SINCE(5, 14)
template <typename T>
QT_DEPRECATED_X("Use QVariant::fromValue() instead.")
inline QVariant qVariantFromValue(const T &t)
{
return QVariant(qMetaTypeId<T>(), &t, QTypeInfo<T>::isPointer);
return QVariant::fromValue(t);
}

template <typename T>
QT_DEPRECATED_X("Use QVariant::setValue() instead.")
inline void qVariantSetValue(QVariant &v, const T &t)
{
v.setValue(t);
}
#endif

template <>
inline QVariant qVariantFromValue(const QVariant &t) { return t; }
template<>
inline QVariant QVariant::fromValue(const QVariant &value)
{
return value;
}

#if QT_HAS_INCLUDE(<variant>) && __cplusplus >= 201703L
template <>
inline QVariant qVariantFromValue(const std::monostate &) { return QVariant(); }
template<>
inline QVariant QVariant::fromValue(const std::monostate &)
{
return QVariant();
}
#endif

template <typename T>
inline void qVariantSetValue(QVariant &v, const T &t)
inline bool QVariant::isValid() const { return d.type != Invalid; }

template<typename T>
inline void QVariant::setValue(const T &avalue)
{
//if possible we reuse the current QVariant private
// If possible we reuse the current QVariant private.
const uint type = qMetaTypeId<T>();
QVariant::Private &d = v.data_ptr();
if (v.isDetached() && (type == d.type || (type <= uint(QVariant::Char) && d.type <= uint(QVariant::Char)))) {
if (isDetached() && (type == d.type || (type <= uint(QVariant::Char) && d.type <= uint(QVariant::Char)))) {
d.type = type;
d.is_null = false;
T *old = reinterpret_cast<T*>(d.is_shared ? d.data.shared->ptr : &d.data.ptr);
if (QTypeInfo<T>::isComplex)
old->~T();
new (old) T(t); //call the copy constructor
new (old) T(avalue); // call the copy constructor
} else {
v = QVariant(type, &t, QTypeInfo<T>::isPointer);
*this = QVariant(type, &avalue, QTypeInfo<T>::isPointer);
}
}

template <>
inline void qVariantSetValue<QVariant>(QVariant &v, const QVariant &t)
template<>
inline void QVariant::setValue(const QVariant &avalue)
{
v = t;
*this = avalue;
}

inline bool QVariant::isValid() const { return d.type != Invalid; }

template<typename T>
inline void QVariant::setValue(const T &avalue)
{ qVariantSetValue(*this, avalue); }

#ifndef QT_NO_DATASTREAM
Q_CORE_EXPORT QDataStream& operator>> (QDataStream& s, QVariant& p);
Q_CORE_EXPORT QDataStream& operator<< (QDataStream& s, const QVariant& p);
Expand Down

0 comments on commit c19d556

Please sign in to comment.