From 6e83cbc6532b48fda16ecb2438c2e5dbf117a122 Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Wed, 26 Jun 2013 12:01:38 +0200 Subject: [PATCH] Make WeakPeerHash more generic --- src/libtomahawk/CMakeLists.txt | 3 +- src/libtomahawk/sip/PeerInfo.cpp | 11 ++- src/libtomahawk/sip/PeerInfo.h | 2 - src/libtomahawk/sip/PeerInfo_p.h | 4 + src/libtomahawk/sip/WeakPeerHash.cpp | 57 ------------ src/libtomahawk/sip/WeakPeerHash.h | 47 ---------- .../WeakObjectHash.cpp} | 41 +++++---- src/libtomahawk/utils/WeakObjectHash.h | 86 +++++++++++++++++++ 8 files changed, 122 insertions(+), 129 deletions(-) delete mode 100644 src/libtomahawk/sip/WeakPeerHash.cpp delete mode 100644 src/libtomahawk/sip/WeakPeerHash.h rename src/libtomahawk/{sip/WeakPeerHash_p.h => utils/WeakObjectHash.cpp} (61%) create mode 100644 src/libtomahawk/utils/WeakObjectHash.h diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index b8e39180e..a168b3483 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -345,7 +345,6 @@ list(APPEND libSources sip/SipInfo.cpp sip/PeerInfo.cpp sip/SipStatusMessage.cpp - sip/WeakPeerHash.cpp utils/TomahawkUtils.cpp utils/Logger.cpp @@ -353,6 +352,8 @@ list(APPEND libSources utils/XspfLoader.cpp utils/TomahawkCache.cpp utils/GuiHelpers.cpp + utils/WeakObjectHash.cpp + thirdparty/kdsingleapplicationguard/kdsingleapplicationguard.cpp thirdparty/kdsingleapplicationguard/kdsharedmemorylocker.cpp diff --git a/src/libtomahawk/sip/PeerInfo.cpp b/src/libtomahawk/sip/PeerInfo.cpp index 1e7c3a524..9003eb9af 100644 --- a/src/libtomahawk/sip/PeerInfo.cpp +++ b/src/libtomahawk/sip/PeerInfo.cpp @@ -28,7 +28,6 @@ #include "SipInfo.h" #include "SipPlugin.h" -#include "WeakPeerHash.h" #include #include @@ -36,7 +35,7 @@ namespace Tomahawk { -WeakPeerHash PeerInfo::s_peersByCacheKey = WeakPeerHash(); +Tomahawk::Utils::WeakObjectHash< PeerInfo > PeerInfoPrivate::s_peersByCacheKey = Tomahawk::Utils::WeakObjectHash< PeerInfo >(); QHash< SipPlugin*, peerinfo_ptr > PeerInfo::s_selfPeersBySipPlugin = QHash< SipPlugin*, peerinfo_ptr >(); @@ -83,9 +82,9 @@ Tomahawk::peerinfo_ptr PeerInfo::get( SipPlugin* parent, const QString& id, GetOptions options ) { const QString key = peerCacheKey( parent, id ); - if ( s_peersByCacheKey.hash().contains( key ) && !s_peersByCacheKey.hash().value( key ).isNull() ) + if ( PeerInfoPrivate::s_peersByCacheKey.hash().contains( key ) && !PeerInfoPrivate::s_peersByCacheKey.hash().value( key ).isNull() ) { - return s_peersByCacheKey.hash().value( key ).toStrongRef(); + return PeerInfoPrivate::s_peersByCacheKey.hash().value( key ).toStrongRef(); } // if AutoCreate isn't enabled nothing to do here @@ -96,7 +95,7 @@ PeerInfo::get( SipPlugin* parent, const QString& id, GetOptions options ) peerinfo_ptr peerInfo( new PeerInfo( parent, id ), &QObject::deleteLater ); peerInfo->setWeakRef( peerInfo.toWeakRef() ); - s_peersByCacheKey.insert( key, peerInfo ); + PeerInfoPrivate::s_peersByCacheKey.insert( key, peerInfo ); return peerInfo; } @@ -106,7 +105,7 @@ QList< Tomahawk::peerinfo_ptr > PeerInfo::getAll() { QList< Tomahawk::peerinfo_ptr > strongRefs; - foreach ( Tomahawk::peerinfo_wptr wptr, s_peersByCacheKey.hash().values() ) + foreach ( Tomahawk::peerinfo_wptr wptr, PeerInfoPrivate::s_peersByCacheKey.hash().values() ) { if ( !wptr.isNull() ) strongRefs << wptr.toStrongRef(); diff --git a/src/libtomahawk/sip/PeerInfo.h b/src/libtomahawk/sip/PeerInfo.h index 44be6847d..9aee964f3 100644 --- a/src/libtomahawk/sip/PeerInfo.h +++ b/src/libtomahawk/sip/PeerInfo.h @@ -30,7 +30,6 @@ class ControlConnection; class SipPlugin; class SipInfo; -class WeakPeerHash; namespace Tomahawk { @@ -130,7 +129,6 @@ private: Q_DECLARE_PRIVATE( Tomahawk::PeerInfo ) Tomahawk::PeerInfoPrivate* d_ptr; - static WeakPeerHash s_peersByCacheKey; static QHash< SipPlugin*, peerinfo_ptr > s_selfPeersBySipPlugin; mutable QPixmap* m_avatar; diff --git a/src/libtomahawk/sip/PeerInfo_p.h b/src/libtomahawk/sip/PeerInfo_p.h index 78187672e..7e8730e2a 100644 --- a/src/libtomahawk/sip/PeerInfo_p.h +++ b/src/libtomahawk/sip/PeerInfo_p.h @@ -22,6 +22,8 @@ #include "PeerInfo.h" +#include "utils/WeakObjectHash.h" + namespace Tomahawk { @@ -40,6 +42,8 @@ public: PeerInfo* q_ptr; Q_DECLARE_PUBLIC ( PeerInfo ) + static Tomahawk::Utils::WeakObjectHash s_peersByCacheKey; + private: QWeakPointer< Tomahawk::PeerInfo > ownRef; QPointer< ControlConnection > controlConnection; diff --git a/src/libtomahawk/sip/WeakPeerHash.cpp b/src/libtomahawk/sip/WeakPeerHash.cpp deleted file mode 100644 index 52a04e8a7..000000000 --- a/src/libtomahawk/sip/WeakPeerHash.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/* === 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 "WeakPeerHash_p.h" - -#include "PeerInfo.h" -#include "utils/Closure.h" - -#define WEAKPEERHASH_KEY "WeakPeerHashKey" - -WeakPeerHash::WeakPeerHash( QObject *parent ) - : QObject( parent ) - , d_ptr( new WeakPeerHashPrivate( this ) ) -{ -} - -WeakPeerHash::WeakPeerHash( const WeakPeerHash &hash ) - : QObject( hash.parent() ) - , d_ptr( new WeakPeerHashPrivate( this ) ) -{ - d_func()->hash = hash.d_func()->hash; -} - -void -WeakPeerHash::insert( const QString &key, const Tomahawk::peerinfo_ptr &value ) -{ - _detail::Closure* cl = NewClosure( value, SIGNAL( destroyed( QObject* ) ), this, SLOT( remove( QString ) ), key ); - cl->setAutoDelete( true ); - d_func()->hash.insert( key, value.toWeakRef() ); -} - -const QHash& -WeakPeerHash::hash() -{ - return d_func()->hash; -} - -void -WeakPeerHash::remove( const QString& key ) -{ - d_func()->hash.remove( key ); -} diff --git a/src/libtomahawk/sip/WeakPeerHash.h b/src/libtomahawk/sip/WeakPeerHash.h deleted file mode 100644 index b165d8eb3..000000000 --- a/src/libtomahawk/sip/WeakPeerHash.h +++ /dev/null @@ -1,47 +0,0 @@ -/* === 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 WEAKPEERHASH_H -#define WEAKPEERHASH_H - -#include "Typedefs.h" - -#include - -class WeakPeerHashPrivate; - -class WeakPeerHash : public QObject -{ - Q_OBJECT -public: - WeakPeerHash( QObject *parent = 0 ); - WeakPeerHash( const WeakPeerHash& hash ); - void insert( const QString& key, const Tomahawk::peerinfo_ptr& value ); - const QHash< QString, Tomahawk::peerinfo_wptr>& hash(); - -signals: - -private slots: - void remove( const QString& key ); -private: - Q_DECLARE_PRIVATE( WeakPeerHash ) - WeakPeerHashPrivate* d_ptr; - -}; - -#endif // WEAKPEERHASH_H diff --git a/src/libtomahawk/sip/WeakPeerHash_p.h b/src/libtomahawk/utils/WeakObjectHash.cpp similarity index 61% rename from src/libtomahawk/sip/WeakPeerHash_p.h rename to src/libtomahawk/utils/WeakObjectHash.cpp index ee4e80e07..7d3de67ad 100644 --- a/src/libtomahawk/sip/WeakPeerHash_p.h +++ b/src/libtomahawk/utils/WeakObjectHash.cpp @@ -16,24 +16,33 @@ * along with Tomahawk. If not, see . */ -#ifndef WEAKPEERHASH_P_H -#define WEAKPEERHASH_P_H +#include "WeakObjectHash.h" -#include "WeakPeerHash.h" -class WeakPeerHashPrivate +Tomahawk::Utils::WeakObjectHashPrivate::WeakObjectHashPrivate(Tomahawk::Utils::WeakObjectHashBase *parent) + : QObject( 0 ) + , m_parent( parent ) { -public: - WeakPeerHashPrivate( WeakPeerHash* q ) - : q_ptr ( q ) - { - } - WeakPeerHash* q_ptr; - Q_DECLARE_PUBLIC ( WeakPeerHash ) - -private: - QHash< QString, Tomahawk::peerinfo_wptr > hash; -}; +} + + +void +Tomahawk::Utils::WeakObjectHashPrivate::remove( const QString& key ) +{ + m_parent->remove( key ); +} + + +void +Tomahawk::Utils::WeakObjectHashBase::remove( const QString& key ) +{ + Q_UNUSED( key ); + // Does nothing but needs to be implemented for linking +} + + +Tomahawk::Utils::WeakObjectHashBase::~WeakObjectHashBase() +{ +} -#endif // WEAKPEERHASH_P_H diff --git a/src/libtomahawk/utils/WeakObjectHash.h b/src/libtomahawk/utils/WeakObjectHash.h new file mode 100644 index 000000000..0845bc133 --- /dev/null +++ b/src/libtomahawk/utils/WeakObjectHash.h @@ -0,0 +1,86 @@ +/* === 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 WEAKPEERHASH_H +#define WEAKPEERHASH_H + +#include "Typedefs.h" +#include "utils/Closure.h" + +#include + +namespace Tomahawk +{ + +namespace Utils +{ + +class WeakObjectHashBase +{ +public: + virtual void remove( const QString& key ); + virtual ~WeakObjectHashBase(); +protected: + WeakObjectHashBase() {} +}; + +class WeakObjectHashPrivate : public QObject +{ + Q_OBJECT +public: + WeakObjectHashPrivate( WeakObjectHashBase* parent ); + +public slots: + void remove( const QString& key ); + +private: + WeakObjectHashBase* m_parent; +}; + +template +class WeakObjectHash : public WeakObjectHashBase +{ +public: + WeakObjectHash() : m_private( this ) {} + + WeakObjectHash( const WeakObjectHash& hash ) + : m_hash( hash.m_hash ) + , m_private( this ) + { + } + + void insert( const QString& key, const QSharedPointer& value ) + { + _detail::Closure* cl = NewClosure( value.data(), SIGNAL( destroyed( QObject* ) ), &m_private, SLOT( remove( QString ) ), key ); + cl->setAutoDelete( true ); + m_hash.insert( key, value.toWeakRef() ); + } + + const QHash< QString, QWeakPointer >& hash() { return m_hash; } + virtual void remove( const QString& key ) { m_hash.remove( key ); } + +private: + QHash< QString, QWeakPointer > m_hash; + WeakObjectHashPrivate m_private; +}; + +} // namespace Utils + +} // namespace Tomahawk + +#endif // WEAKPEERHASH_H