mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 16:56:53 +08:00
Issue #15 Classes for sharing a single value object,
built on top of the lower level API
This commit is contained in:
46
src/blackmisc/sharedstate/scalarobserver.cpp
Normal file
46
src/blackmisc/sharedstate/scalarobserver.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
/* 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/scalarobserver.h"
|
||||
#include "blackmisc/sharedstate/datalink.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace SharedState
|
||||
{
|
||||
void CGenericScalarObserver::initialize(IDataLink *dataLink)
|
||||
{
|
||||
dataLink->subscribe(m_observer.data());
|
||||
m_observer->setEventSubscription(CVariant::from(CAnyMatch()));
|
||||
connect(dataLink->watcher(), &CDataLinkConnectionWatcher::connected, this, &CGenericScalarObserver::reconstruct);
|
||||
if (dataLink->watcher()->isConnected()) { reconstruct(); }
|
||||
}
|
||||
|
||||
void CGenericScalarObserver::reconstruct()
|
||||
{
|
||||
m_observer->requestAsync({}, [this](const CVariant &value) { handleEvent(value); });
|
||||
}
|
||||
|
||||
CVariant CGenericScalarObserver::value() const
|
||||
{
|
||||
QMutexLocker lock(&m_valueMutex);
|
||||
return m_value;
|
||||
}
|
||||
|
||||
void CGenericScalarObserver::handleEvent(const CVariant ¶m)
|
||||
{
|
||||
QMutexLocker lock(&m_valueMutex);
|
||||
if (m_value == param) { return; }
|
||||
m_value = param;
|
||||
lock.unlock();
|
||||
onGenericValueChanged(param);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user