Commit 9d172798 authored by NICLAUSSE Nicolas's avatar NICLAUSSE Nicolas
Browse files

add del, keys and value method in dtkObjectManager API

parent 0ba91e13
......@@ -20,9 +20,9 @@
The QVariant is defined by the QMetaType \a type and the address of the object \a t.
*/
void dtkCorePluginFactoryPrivate::touch(int type, void *t) const
void dtkCorePluginFactoryPrivate::touch(const QVariant &v) const
{
emit created(QVariant(type, t));
emit created(v);
}
/*! \fn void dtkCorePluginFactoryPrivate::connect(dtkObjectManager *manager)
......
......@@ -33,7 +33,7 @@ signals:
void created(const QVariant&) const;
public:
void touch(int type, void *t) const;
void touch(const QVariant&) const;
public:
void connect(dtkObjectManager *manager) const;
......
......@@ -14,6 +14,7 @@
#pragma once
#include <dtkMeta>
// ///////////////////////////////////////////////////////////////////
// dtkCorePluginFactory implementation
// ///////////////////////////////////////////////////////////////////
......@@ -79,7 +80,7 @@ template <typename T> T *dtkCorePluginFactory<T>::create(const QString& key) con
}
if (obj) {
m_d->touch(qMetaTypeId<T *>(), obj);
m_d->touch(dtkMetaType::variantFromValue(obj));
}
return obj;
......
......@@ -37,7 +37,30 @@ void dtkObjectManager::add(const QVariant& instance)
{
static int count = 0;
d->instances.insert(QString(instance.typeName()) + " " + count++, instance);
d->instances.insert(QString("%1 %2").arg(instance.typeName()).arg(count++), instance);
}
QVariant dtkObjectManager::value(const QString& key) const
{
return d->instances.value(key);
}
QStringList dtkObjectManager::keys(void) const
{
return d->instances.keys();
}
bool dtkObjectManager::del(const QVariant& v)
{
QVariantMap::const_iterator i = d->instances.begin();
while (i != d->instances.end()) {
if (i.value() == v) {
d->instances.remove(i.key());
return true;
}
++i;
}
return false;
}
dtkObjectManager::dtkObjectManager(void)
......
......@@ -30,6 +30,11 @@ public:
public slots:
void add(const QVariant&);
bool del(const QVariant&);
public:
QVariant value(const QString& key) const;
QStringList keys(void) const;
protected:
static dtkObjectManager *s_instance;
......
......@@ -23,9 +23,15 @@
//
// ///////////////////////////////////////////////////////////////////
dtkObjectManagerTestCase::dtkObjectManagerTestCase(void)
class dtkObjectManagerTestCasePrivate
{
public:
dtkDistributedCommunicator *comm_1;
};
dtkObjectManagerTestCase::dtkObjectManagerTestCase(void): d(new dtkObjectManagerTestCasePrivate)
{
d->comm_1 = nullptr;
}
dtkObjectManagerTestCase::~dtkObjectManagerTestCase(void)
......@@ -35,13 +41,15 @@ dtkObjectManagerTestCase::~dtkObjectManagerTestCase(void)
void dtkObjectManagerTestCase::initTestCase(void)
{
QMetaType::registerComparators<dtkDistributedCommunicator*>();
dtkDistributed::communicator::pluginFactory().connect(dtkObjectManager::instance());
dtkDistributed::communicator::initialize();
dtkDistributedCommunicator *communicator_1 = dtkDistributed::communicator::pluginFactory().create("qthread");
d->comm_1 = dtkDistributed::communicator::pluginFactory().create("qthread");
dtkDistributedCommunicator *communicator_2 = dtkDistributed::communicator::pluginFactory().create("qthread");
QVERIFY(communicator_1 != nullptr);
QVERIFY(d->comm_1 != nullptr);
QVERIFY(communicator_2 != nullptr);
}
......@@ -55,6 +63,35 @@ void dtkObjectManagerTestCase::testCount(void)
QCOMPARE(dtkObjectManager::instance()->count() , 2);
}
void dtkObjectManagerTestCase::testValue(void)
{
QVariant v = dtkObjectManager::instance()->value(QString("dtkDistributedCommunicator* 0"));
dtkDistributedCommunicator * comm = v.value<dtkDistributedCommunicator*>();
QCOMPARE(comm, d->comm_1);
}
void dtkObjectManagerTestCase::testKeys(void)
{
QCOMPARE(dtkObjectManager::instance()->keys()[0] , QString("dtkDistributedCommunicator* 0"));
QCOMPARE(dtkObjectManager::instance()->keys()[1] , QString("dtkDistributedCommunicator* 1"));
}
void dtkObjectManagerTestCase::testDel(void)
{
QVariant v;
QCOMPARE(dtkObjectManager::instance()->del(v), false);
v.setValue(d->comm_1);
QCOMPARE(dtkObjectManager::instance()->del(v), true);
QCOMPARE(dtkObjectManager::instance()->count() , 1);
v = dtkObjectManager::instance()->value(QString("dtkDistributedCommunicator* 1"));
dtkDistributedCommunicator * comm = v.value<dtkDistributedCommunicator*>();
QCOMPARE(dtkObjectManager::instance()->del(v), true);
QCOMPARE(dtkObjectManager::instance()->count() , 0);
QCOMPARE(dtkObjectManager::instance()->del(v), false);
QCOMPARE(dtkObjectManager::instance()->count() , 0);
}
void dtkObjectManagerTestCase::cleanup(void)
{
......
......@@ -16,6 +16,9 @@
#include <dtkTest>
class dtkObjectManagerTestCasePrivate;
class dtkObjectManagerTestCase : public QObject
{
Q_OBJECT
......@@ -30,10 +33,16 @@ private slots:
private slots:
void testCount(void);
void testValue(void);
void testKeys(void);
void testDel(void);
private slots:
void cleanupTestCase(void);
void cleanup(void);
private:
dtkObjectManagerTestCasePrivate *d;
};
//
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment