mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-10 22:15:34 +08:00
Issue #15 Classes for sharing a single value object,
built on top of the lower level API
This commit is contained in:
78
src/blackmisc/sharedstate/scalarobserver.h
Normal file
78
src/blackmisc/sharedstate/scalarobserver.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/* 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_SCALAROBSERVER_H
|
||||
#define BLACKMISC_SHAREDSTATE_SCALAROBSERVER_H
|
||||
|
||||
#include "blackmisc/sharedstate/activeobserver.h"
|
||||
#include "blackmisc/variant.h"
|
||||
#include "blackmisc/blackmiscexport.h"
|
||||
#include <QObject>
|
||||
#include <QMutex>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace SharedState
|
||||
{
|
||||
class IDataLink;
|
||||
|
||||
/*!
|
||||
* Non-template base class for CScalarObserver.
|
||||
*/
|
||||
class BLACKMISC_EXPORT CGenericScalarObserver : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! Subscribe using the given transport mechanism.
|
||||
void initialize(IDataLink *);
|
||||
|
||||
protected:
|
||||
//! Constructor.
|
||||
CGenericScalarObserver(QObject *parent) : QObject(parent) {}
|
||||
|
||||
//! Get scalar value as variant.
|
||||
CVariant value() const;
|
||||
|
||||
private:
|
||||
void reconstruct();
|
||||
void handleEvent(const CVariant ¶m);
|
||||
virtual void onGenericValueChanged(const CVariant &value) = 0;
|
||||
|
||||
QSharedPointer<CActiveObserver> m_observer = CActiveObserver::create(this, &CGenericScalarObserver::handleEvent);
|
||||
mutable QMutex m_valueMutex;
|
||||
CVariant m_value;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Base class for an object that shares state with a corresponding CScalarMutator subclass object.
|
||||
* \tparam T Datatype encapsulating the state to be shared.
|
||||
*/
|
||||
template <typename T>
|
||||
class CScalarObserver : public CGenericScalarObserver
|
||||
{
|
||||
protected:
|
||||
//! Constructor.
|
||||
CScalarObserver(QObject *parent) : CGenericScalarObserver(parent) {}
|
||||
|
||||
public:
|
||||
//! Get scalar value.
|
||||
T value() const { return CGenericScalarObserver::value().template to<T>(); }
|
||||
|
||||
private:
|
||||
//! Called when the scalar value changes.
|
||||
virtual void onValueChanged(const T &value) = 0;
|
||||
|
||||
virtual void onGenericValueChanged(const CVariant &value) override final { onValueChanged(value.to<T>()); }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user