Issue #15 Classes for sharing a single value object,

built on top of the lower level API
This commit is contained in:
Mat Sutcliffe
2019-02-26 01:51:54 +00:00
parent 538421b0b7
commit 805cec7caf
6 changed files with 320 additions and 0 deletions

View 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 &param)
{
QMutexLocker lock(&m_valueMutex);
if (m_value == param) { return; }
m_value = param;
lock.unlock();
onGenericValueChanged(param);
}
}
}