swift
Loading...
Searching...
No Matches
scalarobserver.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2017 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_SHAREDSTATE_SCALAROBSERVER_H
7#define SWIFT_MISC_SHAREDSTATE_SCALAROBSERVER_H
8
9#include <QMutex>
10#include <QObject>
11
14#include "misc/variant.h"
15
17{
18 class IDataLink;
19
25 {
27
28 public:
30 void initialize(IDataLink *);
31
32 protected:
35
37 CVariant value() const;
38
39 private:
40 void reconstruct();
41 void handleEvent(const CVariant &param);
42 virtual void onGenericValueChanged(const CVariant &value) = 0;
43
45 CActiveObserver::create(this, &CGenericScalarObserver::handleEvent);
46 mutable QMutex m_valueMutex;
47 CVariant m_value;
48 };
49
55 template <typename T>
57 {
58 protected:
61
62 public:
64 T value() const { return CGenericScalarObserver::value().template value<T>(); }
65
66 private:
68 virtual void onValueChanged(const T &value) = 0;
69
70 void onGenericValueChanged(const CVariant &value) final { onValueChanged(value.value<T>()); }
71 };
72} // namespace swift::misc::shared_state
73
74#endif // SWIFT_MISC_SHAREDSTATE_SCALAROBSERVER_H
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition variant.h:69
static auto create(T *parent, F eventHandler)
Factory method.
void initialize(IDataLink *)
Subscribe using the given transport mechanism.
CVariant value() const
Get scalar value as variant.
CGenericScalarObserver(QObject *parent)
Constructor.
virtual void onValueChanged(const T &value)=0
Called when the scalar value changes.
CScalarObserver(QObject *parent)
Constructor.
Utilities for sharing state between multiple objects.
Definition application.h:48
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
QObject * parent() const const
#define SWIFT_MISC_EXPORT
Export a class or function from the library.