Skip to content

Commit

Permalink
Added QPointer
Browse files Browse the repository at this point in the history
  • Loading branch information
filcuc committed Aug 2, 2020
1 parent 6ec7057 commit f357c6e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
21 changes: 20 additions & 1 deletion lib/include/DOtherSide/DOtherSide.h
Original file line number Diff line number Diff line change
Expand Up @@ -917,8 +917,27 @@ DOS_API int DOS_CALL dos_qdeclarative_qmlregistersingletontype(const QmlRegister

/// @}

DOS_API DosQObject

/// \defgroup QPointer QPointer
/// @{

/// \brief Create a new QPointer with the given DosQObject
DOS_API DosQPointer* DOS_CALL dos_qpointer_create(DosQObject* object);

/// \brief Free the memory allocated for the given QPointer
DOS_API void DOS_CALL dos_qpointer_delete(DosQPointer* self);

/// \brief Test the QPointer for nullness
DOS_API bool DOS_CALL dos_qpointer_is_null(DosQPointer* self);

/// \brief Clear the QPointer
DOS_API void DOS_CALL dos_qpointer_clear(DosQPointer* self);

/// \brief Return a pointer to the tracked DosQObject
/// \note The return DosQObject is a reference and should not be fred unless you know what you're doing
DOS_API DosQObject* DOS_CALL dos_qpointer_data(DosQPointer* self);

/// @}

#ifdef __cplusplus
}
Expand Down
3 changes: 3 additions & 0 deletions lib/include/DOtherSide/DOtherSideTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ typedef void DosQQuickImageProvider;
/// A pointer to a QPixmap
typedef void DosPixmap;

/// A pointer to a QPointer
typedef void DosQPointer;

/// A pixmap callback to be supplied to an image provider
/// \param id Image source id
/// \param width pointer to the width of the image
Expand Down
27 changes: 27 additions & 0 deletions lib/src/DOtherSide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QtCore/QDebug>
#include <QtCore/QModelIndex>
#include <QtCore/QHash>
#include <QtCore/QPointer>
#include <QtCore/QResource>
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlContext>
Expand Down Expand Up @@ -1075,3 +1076,29 @@ char *dos_signal_macro(const char *str)
{
return ::strdup((std::string("2") + str).c_str());
}

DosQPointer *dos_qpointer_create(DosQObject *object)
{
return new QPointer<QObject>(static_cast<QObject*>(object));
}

void dos_qpointer_delete(DosQPointer *self)
{
delete static_cast<QPointer<QObject>*>(self);
}

bool dos_qpointer_is_null(DosQPointer *self)
{
return static_cast<QPointer<QObject>*>(self)->isNull();
}

void dos_qpointer_clear(DosQPointer *self)
{
static_cast<QPointer<QObject>*>(self)->clear();
}

DosQObject* dos_qpointer_data(DosQPointer *self)
{
return static_cast<QPointer<QObject>*>(self)->data();
}

19 changes: 18 additions & 1 deletion test/test_dotherside.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,22 @@ private slots:
}
};

class TestQPointer : public QObject
{
Q_OBJECT

private slots:
void testQPointer() {
MockQObject m;
auto e = dos_qpointer_create(m.data());
QVERIFY(!dos_qpointer_is_null(e));
QCOMPARE(dos_qpointer_data(e), m.data());
dos_qpointer_clear(e);
QVERIFY(dos_qpointer_is_null(e));
dos_qpointer_delete(e);
}
};

bool TestQMetaObject::called = false;

int main(int argc, char *argv[])
Expand All @@ -795,7 +811,8 @@ int main(int argc, char *argv[])
success &= ExecuteGuiTest<TestQAbstractItemModel>(argc, argv);
success &= ExecuteGuiTest<TestQDeclarativeIntegration>(argc, argv);
success &= ExecuteGuiTest<TestQQuickView>(argc, argv);
success &= ExecuteGuiTest<TestQMetaObject>(argc, argv);
success &= ExecuteTest<TestQMetaObject>(argc, argv);
success &= ExecuteTest<TestQPointer>(argc, argv);
return success ? 0 : 1;
}

Expand Down

0 comments on commit f357c6e

Please # to comment.