swift
Loading...
Searching...
No Matches
listobserver.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_LISTOBSERVER_H
7#define SWIFT_MISC_SHAREDSTATE_LISTOBSERVER_H
8
9#include <QMutex>
10#include <QObject>
11
15#include "misc/variantlist.h"
16
18{
24 {
26
27 protected:
30
32 virtual void initialize(IDataLink *);
33
35 void setFilter(const CVariant &filter);
36
38 CVariantList allValues() const;
39
41 int cleanValues();
42
43 private:
44 void reconstruct();
45 void handleEvent(const CVariant &param);
46 virtual void onGenericElementAdded(const CVariant &value) = 0;
47 virtual void onGenericElementsReplaced(const CVariantList &values) = 0;
48
49 QSharedPointer<CActiveObserver> m_observer = CActiveObserver::create(this, &CGenericListObserver::handleEvent);
50 CDataLinkConnectionWatcher *m_watcher = nullptr;
51 mutable QMutex m_listMutex;
52 CVariantList m_list;
53 };
54
61 template <typename T, typename U = CAnyMatch>
63 {
64 protected:
67
68 public:
70 void initialize(IDataLink *dataLink) override
71 {
73 if (std::is_same_v<U, CAnyMatch>)
74 {
75 setFilter({});
76 }
77 }
78
81
83 T allValues() const { return CVariant::fromValue(CGenericListObserver::allValues()).template value<T>(); }
84
86 virtual void onElementAdded(const typename T::value_type &value) = 0;
87
89 virtual void onElementsReplaced(const T &values) = 0;
90
91 private:
92 void onGenericElementAdded(const CVariant &value) final
93 {
94 onElementAdded(value.value<typename T::value_type>());
95 }
96
97 void onGenericElementsReplaced(const CVariantList &values) final { onElementsReplaced(values.to<T>()); }
98 };
99} // namespace swift::misc::shared_state
100
101#endif // SWIFT_MISC_SHAREDSTATE_LISTOBSERVER_H
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition variant.h:69
static CVariant fromValue(T &&value)
Construct a variant from a value.
Definition variant.h:139
Value object encapsulating a list of variants.
Definition variantlist.h:29
static auto create(T *parent, F eventHandler)
Factory method.
void setFilter(const CVariant &filter)
Set list filter as variant.
CGenericListObserver(QObject *parent)
Constructor.
virtual void initialize(IDataLink *)
Subscribe using the given transport mechanism.
CVariantList allValues() const
Get filtered list value as variant list.
virtual void onElementsReplaced(const T &values)=0
Called when the whole list is updated wholesale.
CListObserver(QObject *parent)
Constructor.
void setFilter(const U &filter)
Set filter to choose list elements.
void initialize(IDataLink *dataLink)
Subscribe using the given transport mechanism.
virtual void onElementAdded(const typename T::value_type &value)=0
Called when an element matching the filter is added to the list.
T allValues() const
Get list value containing all elements matching the filter.
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.