From 0c978407088fdbd15dc185aef7506087a88e1443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= Date: Tue, 31 Jan 2012 17:35:00 +0100 Subject: Introduce Q_DECLARE_OPAQUE_POINTER To hide the IsPointerToTypeDerivedFromQObject monstruosity :-) Documentation for Q_DECLARE_METATYPE and qRegisterMetaType was updated to mention requirements on registered types and how they can be circumvented for pointer types with the new macro. Change-Id: If83b037a8e2f28761eb903525e87008107298801 Reviewed-by: Harald Fernengel Reviewed-by: Stephen Kelly --- src/corelib/kernel/qmetatype.cpp | 20 ++++++++++++++++++++ src/corelib/kernel/qmetatype.h | 10 ++++++++++ src/sql/drivers/psql/qsql_psql.cpp | 12 ++---------- src/sql/drivers/sqlite/qsql_sqlite.cpp | 12 ++---------- src/sql/drivers/sqlite2/qsql_sqlite2.cpp | 11 +++-------- src/sql/drivers/tds/qsql_tds.cpp | 15 +++------------ 6 files changed, 40 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index 6a7c5c2a34..d965c48255 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -116,6 +116,16 @@ template<> struct TypeDefinition { static const bool IsAvailable = fals #endif } // namespace +/*! + \macro Q_DECLARE_OPAQUE_POINTER(Pointer) + \relates QMetaType + + This macro enables pointers to forward-declared types to be registered with + QMetaType using either Q_DECLARE_METATYPE() or qRegisterMetaType(). + + \sa Q_DECLARE_METATYPE(), qRegisterMetaType() + +*/ /*! \macro Q_DECLARE_METATYPE(Type) @@ -126,6 +136,11 @@ template<> struct TypeDefinition { static const bool IsAvailable = fals a public destructor. It is needed to use the type \a Type as a custom type in QVariant. + This macro requires that \a Type is a fully defined type at the point where + it is used. For pointer types, it also requires that the pointed to type is + fully defined. Use in conjunction with Q_DECLARE_OPAQUE_POINTER() to + register pointers to forward declared types. + Ideally, this macro should be placed below the declaration of the class or struct. If that is not possible, it can be put in a private header file which has to be included every time that @@ -1630,6 +1645,11 @@ QMetaType::TypeFlags QMetaType::typeFlags(int type) public default constructor, a public copy constructor and a public destructor can be registered. + This function requires that \c{T} is a fully defined type at the point + where the function is called. For pointer types, it also requires that the + pointed to type is fully defined. Use Q_DECLARE_OPAQUE_POINTER() to be able + to register pointers to forward declared types. + After a type has been registered, you can create and destroy objects of that type dynamically at run-time. diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index fd27bb81de..9e702298c1 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -471,6 +471,16 @@ inline int qRegisterMetaTypeStreamOperators() } #endif +#define Q_DECLARE_OPAQUE_POINTER(POINTER) \ + QT_BEGIN_NAMESPACE namespace QtPrivate { \ + template <> \ + struct IsPointerToTypeDerivedFromQObject \ + { \ + enum { Value = false }; \ + }; \ + } QT_END_NAMESPACE \ + /**/ + #define Q_DECLARE_METATYPE(TYPE) \ QT_BEGIN_NAMESPACE \ template <> \ diff --git a/src/sql/drivers/psql/qsql_psql.cpp b/src/sql/drivers/psql/qsql_psql.cpp index e3533581ba..ec31d54f0f 100644 --- a/src/sql/drivers/psql/qsql_psql.cpp +++ b/src/sql/drivers/psql/qsql_psql.cpp @@ -107,18 +107,10 @@ template inline void PQfreemem(T *t, int = 0) { free(t); } -QT_BEGIN_NAMESPACE namespace QtPrivate { -template <> struct IsPointerToTypeDerivedFromQObject { - enum { Value = false }; -}; -} QT_END_NAMESPACE +Q_DECLARE_OPAQUE_POINTER(PGconn*) Q_DECLARE_METATYPE(PGconn*) -QT_BEGIN_NAMESPACE namespace QtPrivate { -template <> struct IsPointerToTypeDerivedFromQObject { - enum { Value = false }; -}; -} QT_END_NAMESPACE +Q_DECLARE_OPAQUE_POINTER(PGresult*) Q_DECLARE_METATYPE(PGresult*) QT_BEGIN_NAMESPACE diff --git a/src/sql/drivers/sqlite/qsql_sqlite.cpp b/src/sql/drivers/sqlite/qsql_sqlite.cpp index 962fc97dfc..d2dc5af070 100644 --- a/src/sql/drivers/sqlite/qsql_sqlite.cpp +++ b/src/sql/drivers/sqlite/qsql_sqlite.cpp @@ -59,18 +59,10 @@ #include -QT_BEGIN_NAMESPACE namespace QtPrivate { -template <> struct IsPointerToTypeDerivedFromQObject { - enum { Value = false }; -}; -} QT_END_NAMESPACE +Q_DECLARE_OPAQUE_POINTER(sqlite3*) Q_DECLARE_METATYPE(sqlite3*) -QT_BEGIN_NAMESPACE namespace QtPrivate { -template <> struct IsPointerToTypeDerivedFromQObject { - enum { Value = false }; -}; -} QT_END_NAMESPACE +Q_DECLARE_OPAQUE_POINTER(sqlite3_stmt*) Q_DECLARE_METATYPE(sqlite3_stmt*) QT_BEGIN_NAMESPACE diff --git a/src/sql/drivers/sqlite2/qsql_sqlite2.cpp b/src/sql/drivers/sqlite2/qsql_sqlite2.cpp index 46127923bf..b42f82e800 100644 --- a/src/sql/drivers/sqlite2/qsql_sqlite2.cpp +++ b/src/sql/drivers/sqlite2/qsql_sqlite2.cpp @@ -60,15 +60,10 @@ typedef struct sqlite_vm sqlite_vm; -QT_BEGIN_NAMESPACE namespace QtPrivate { -template <> struct IsPointerToTypeDerivedFromQObject { - enum { Value = false }; -}; -template <> struct IsPointerToTypeDerivedFromQObject { - enum { Value = false }; -}; -} QT_END_NAMESPACE +Q_DECLARE_OPAQUE_POINTER(sqlite_vm*) Q_DECLARE_METATYPE(sqlite_vm*) + +Q_DECLARE_OPAQUE_POINTER(sqlite*) Q_DECLARE_METATYPE(sqlite*) QT_BEGIN_NAMESPACE diff --git a/src/sql/drivers/tds/qsql_tds.cpp b/src/sql/drivers/tds/qsql_tds.cpp index 2a97a3c392..b9414499d8 100644 --- a/src/sql/drivers/tds/qsql_tds.cpp +++ b/src/sql/drivers/tds/qsql_tds.cpp @@ -63,6 +63,9 @@ #include +Q_DECLARE_OPAQUE_POINTER(LOGINREC*) +Q_DECLARE_OPAQUE_POINTER(DBPROCESS*) + QT_BEGIN_NAMESPACE #ifdef DBNTWIN32 @@ -127,18 +130,6 @@ QT_BEGIN_NAMESPACE #define CS_PUBLIC #endif -namespace QtPrivate { -template <> struct IsPointerToTypeDerivedFromQObject { - enum { Value = false }; -}; -} - -namespace QtPrivate { -template <> struct IsPointerToTypeDerivedFromQObject { - enum { Value = false }; -}; -} - QSqlError qMakeError(const QString& err, QSqlError::ErrorType type, int errNo = -1) { return QSqlError(QLatin1String("QTDS: ") + err, QString(), type, errNo); -- cgit v1.2.3