swift
listobserver.cpp
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 
7 
9 #include "misc/variantlist.h"
10 
12 {
14  {
15  dataLink->subscribe(m_observer.data());
16  m_watcher = dataLink->watcher();
17  connect(m_watcher, &CDataLinkConnectionWatcher::connected, this, &CGenericListObserver::reconstruct);
18  }
19 
21  {
22  m_observer->setEventSubscription(filter);
23  if (m_watcher && m_watcher->isConnected()) { reconstruct(); }
24  }
25 
26  void CGenericListObserver::reconstruct()
27  {
28  m_observer->requestAsync(m_observer->eventSubscription(), [this](const CVariant &list) {
29  QMutexLocker lock(&m_listMutex);
30  m_list = list.to<CVariantList>();
31  lock.unlock();
32  onGenericElementsReplaced(allValues());
33  });
34  }
35 
37  {
38  QMutexLocker lock(&m_listMutex);
39  return m_list;
40  }
41 
43  {
44  QMutexLocker lock(&m_listMutex);
45  return m_list.removeIf(
46  [filter = m_observer->eventSubscription()](const CVariant &value) { return !value.matches(filter); });
47  }
48 
49  void CGenericListObserver::handleEvent(const CVariant &param)
50  {
51  QMutexLocker lock(&m_listMutex);
52  m_list.push_back(param);
53  lock.unlock();
54  onGenericElementAdded(param);
55  }
56 } // namespace swift::misc::shared_state
int removeIf(Predicate p)
Remove elements for which a given predicate returns true.
Definition: sequence.h:446
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
Value object encapsulating a list of variants.
Definition: variantlist.h:29
int cleanValues()
Remove any old values that no longer match the filter.
void setFilter(const CVariant &filter)
Set list filter as variant.
virtual void initialize(IDataLink *)
Subscribe using the given transport mechanism.
CVariantList allValues() const
Get filtered list value as variant list.
Utilities for sharing state between multiple objects.
Definition: application.h:48