|
| 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 | +#pragma once |
| 31 | + |
| 32 | +#include <memory> |
| 33 | +#include <vector> |
| 34 | +#include <QObject> |
| 35 | + |
| 36 | +namespace facelift { |
| 37 | + |
| 38 | +class IObserver : public QObject |
| 39 | +{ |
| 40 | + Q_OBJECT |
| 41 | +public: |
| 42 | + virtual void onReadyChanged(std::shared_ptr<QMetaObject::Connection> connection) = 0; |
| 43 | +}; |
| 44 | + |
| 45 | +class IsReadyObserver: public QObject |
| 46 | +{ |
| 47 | + Q_OBJECT |
| 48 | + std::vector<IObserver *> m_observers; |
| 49 | + std::shared_ptr<QMetaObject::Connection> connection; |
| 50 | +public: |
| 51 | + IsReadyObserver() : connection{std::make_shared<QMetaObject::Connection>()} |
| 52 | + { |
| 53 | + *connection = QObject::connect(this, &IsReadyObserver::readyChanged, this, [ this ](){ |
| 54 | + for (auto observer : m_observers) { |
| 55 | + observer->onReadyChanged( connection ); |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + // Set observers |
| 60 | + void setObservers(const std::vector<IObserver *> &observers){ |
| 61 | + m_observers = observers; |
| 62 | + } |
| 63 | + |
| 64 | + // Get observers |
| 65 | + const std::vector<IObserver *>& getObservers() const { |
| 66 | + return m_observers; |
| 67 | + } |
| 68 | + |
| 69 | + Q_SIGNAL void readyChanged(); |
| 70 | + void onReadyChanged(){ |
| 71 | + emit readyChanged(); |
| 72 | + }; |
| 73 | +}; |
| 74 | + |
| 75 | +// Single-time observer which will unregister itself when done |
| 76 | +template<typename Function> |
| 77 | +class SingleTimeObserver : public IObserver |
| 78 | +{ |
| 79 | + Function m_func; |
| 80 | +public: |
| 81 | + explicit SingleTimeObserver(Function func) : m_func{func} {} |
| 82 | + ~SingleTimeObserver() = default; |
| 83 | + |
| 84 | + void onReadyChanged(std::shared_ptr<QMetaObject::Connection> connection) override { |
| 85 | + m_func(); |
| 86 | + QObject::disconnect(*connection); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +// Standard observer which will work for each signal |
| 91 | +template<typename Function> |
| 92 | +class StandartObserver : public IObserver |
| 93 | +{ |
| 94 | + Function m_func; |
| 95 | +public: |
| 96 | + explicit StandartObserver(Function func) : m_func{func} {} |
| 97 | + ~StandartObserver() = default; |
| 98 | + |
| 99 | + void onReadyChanged(std::shared_ptr<QMetaObject::Connection> ) override { |
| 100 | + m_func(); |
| 101 | + } |
| 102 | +}; |
| 103 | +} |
0 commit comments