mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-05 17:35:34 +08:00
Issue #15 Added CPassiveObserver and CPassiveMutator
These implement a many-to-many publish/subscribe pattern.
This commit is contained in:
22
src/blackmisc/sharedstate/passivemutator.cpp
Normal file
22
src/blackmisc/sharedstate/passivemutator.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/* Copyright (C) 2017
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#include "blackmisc/sharedstate/passivemutator.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace SharedState
|
||||
{
|
||||
void CPassiveMutator::postEvent(const CVariant ¶m)
|
||||
{
|
||||
emit eventPosted(param);
|
||||
}
|
||||
}
|
||||
}
|
||||
53
src/blackmisc/sharedstate/passivemutator.h
Normal file
53
src/blackmisc/sharedstate/passivemutator.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/* Copyright (C) 2017
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKMISC_SHAREDSTATE_PASSIVEMUTATOR_H
|
||||
#define BLACKMISC_SHAREDSTATE_PASSIVEMUTATOR_H
|
||||
|
||||
#include "blackmisc/variant.h"
|
||||
#include "blackmisc/blackmiscexport.h"
|
||||
#include <QObject>
|
||||
#include <QSharedPointer>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace SharedState
|
||||
{
|
||||
class CActiveMutator;
|
||||
|
||||
/*!
|
||||
* Endpoint which can emit events to subscribers.
|
||||
*/
|
||||
class BLACKMISC_EXPORT CPassiveMutator : public QObject, public QEnableSharedFromThis<CPassiveMutator>
|
||||
{
|
||||
Q_OBJECT
|
||||
friend CActiveMutator;
|
||||
friend QSharedPointer<CPassiveMutator>;
|
||||
|
||||
CPassiveMutator(QObject* parent) : QObject(parent) {}
|
||||
|
||||
public:
|
||||
//! Factory method.
|
||||
static auto create(QObject *parent) { return QSharedPointer<CPassiveMutator>::create(parent); }
|
||||
|
||||
//! Emit an event.
|
||||
void postEvent(const CVariant ¶m);
|
||||
|
||||
//! Get a QWeakPointer pointing to this object.
|
||||
QWeakPointer<const CPassiveMutator> weakRef() const { return sharedFromThis(); }
|
||||
|
||||
signals:
|
||||
//! Emitted by postEvent.
|
||||
void eventPosted(const BlackMisc::CVariant ¶m);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
36
src/blackmisc/sharedstate/passiveobserver.cpp
Normal file
36
src/blackmisc/sharedstate/passiveobserver.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
/* Copyright (C) 2017
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#include "blackmisc/sharedstate/passiveobserver.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace SharedState
|
||||
{
|
||||
void CPassiveObserver::setEventSubscription(const CVariant ¶m)
|
||||
{
|
||||
QMutexLocker lock(&m_eventSubscriptionMutex);
|
||||
m_eventSubscription = param;
|
||||
lock.unlock();
|
||||
emit eventSubscriptionChanged(param);
|
||||
}
|
||||
|
||||
CVariant CPassiveObserver::eventSubscription() const
|
||||
{
|
||||
QMutexLocker lock(&m_eventSubscriptionMutex);
|
||||
return m_eventSubscription;
|
||||
}
|
||||
|
||||
void CPassiveObserver::handleEvent(const CVariant& param) const
|
||||
{
|
||||
m_eventHandler(param);
|
||||
}
|
||||
}
|
||||
}
|
||||
92
src/blackmisc/sharedstate/passiveobserver.h
Normal file
92
src/blackmisc/sharedstate/passiveobserver.h
Normal file
@@ -0,0 +1,92 @@
|
||||
/* Copyright (C) 2017
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKMISC_SHAREDSTATE_PASSIVEOBSERVER_H
|
||||
#define BLACKMISC_SHAREDSTATE_PASSIVEOBSERVER_H
|
||||
|
||||
#include "blackmisc/variant.h"
|
||||
#include "blackmisc/valueobject.h"
|
||||
#include "blackmisc/blackmiscexport.h"
|
||||
#include <QObject>
|
||||
#include <QSharedPointer>
|
||||
#include <QMutex>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace SharedState
|
||||
{
|
||||
class CActiveObserver;
|
||||
|
||||
/*!
|
||||
* Endpoint which can subscribe to events emitted by CPassiveMutator.
|
||||
*/
|
||||
class BLACKMISC_EXPORT CPassiveObserver : public QObject, public QEnableSharedFromThis<CPassiveObserver>
|
||||
{
|
||||
Q_OBJECT
|
||||
friend CActiveObserver;
|
||||
friend QSharedPointer<CPassiveObserver>;
|
||||
|
||||
template <typename T, typename F>
|
||||
CPassiveObserver(T *parent, F eventHandler) :
|
||||
QObject(parent),
|
||||
m_eventHandler([ = ](const CVariant ¶m) { Private::invokeMethod(parent, eventHandler, param); })
|
||||
{}
|
||||
|
||||
public:
|
||||
//! Factory method.
|
||||
template <typename T, typename F>
|
||||
static auto create(T *parent, F eventHandler) { return QSharedPointer<CPassiveObserver>::create(parent, eventHandler); }
|
||||
|
||||
//! Set the object that determines which events are subscribed to.
|
||||
void setEventSubscription(const CVariant ¶m);
|
||||
|
||||
//! Get the object that determines which events are subscribed to.
|
||||
CVariant eventSubscription() const;
|
||||
|
||||
//! Called when a subscribed event is emitted.
|
||||
void handleEvent(const CVariant& param) const;
|
||||
|
||||
//! Get a QWeakPointer pointing to this object.
|
||||
QWeakPointer<const CPassiveObserver> weakRef() const { return sharedFromThis(); }
|
||||
|
||||
signals:
|
||||
//! Emitted by setEventSubscription.
|
||||
void eventSubscriptionChanged(const BlackMisc::CVariant ¶m);
|
||||
|
||||
private:
|
||||
const std::function<void(const CVariant ¶m)> m_eventHandler;
|
||||
|
||||
mutable QMutex m_eventSubscriptionMutex;
|
||||
CVariant m_eventSubscription;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Dummy value class that matches any event.
|
||||
*/
|
||||
class BLACKMISC_EXPORT CAnyMatch : public CValueObject<CAnyMatch>
|
||||
{
|
||||
public:
|
||||
//! Returns true.
|
||||
bool matches(const CVariant &) const { return true; }
|
||||
|
||||
//! To string.
|
||||
QString convertToQString(bool = false) const { return QStringLiteral("any"); }
|
||||
|
||||
private:
|
||||
int m_dummy = 0;
|
||||
|
||||
BLACK_METACLASS(CAnyMatch, BLACK_METAMEMBER(dummy));
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Q_DECLARE_METATYPE(BlackMisc::SharedState::CAnyMatch)
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user