swift
passiveobserver.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_PASSIVEOBSERVER_H
7 #define SWIFT_MISC_SHAREDSTATE_PASSIVEOBSERVER_H
8 
9 #include <QMutex>
10 #include <QObject>
11 #include <QSharedPointer>
12 
13 #include "misc/swiftmiscexport.h"
14 #include "misc/valueobject.h"
15 #include "misc/variant.h"
16 
18 
20 {
21  class CActiveObserver;
22 
27  class SWIFT_MISC_EXPORT CPassiveObserver : public QObject, public QEnableSharedFromThis<CPassiveObserver>
28  {
29  Q_OBJECT
30  friend CActiveObserver;
31  friend QSharedPointer<CPassiveObserver>;
32 
33  template <typename T, typename F>
34  CPassiveObserver(T *parent, F eventHandler)
35  : QObject(parent),
36  m_eventHandler([=](const CVariant &param) { private_ns::invokeMethod(parent, eventHandler, param); })
37  {}
38 
39  public:
41  template <typename T, typename F>
42  static auto create(T *parent, F eventHandler)
43  {
44  return QSharedPointer<CPassiveObserver>::create(parent, eventHandler);
45  }
46 
48  void setEventSubscription(const CVariant &param);
49 
51  CVariant eventSubscription() const;
52 
54  void handleEvent(const CVariant &param) const;
55 
57  QWeakPointer<const CPassiveObserver> weakRef() const { return sharedFromThis(); }
58 
59  signals:
62 
63  private:
64  const std::function<void(const CVariant &param)> m_eventHandler;
65 
66  mutable QMutex m_eventSubscriptionMutex;
67  CVariant m_eventSubscription;
68  };
69 
74  class SWIFT_MISC_EXPORT CAnyMatch : public CValueObject<CAnyMatch>
75  {
76  public:
78  bool matches(const CVariant &) const { return true; }
79 
81  QString convertToQString(bool = false) const { return QStringLiteral("any"); }
82 
83  private:
84  int m_dummy = 0;
85 
87  };
88 } // namespace swift::misc::shared_state
89 
90 Q_DECLARE_METATYPE(swift::misc::shared_state::CAnyMatch)
91 
92 #endif // SWIFT_MISC_SHAREDSTATE_PASSIVEOBSERVER_H
Mix of the most commonly used mixin classes.
Definition: valueobject.h:114
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
Extends CPassiveObserver with the ability to send requests and receive replies.
Dummy value class that matches any event.
bool matches(const CVariant &) const
Returns true.
QString convertToQString(bool=false) const
To string.
Endpoint which can subscribe to events emitted by CPassiveMutator.
static auto create(T *parent, F eventHandler)
Factory method.
QWeakPointer< const CPassiveObserver > weakRef() const
Get a QWeakPointer pointing to this object.
void eventSubscriptionChanged(const swift::misc::CVariant &param)
Emitted by setEventSubscription.
#define SWIFT_METAMEMBER(MEMBER,...)
Macro to define an element within a metaclass.
Definition: metaclass.h:73
#define SWIFT_METACLASS(CLASS,...)
Macro to define a nested metaclass that describes the attributes of its enclosing class.
Definition: metaclass.h:53
Utilities for sharing state between multiple objects.
Definition: application.h:48
#define SWIFT_MISC_EXPORT
Export a class or function from the library.
#define SWIFT_DECLARE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template declaration of mixins for a CValueObject subclass to be placed near the top of the ...
Definition: valueobject.h:65