Consider QObject could be null in CSlot

This commit is contained in:
Roland Winklmeier
2016-05-25 16:58:59 +02:00
parent 4b441c5d56
commit 26a72d5eb2
4 changed files with 97 additions and 3 deletions

View File

@@ -62,13 +62,13 @@ namespace BlackMisc
//! True if the slot can be called, false if it is empty.
operator bool() const
{
return !this->isEmpty();
return !this->isEmpty() && !this->hasNullObject();
}
//! True if the slot is empty, false if it can be called.
//! True if the slot is empty or object is null, false if it can be called.
bool operator !() const
{
return this->isEmpty();
return this->isEmpty() || this->hasNullObject();
}
//! True if the slot is empty, false if it can be called.
@@ -77,6 +77,12 @@ namespace BlackMisc
return ! m_function;
}
//! True if the object is null
bool hasNullObject() const
{
return m_object.isNull();
}
private:
QPointer<QObject> m_object;
std::function<R(Args...)> m_function;

View File

@@ -23,6 +23,7 @@
#include "testlibrarypath.h"
#include "testmath.h"
#include "testphysicalquantities.h"
#include "testslot.h"
#include "testvaluecache.h"
#include "testvariantandmap.h"
#include "testweather.h"
@@ -45,6 +46,7 @@ namespace BlackMiscTest
CTestVariantAndMap variantAndMap;
CTestInput inputTests;
CTestIdentifier identifierTests;
CTestSlot slotTests;
CTestValueCache valueCacheTests;
CTestWeather weatherTests;
CTestMath mathTests;
@@ -56,6 +58,7 @@ namespace BlackMiscTest
status |= QTest::qExec(&containerTests, argc, argv);
status |= QTest::qExec(&variantAndMap, argc, argv);
status |= QTest::qExec(&inputTests, argc, argv);
status |= QTest::qExec(&slotTests, argc, argv);
status |= QTest::qExec(&valueCacheTests, argc, argv);
status |= QTest::qExec(&weatherTests, argc, argv);
status |= QTest::qExec(&mathTests, argc, argv);

View File

@@ -0,0 +1,42 @@
/* Copyright (C) 2016
* swift Project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \cond PRIVATE_TESTS
/*!
* \file
* \ingroup testblackmisc
*/
#include "testslot.h"
#include "blackmisc/slot.h"
#include <QTest>
using namespace BlackMisc;
namespace BlackMiscTest
{
void CTestSlot::slotBasics()
{
QObject *obj = new QObject(this);
CSlot<void(const QString &)> slot1 = { obj, &QObject::setObjectName };
QVERIFY2(slot1, "Slot has valid object and function - can be called.");
CSlot<void(const QString &)> slot2 = { static_cast<QObject*>(nullptr), &QObject::setObjectName };
QVERIFY2(!slot2, "Slot has an invalid pointer - cannot be called.");
CSlot<void(const QString &)> slot3;
QVERIFY2(!slot3, "Slot has an invalid pointer and invalid function - cannot be called.");
}
} // namespace
//! \endcond

View File

@@ -0,0 +1,43 @@
/* Copyright (C) 2016
* swift project community / contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#ifndef BLACKMISCTEST_TESTSLOT_H
#define BLACKMISCTEST_TESTSLOT_H
//! \cond PRIVATE_TESTS
/*!
* \file
* \ingroup testblackmisc
*/
#include <QObject>
namespace BlackMiscTest
{
//! CSlot tests
class CTestSlot : public QObject
{
Q_OBJECT
public:
//! Standard test case constructor
explicit CTestSlot(QObject *parent = nullptr) : QObject(parent) {}
private slots:
//! Basic unit tests for CSlot
void slotBasics();
};
} // namespace
//! \endcond
#endif // guard