/* This file is a part of Calyp project * Copyright (C) 2014-2021 by Joao Carreira (jfmcarreira@gmail.com) * Luis Lucas (luisfrlucas@gmail.com) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ /** * \file ResourceHandle.h * \brief Handle multiple sub-windows */ #ifndef __RESOURCEHANDLE_H__ #define __RESOURCEHANDLE_H__ #include #include #include #include #include #include #include "CalypResource.h" #include "CommonDefs.h" #include "lib/CalypStream.h" class ResourceWorker : public QThread { private: QMutex m_Mutex; QWaitCondition m_ResourceIdle; std::shared_ptr m_resource; std::atomic m_bStop{ false }; std::atomic m_bStarted{ false }; std::atomic m_bStarting{ false }; public: ResourceWorker( std::shared_ptr resource ) : m_resource{ std::move( resource ) } {} ResourceWorker( ResourceWorker&& other ) noexcept = delete; ResourceWorker& operator=( ResourceWorker&& other ) noexcept = delete; ResourceWorker( const ResourceWorker& other ) = delete; ResourceWorker& operator=( const ResourceWorker& other ) = delete; ~ResourceWorker(); void stop(); void wake(); void start(); void run() override; auto mutex() -> QMutex* { return &m_Mutex; } }; class ResourceWorker; class ResourceHandle : public QObject { Q_OBJECT public: ResourceHandle( QObject* parent ); ResourceHandle( ResourceHandle&& other ) noexcept = delete; ResourceHandle& operator=( ResourceHandle&& other ) noexcept = delete; ResourceHandle( const ResourceHandle& other ) = delete; ResourceHandle& operator=( const ResourceHandle& other ) = delete; ~ResourceHandle(); auto getResource( CalypStream* ptr ) -> std::size_t; auto getResourceAsset( std::size_t id ) -> CalypStream*; auto appendResource( std::unique_ptr&& resource ) -> std::size_t; void removeResource( std::size_t id ); void stopResourceWorker( std::size_t id ); void startResourceWorker( std::size_t id ); void wakeResourceWorker( std::size_t id ); template auto executeResourceAction( std::size_t id, const std::function& action ) -> bool { static_assert( std::is_base_of::value, "T must inherit from CalypResource" ); if( !m_apcStreamResourcesList.count( id ) ) { assert( false ); return false; } bool result{ false }; if( auto resource = dynamic_cast( m_apcStreamResourcesList[id].get() ) ) { QMutexLocker locker( m_apcStreamResourcesWorkersList[id]->mutex() ); result = action( resource ); } m_apcStreamResourcesWorkersList[id]->wake(); return result; } private: auto addResource() -> std::size_t; private: std::size_t unique_id{ 0 }; std::map> m_apcStreamResourcesList; std::map> m_apcStreamResourcesWorkersList; }; #endif // __RESOURCEHANDLE_H__