| 
									
										
										
										
											2022-02-20 22:08:49 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2023-11-19 06:43:20 +01:00
										 |  |  | // Copyright (C) 2022 Jiří Pinkava <jiri.pinkava@rossum.ai>                      //
 | 
					
						
							|  |  |  | // Copyright (C) 2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com>               //
 | 
					
						
							| 
									
										
										
										
											2022-02-20 22:08:49 +01:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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 as version 3 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 V3 for more details.                               //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License             //
 | 
					
						
							|  |  |  | // along with this program. If not, see <http://www.gnu.org/licenses/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "objectpipesregistrations.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ObjectPipesRegistrations::ObjectPipesRegistrations(ObjectPipeElementsStore *objectPipeElementsStore) : | 
					
						
							|  |  |  |     m_typeCount(0), | 
					
						
							|  |  |  |     m_pipeId(0), | 
					
						
							| 
									
										
										
										
											2022-09-15 21:59:42 +02:00
										 |  |  |     m_objectPipeElementsStore(objectPipeElementsStore) | 
					
						
							| 
									
										
										
										
											2022-02-20 22:08:49 +01:00
										 |  |  | {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ObjectPipesRegistrations::~ObjectPipesRegistrations() | 
					
						
							|  |  |  | {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ObjectPipe *ObjectPipesRegistrations::registerProducerToConsumer(const QObject *producer, const QObject *consumer, const QString& type) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int typeId; | 
					
						
							|  |  |  |     QMutexLocker mlock(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_typeIds.contains(type)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         typeId = m_typeIds.value(type); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         typeId = m_typeCount++; | 
					
						
							|  |  |  |         m_typeIds.insert(type, typeId); | 
					
						
							|  |  |  |         m_types.insert(typeId, type); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (auto& pipe : m_pipes) // check if pipe exists already - there is a unique pipe per producer, consumer and type
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-03-28 20:12:25 +02:00
										 |  |  |         if ((producer == pipe->m_producer) && (consumer == pipe->m_consumer) && (typeId == pipe->m_typeId)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             qDebug("ObjectPipesRegistrations::registerProducerToConsumer: return existing pipe %p %p %s %s", | 
					
						
							|  |  |  |                 producer, consumer, qPrintable(pipe->m_element->objectName()), qPrintable(type)); | 
					
						
							|  |  |  |             pipe->unsetToBeDeleted(); | 
					
						
							|  |  |  |             if (!m_producerPipes[producer].contains(pipe)) { | 
					
						
							|  |  |  |                 m_producerPipes[producer].push_back(pipe); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (!m_consumerPipes[consumer].contains(pipe)) { | 
					
						
							|  |  |  |                 m_consumerPipes[consumer].push_back(pipe); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (!m_typeIdPipes[typeId].contains(pipe)) { | 
					
						
							|  |  |  |                 m_typeIdPipes[typeId].push_back(pipe); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (!m_producerAndTypeIdPipes[std::make_tuple(producer, typeId)].contains(pipe)) { | 
					
						
							|  |  |  |                 m_producerAndTypeIdPipes[std::make_tuple(producer, typeId)].push_back(pipe); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (!m_pipeMap.contains(std::make_tuple(producer, consumer, typeId))) { | 
					
						
							|  |  |  |                 m_pipeMap[std::make_tuple(producer, consumer, typeId)] = pipe; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-02-20 22:08:49 +01:00
										 |  |  |             return pipe; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QObject *element = m_objectPipeElementsStore->createElement(); | 
					
						
							| 
									
										
										
										
											2022-03-28 20:12:25 +02:00
										 |  |  |     qDebug("ObjectPipesRegistrations::registerProducerToConsumer: new pipe %p %p %s %s", | 
					
						
							| 
									
										
										
										
											2022-02-25 23:43:50 +01:00
										 |  |  |         producer, consumer, qPrintable(element->objectName()), qPrintable(type)); | 
					
						
							| 
									
										
										
										
											2022-02-20 22:08:49 +01:00
										 |  |  |     m_pipes.push_back(new ObjectPipe()); | 
					
						
							|  |  |  |     m_pipes.back()->m_pipeId = ++m_pipeId; | 
					
						
							|  |  |  |     m_pipes.back()->m_typeId = typeId; | 
					
						
							|  |  |  |     m_pipes.back()->m_producer = producer; | 
					
						
							|  |  |  |     m_pipes.back()->m_consumer = consumer; | 
					
						
							|  |  |  |     m_pipes.back()->m_element = element; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_producerPipes[producer].push_back(m_pipes.back()); | 
					
						
							|  |  |  |     m_consumerPipes[consumer].push_back(m_pipes.back()); | 
					
						
							|  |  |  |     m_typeIdPipes[typeId].push_back(m_pipes.back()); | 
					
						
							|  |  |  |     m_producerAndTypeIdPipes[std::make_tuple(producer, typeId)].push_back(m_pipes.back()); | 
					
						
							|  |  |  |     m_pipeMap[std::make_tuple(producer, consumer, typeId)] = m_pipes.back(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     connect(producer, SIGNAL(destroyed(QObject*)), this, SLOT(removeProducer(QObject*))); | 
					
						
							|  |  |  |     connect(consumer, SIGNAL(destroyed(QObject*)), this, SLOT(removeConsumer(QObject*))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return m_pipes.back(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ObjectPipe *ObjectPipesRegistrations::unregisterProducerToConsumer(const QObject *producer, const QObject *consumer, const QString& type) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ObjectPipe *pipe = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_typeIds.contains(type)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         int typeId = m_typeIds.value(type); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (m_pipeMap.contains(std::make_tuple(producer, consumer, typeId))) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             pipe = m_pipeMap[std::make_tuple(producer, consumer, typeId)]; | 
					
						
							|  |  |  |             m_producerPipes[producer].removeAll(pipe); | 
					
						
							|  |  |  |             m_consumerPipes[consumer].removeAll(pipe); | 
					
						
							|  |  |  |             m_typeIdPipes[typeId].removeAll(pipe); | 
					
						
							|  |  |  |             m_producerAndTypeIdPipes[std::make_tuple(producer, typeId)].removeAll(pipe); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (m_producerPipes[producer].size() == 0) { | 
					
						
							|  |  |  |                 m_producerPipes.remove(producer); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (m_consumerPipes[consumer].size() == 0) { | 
					
						
							|  |  |  |                 m_consumerPipes.remove(consumer); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (m_typeIdPipes[typeId].size() == 0) { | 
					
						
							|  |  |  |                 m_typeIdPipes.remove(typeId); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (m_producerAndTypeIdPipes[std::make_tuple(producer, typeId)].size() == 0) { | 
					
						
							|  |  |  |                 m_producerAndTypeIdPipes.remove(std::make_tuple(producer, typeId)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 02:18:51 +01:00
										 |  |  |             pipe->setToBeDeleted(PipeDeletionReason::PipeDeleted, pipe); | 
					
						
							| 
									
										
										
										
											2022-02-20 22:08:49 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-25 23:43:50 +01:00
										 |  |  |     if (pipe) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         qDebug("ObjectPipesRegistrations::unregisterProducerToConsumer: %p %p %s %s", | 
					
						
							|  |  |  |             producer, consumer, qPrintable(pipe->m_element->objectName()), qPrintable(type)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         qDebug("ObjectPipesRegistrations::unregisterProducerToConsumer: %p %p %s nullptr", | 
					
						
							|  |  |  |             producer, consumer, qPrintable(type)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-20 22:08:49 +01:00
										 |  |  |     return pipe; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ObjectPipesRegistrations::getPipes(const QObject *producer, const QString& type, QList<ObjectPipe*>& pipes) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QMutexLocker mlock(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_typeIds.contains(type)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_producerAndTypeIdPipes.contains(std::make_tuple(producer, m_typeIds[type]))) { | 
					
						
							|  |  |  |             pipes = m_producerAndTypeIdPipes[std::make_tuple(producer, m_typeIds[type])]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ObjectPipesRegistrations::processGC() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QMutexLocker mlock(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     typename QList<ObjectPipe*>::iterator itPipe = m_pipes.begin(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (itPipe != m_pipes.end()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ((*itPipe)->getGCCount() > 0) // scheduled for deletion
 | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if ((*itPipe)->decreaseGCCount() == 0) // delete on this pass
 | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 m_objectPipeElementsStore->deleteElement((*itPipe)->m_element); | 
					
						
							|  |  |  |                 delete *itPipe; | 
					
						
							|  |  |  |                 itPipe = m_pipes.erase(itPipe); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 ++itPipe; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-02-21 02:18:51 +01:00
										 |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ++itPipe; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-02-20 22:08:49 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ObjectPipesRegistrations::removeProducer(QObject *producer) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-02-26 13:44:46 +01:00
										 |  |  |     qDebug("ObjectPipesRegistrations::removeProducer: %p %s", producer, qPrintable(producer->objectName())); | 
					
						
							| 
									
										
										
										
											2022-02-20 22:08:49 +01:00
										 |  |  |     QMutexLocker mlock(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_producerPipes.contains(producer) && (m_producerPipes[producer].size() != 0)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         const QList<ObjectPipe*>& pipeList = m_producerPipes[producer]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (auto& pipe : pipeList) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             for (const auto& consumer : m_consumerPipes.keys()) { | 
					
						
							|  |  |  |                 m_consumerPipes[consumer].removeAll(pipe); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (const auto& typeId : m_typeIdPipes.keys()) { | 
					
						
							|  |  |  |                 m_typeIdPipes[typeId].removeAll(pipe); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 02:18:51 +01:00
										 |  |  |             pipe->setToBeDeleted(PipeDeletionReason::PipeProducerDeleted, producer); | 
					
						
							| 
									
										
										
										
											2022-02-20 22:08:49 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         m_producerPipes.remove(producer); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     typename QMap<std::tuple<const QObject*, const QObject*, int>, ObjectPipe*>::iterator itP = m_pipeMap.begin(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (itP != m_pipeMap.end()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (std::get<0>(itP.key())) { | 
					
						
							|  |  |  |             itP = m_pipeMap.erase(itP); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             ++itP; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     typename QMap<std::tuple<const QObject*, int>, QList<ObjectPipe*>>::iterator itPT = m_producerAndTypeIdPipes.begin(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (itPT != m_producerAndTypeIdPipes.end()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (std::get<0>(itPT.key()) == producer) { | 
					
						
							|  |  |  |             itPT = m_producerAndTypeIdPipes.erase(itPT); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             ++itPT; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ObjectPipesRegistrations::removeConsumer(QObject *consumer) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-02-26 13:44:46 +01:00
										 |  |  |     qDebug("ObjectPipesRegistrations::removeConsumer: %p %s", consumer, qPrintable(consumer->objectName())); | 
					
						
							| 
									
										
										
										
											2022-02-20 22:08:49 +01:00
										 |  |  |     QMutexLocker mlock(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_consumerPipes.contains(consumer) && (m_consumerPipes[consumer].size() != 0)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         QList<ObjectPipe*>& pipeList = m_consumerPipes[consumer]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (auto& pipe : pipeList) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             for (const auto& producer : m_producerPipes.keys()) { | 
					
						
							|  |  |  |                 m_producerPipes[producer].removeAll(pipe); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (const auto& typeId : m_typeIdPipes.keys()) { | 
					
						
							|  |  |  |                 m_typeIdPipes[typeId].removeAll(pipe); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (const auto& producerAndTypeId : m_producerAndTypeIdPipes.keys()) { | 
					
						
							|  |  |  |                 m_producerAndTypeIdPipes[producerAndTypeId].removeAll(pipe); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 02:18:51 +01:00
										 |  |  |             pipe->setToBeDeleted(PipeDeletionReason::PipeConsumerDeleted, consumer); | 
					
						
							| 
									
										
										
										
											2022-02-20 22:08:49 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         m_consumerPipes.remove(consumer); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     typename QMap<std::tuple<const QObject*, const QObject*, int>, ObjectPipe*>::iterator it = m_pipeMap.begin(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (it != m_pipeMap.end()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (std::get<1>(it.key()) == consumer) { | 
					
						
							|  |  |  |             it = m_pipeMap.erase(it); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             ++it; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |