From 0da448c5a74ee87fedea3dfbd53eae143833bfc2 Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Sun, 25 Aug 2013 11:59:05 +0200 Subject: [PATCH] Add WeakObjectList helper class --- src/libtomahawk/utils/WeakObjectList.cpp | 52 +++++++++++++ src/libtomahawk/utils/WeakObjectList.h | 95 ++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/libtomahawk/utils/WeakObjectList.cpp create mode 100644 src/libtomahawk/utils/WeakObjectList.h diff --git a/src/libtomahawk/utils/WeakObjectList.cpp b/src/libtomahawk/utils/WeakObjectList.cpp new file mode 100644 index 000000000..5c2bea2d7 --- /dev/null +++ b/src/libtomahawk/utils/WeakObjectList.cpp @@ -0,0 +1,52 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * Tomahawk 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 3 of the License, or + * (at your option) any later version. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#include "WeakObjectList.h" + +namespace Tomahawk { +namespace Utils { + + +void +WeakObjectListBase::remove( QObject* object ) +{ + Q_UNUSED( object ); + // Nothing to do here +} + + +WeakObjectListBase::~WeakObjectListBase() +{ +} + + +WeakObjectListPrivate::WeakObjectListPrivate( WeakObjectListBase* parent ) + : QObject( 0 ) + , m_parent( parent ) +{ +} + + +void +WeakObjectListPrivate::remove( QObject* object ) +{ + m_parent->remove( object ); +} + +} // namespace Utils +} // namespace Tomahawk diff --git a/src/libtomahawk/utils/WeakObjectList.h b/src/libtomahawk/utils/WeakObjectList.h new file mode 100644 index 000000000..4358cd92d --- /dev/null +++ b/src/libtomahawk/utils/WeakObjectList.h @@ -0,0 +1,95 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * Tomahawk 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 3 of the License, or + * (at your option) any later version. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#ifndef TOMAHAWK_UTILS_WEAKOBJECTLIST_H +#define TOMAHAWK_UTILS_WEAKOBJECTLIST_H + + +#include +#include +#include + + +namespace Tomahawk { +namespace Utils { + +class WeakObjectListBase +{ +public: + virtual void remove( QObject* object ); + virtual ~WeakObjectListBase(); +protected: + WeakObjectListBase() {} +}; + +class WeakObjectListPrivate : public QObject +{ + Q_OBJECT +public: + WeakObjectListPrivate( WeakObjectListBase* parent ); + +public slots: + void remove( QObject* object ); + +private: + WeakObjectListBase* m_parent; +}; + +template +class WeakObjectList : public WeakObjectListBase +{ + typedef QWeakPointer wptr; +public: + WeakObjectList() : m_private( this ) {} + + WeakObjectList( const WeakObjectList& list ) + : m_list( list.m_list ) + , m_private( this ) + { + } + + void insert( const QSharedPointer& value ) + { + m_private.connect( value.data(), SIGNAL( destroyed( QObject* ) ), &m_private, SLOT( remove( QObject* )) ); + m_list.append( value.toWeakRef() ); + } + + const QList& list() { return m_list; } + QMutableListIterator< wptr > iter() { return QMutableListIterator< wptr >( m_list ); } + virtual void remove( QObject* object ) + { + QMutableListIterator< wptr > iter( m_list ); + while ( iter.hasNext() ) + { + wptr ptr = iter.next(); + if ( ptr.data() == object || ptr.isNull() ) + { + iter.remove(); + } + } + } + +private: + QList< wptr > m_list; + WeakObjectListPrivate m_private; +}; + +} // namespace Utils +} // namespace Tomahawk + +#endif // TOMAHAWK_UTILS_WEAKOBJECTLIST_H