|
| 1 | +/********************************************************************** |
| 2 | +** |
| 3 | +** Copyright (C) 2020 Luxoft Sweden AB |
| 4 | +** |
| 5 | +** This file is part of the FaceLift project |
| 6 | +** |
| 7 | +** Permission is hereby granted, freIPCServiceAdapterBasee of charge, to any person |
| 8 | +** obtaining a copy of this software and associated documentation files |
| 9 | +** (the "Software"), to deal in the Software without restriction, |
| 10 | +** including without limitation the rights to use, copy, modify, merge, |
| 11 | +** publish, distribute, sublicense, and/or sell copies of the Software, |
| 12 | +** and to permit persons to whom the Software is furnished to do so, |
| 13 | +** subject to the following conditions: |
| 14 | +** |
| 15 | +** The above copyright notice and this permission notice shall be |
| 16 | +** included in all copies or substantial portions of the Software. |
| 17 | +** |
| 18 | +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 22 | +** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 23 | +** ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | +** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +** SOFTWARE. |
| 26 | +** |
| 27 | +** SPDX-License-Identifier: MIT |
| 28 | +** |
| 29 | +**********************************************************************/ |
| 30 | + |
| 31 | +#pragma once |
| 32 | + |
| 33 | +#include <memory> |
| 34 | +#include <QVector> |
| 35 | +#include <QObject> |
| 36 | + |
| 37 | +namespace facelift { |
| 38 | + |
| 39 | +class IObserver : public QObject |
| 40 | +{ |
| 41 | + Q_OBJECT |
| 42 | +public: |
| 43 | + virtual void onReadyChanged(std::shared_ptr<QMetaObject::Connection> connection) = 0; |
| 44 | +}; |
| 45 | + |
| 46 | +class IsReadyObserver: public QObject |
| 47 | +{ |
| 48 | + Q_OBJECT |
| 49 | + QVector<IObserver *> m_observers{}; |
| 50 | + |
| 51 | +public: |
| 52 | + IsReadyObserver() {} |
| 53 | + |
| 54 | + // Set observers |
| 55 | + void setObservers(const QVector<IObserver *> &observers) { |
| 56 | + m_observers = observers; |
| 57 | + for(auto observer: observers){ |
| 58 | + auto connection = std::make_shared<QMetaObject::Connection>(); |
| 59 | + *connection = QObject::connect(this, &IsReadyObserver::readyChanged, observer, [observer, connection](){ |
| 60 | + observer->onReadyChanged( connection ); |
| 61 | + }); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Get observers |
| 66 | + const QVector<IObserver *> &getObservers() const { |
| 67 | + return m_observers; |
| 68 | + } |
| 69 | + |
| 70 | + Q_SIGNAL void readyChanged(); |
| 71 | + |
| 72 | + void onReadyChanged() { |
| 73 | + emit readyChanged(); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +// Single-time observer which will unregister itself when done |
| 78 | +template<typename T> |
| 79 | +class SingleTimeObserver : public IObserver |
| 80 | +{ |
| 81 | + T m_function; |
| 82 | + |
| 83 | +public: |
| 84 | + explicit SingleTimeObserver(T function) : m_function{function} {} |
| 85 | + ~SingleTimeObserver() = default; |
| 86 | + |
| 87 | + void onReadyChanged(std::shared_ptr<QMetaObject::Connection> connection) override { |
| 88 | + m_function(); |
| 89 | + QObject::disconnect(*connection); |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +// Standard observer which will work for each signal |
| 94 | +template<typename T> |
| 95 | +class StandartObserver : public IObserver |
| 96 | +{ |
| 97 | + T m_function; |
| 98 | + |
| 99 | +public: |
| 100 | + explicit StandartObserver(T function) : m_function{function} {} |
| 101 | + ~StandartObserver() = default; |
| 102 | + |
| 103 | + void onReadyChanged(std::shared_ptr<QMetaObject::Connection> ) override { |
| 104 | + m_function(); |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +} |
0 commit comments