1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-02-27 05:12:34 +01:00

Make WeakPeerHash more generic

This commit is contained in:
Uwe L. Korn 2013-06-26 12:01:38 +02:00
parent 2364a31515
commit 6e83cbc653
8 changed files with 122 additions and 129 deletions

View File

@ -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

View File

@ -28,7 +28,6 @@
#include "SipInfo.h"
#include "SipPlugin.h"
#include "WeakPeerHash.h"
#include <QCryptographicHash>
#include <QBuffer>
@ -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();

View File

@ -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;

View File

@ -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<PeerInfo> s_peersByCacheKey;
private:
QWeakPointer< Tomahawk::PeerInfo > ownRef;
QPointer< ControlConnection > controlConnection;

View File

@ -1,57 +0,0 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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<QString, Tomahawk::peerinfo_wptr>&
WeakPeerHash::hash()
{
return d_func()->hash;
}
void
WeakPeerHash::remove( const QString& key )
{
d_func()->hash.remove( key );
}

View File

@ -1,47 +0,0 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef WEAKPEERHASH_H
#define WEAKPEERHASH_H
#include "Typedefs.h"
#include <QObject>
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

View File

@ -16,24 +16,33 @@
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#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

View File

@ -0,0 +1,86 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef WEAKPEERHASH_H
#define WEAKPEERHASH_H
#include "Typedefs.h"
#include "utils/Closure.h"
#include <QObject>
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 T>
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<T>& 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<T> >& hash() { return m_hash; }
virtual void remove( const QString& key ) { m_hash.remove( key ); }
private:
QHash< QString, QWeakPointer<T> > m_hash;
WeakObjectHashPrivate m_private;
};
} // namespace Utils
} // namespace Tomahawk
#endif // WEAKPEERHASH_H