Q_DECLARE_SHARED: fix docs; require and use member-swap

By requiring a member-swap, this macro becomes applicable to a wider
range of types (e.g. QFont, which has another member besides 'd'),
while at the same time avoiding the encapsulation leak that is data_ptr().

There have been concerns over breaking existing users of
this macro, but for some time now, Q_DECLARE_SHARED only
works within QT_BEGIN_NAMESPACE anyway, so its a safe bet
that all users of this macro are in-tree.

Change-Id: I7fdd9dba204554af8d3f9768b97bb42847a5acf4
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2012-04-05 15:34:46 +02:00 committed by Qt by Nokia
parent fa36d81bbc
commit 294960c160

View File

@ -166,23 +166,23 @@ Q_DECLARE_TYPEINFO_BODY(QFlags<T>, Q_PRIMITIVE_TYPE);
/* /*
Specialize a shared type with: Specialize a shared type with:
Q_DECLARE_SHARED(type); Q_DECLARE_SHARED(type)
where 'type' is the name of the type to specialize. NOTE: shared where 'type' is the name of the type to specialize. NOTE: shared
types must declare a 'bool isDetached(void) const;' member for this types must define a member-swap, and be defined in the same
to work. namespace as Qt for this to work.
*/ */
#define Q_DECLARE_SHARED_STL(TYPE) \ #define Q_DECLARE_SHARED_STL(TYPE) \
QT_END_NAMESPACE \ QT_END_NAMESPACE \
namespace std { \ namespace std { \
template<> inline void swap<QT_PREPEND_NAMESPACE(TYPE)>(QT_PREPEND_NAMESPACE(TYPE) &value1, QT_PREPEND_NAMESPACE(TYPE) &value2) \ template<> inline void swap<QT_PREPEND_NAMESPACE(TYPE)>(QT_PREPEND_NAMESPACE(TYPE) &value1, QT_PREPEND_NAMESPACE(TYPE) &value2) \
{ swap(value1.data_ptr(), value2.data_ptr()); } \ { value1.swap(value2); } \
} \ } \
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
#define Q_DECLARE_SHARED(TYPE) \ #define Q_DECLARE_SHARED(TYPE) \
template <> inline void qSwap<TYPE>(TYPE &value1, TYPE &value2) \ template <> inline void qSwap<TYPE>(TYPE &value1, TYPE &value2) \
{ qSwap(value1.data_ptr(), value2.data_ptr()); } \ { value1.swap(value2); } \
Q_DECLARE_SHARED_STL(TYPE) Q_DECLARE_SHARED_STL(TYPE)
/* /*