[vlmc-devel] Library: Use QSharedPointer to store Media

Hugo Beauzée-Luyssen git at videolan.org
Fri Aug 19 02:41:35 CEST 2016


vlmc | branch: medialibrary | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Thu Aug 18 23:53:52 2016 +0200| [de568fd491bae7dd67add1ea3b656ac3ebb150b9] | committer: Hugo Beauzée-Luyssen

Library: Use QSharedPointer to store Media

> https://code.videolan.org/videolan/vlmc/commit/de568fd491bae7dd67add1ea3b656ac3ebb150b9
---

 src/Gui/import/TagWidget.cpp            |  2 +-
 src/Gui/media/ClipMetadataDisplayer.cpp |  2 +-
 src/Gui/media/ClipMetadataDisplayer.h   |  2 +-
 src/Library/Library.cpp                 |  4 ++--
 src/Library/Library.h                   |  7 ++++---
 src/Media/Clip.cpp                      |  8 +++-----
 src/Media/Clip.h                        | 11 ++++++-----
 src/Media/Media.cpp                     |  8 +++++---
 src/Media/Media.h                       |  2 +-
 9 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/src/Gui/import/TagWidget.cpp b/src/Gui/import/TagWidget.cpp
index 0b9b83c..bebec2a 100644
--- a/src/Gui/import/TagWidget.cpp
+++ b/src/Gui/import/TagWidget.cpp
@@ -72,7 +72,7 @@ TagWidget::clipSelected( Clip* clip )
         else
             static_cast<QPushButton*>(m_buttonList[i])->setChecked( false );
     }
-    connect( clip->media(), SIGNAL( metaDataComputed( const Media* ) ),
+    connect( clip->media().data(), SIGNAL( metaDataComputed( const Media* ) ),
              this, SLOT( setMetaTags() ) );
     connect( clip, SIGNAL( destroyed() ), this, SLOT( clipDestroyed() ) );
 }
diff --git a/src/Gui/media/ClipMetadataDisplayer.cpp b/src/Gui/media/ClipMetadataDisplayer.cpp
index 7fbf778..ed84035 100644
--- a/src/Gui/media/ClipMetadataDisplayer.cpp
+++ b/src/Gui/media/ClipMetadataDisplayer.cpp
@@ -96,7 +96,7 @@ void
 ClipMetadataDisplayer::setWatchedClip( const Clip *clip )
 {
     if ( m_watchedMedia )
-        disconnect( m_watchedMedia );
+        disconnect( m_watchedMedia.data() );
     if ( m_watchedClip )
         disconnect( m_watchedClip );
 
diff --git a/src/Gui/media/ClipMetadataDisplayer.h b/src/Gui/media/ClipMetadataDisplayer.h
index 745a7cc..5b5ae77 100644
--- a/src/Gui/media/ClipMetadataDisplayer.h
+++ b/src/Gui/media/ClipMetadataDisplayer.h
@@ -51,7 +51,7 @@ class ClipMetadataDisplayer : public QWidget
     private:
         Ui::ClipMetadataDisplayer       *m_ui;
         const Clip                      *m_watchedClip;
-        const Media                     *m_watchedMedia;
+        QSharedPointer<const Media>     m_watchedMedia;
 
     private slots:
         void                            metadataUpdated();
diff --git a/src/Library/Library.cpp b/src/Library/Library.cpp
index 418b93d..f2e885e 100644
--- a/src/Library/Library.cpp
+++ b/src/Library/Library.cpp
@@ -91,7 +91,7 @@ Library::~Library()
 }
 
 void
-Library::addMedia( Media* media )
+Library::addMedia( QSharedPointer<Media> media )
 {
     setCleanState( false );
     if ( m_media.contains( media->id() ) )
@@ -122,7 +122,7 @@ Library::isInCleanState() const
     return m_cleanState;
 }
 
-Media*
+QSharedPointer<Media>
 Library::media( qint64 mediaId )
 {
     return m_media.value( mediaId );
diff --git a/src/Library/Library.h b/src/Library/Library.h
index 4e90925..ffaa0e0 100644
--- a/src/Library/Library.h
+++ b/src/Library/Library.h
@@ -32,6 +32,7 @@
 
 #include <QObject>
 #include <QHash>
+#include <QSharedPointer>
 
 class Clip;
 class Media;
@@ -50,10 +51,10 @@ class Library : public QObject
 public:
     Library( Settings* projectSettings );
     virtual ~Library();
-    virtual void    addMedia( Media* media );
+    virtual void    addMedia( QSharedPointer<Media> media );
     virtual bool    addClip( Clip *clip );
     bool            isInCleanState() const;
-    Media*          media( qint64 mediaId );
+    QSharedPointer<Media> media( qint64 mediaId );
     /**
      * @brief clip returns an existing clip
      * @param uuid the clip's UUID
@@ -71,7 +72,7 @@ private:
     bool        m_cleanState;
 
     Settings*   m_settings;
-    QHash<qint64, Media*>  m_media;
+    QHash<qint64, QSharedPointer<Media>>  m_media;
     /**
      * @brief m_clips   contains all the clips loaded in the library, without any
      *                  subclip hierarchy
diff --git a/src/Media/Clip.cpp b/src/Media/Clip.cpp
index 796ba22..e3b0e25 100644
--- a/src/Media/Clip.cpp
+++ b/src/Media/Clip.cpp
@@ -39,7 +39,7 @@
 #include "Tools/VlmcDebug.h"
 #include <QVariant>
 
-Clip::Clip( Media *media, qint64 begin /*= 0*/, qint64 end /*= Backend::IInput::EndOfMedia */, const QString& uuid /*= QString()*/ ) :
+Clip::Clip( QSharedPointer<Media> media, qint64 begin /*= 0*/, qint64 end /*= Backend::IInput::EndOfMedia */, const QString& uuid /*= QString()*/ ) :
         Workflow::Helper( uuid ),
         m_media( media ),
         m_input( std::move( m_media->input()->cut( begin, end ) ) ),
@@ -78,17 +78,15 @@ Clip::Clip( Clip *parent, qint64 begin /*= -1*/, qint64 end /*= -2*/,
 Clip::~Clip()
 {
     emit unloaded( this );
-    if ( isRootClip() == true )
-        delete m_media;
 }
 
-Media*
+QSharedPointer<Media>
 Clip::media()
 {
     return m_media;
 }
 
-const Media*
+QSharedPointer<const Media>
 Clip::media() const
 {
     return m_media;
diff --git a/src/Media/Clip.h b/src/Media/Clip.h
index 9029b6f..c91deca 100644
--- a/src/Media/Clip.h
+++ b/src/Media/Clip.h
@@ -31,6 +31,7 @@
 
 #include "Workflow/Helper.h"
 #include <QHash>
+#include <QSharedPointer>
 #include <QStringList>
 #include <QUuid>
 #include <QXmlStreamWriter>
@@ -63,7 +64,7 @@ class   Clip : public Workflow::Helper
          *                  the end of the parent will be used.
          *  \param  uuid    A unique identifier. If not given, one will be generated.
          */
-        Clip( Media *parent, qint64 begin = 0, qint64 end = Backend::IInput::EndOfMedia, const QString &uuid = QStringLiteral() );
+        Clip( QSharedPointer<Media> parent, qint64 begin = 0, qint64 end = Backend::IInput::EndOfMedia, const QString &uuid = QStringLiteral() );
         /**
          *  \brief  Clones a Clip, potentially with a new begin and end.
          *
@@ -84,8 +85,8 @@ class   Clip : public Workflow::Helper
         /**
             \return         Returns the Media that the clip was basep uppon.
         */
-        Media*              media();
-        const Media*        media() const;
+        QSharedPointer<Media>       media();
+        QSharedPointer<const Media> media() const;
 
         Clip                *parent();
         const Clip          *parent() const;
@@ -151,8 +152,8 @@ class   Clip : public Workflow::Helper
         void                loadVariant(const QVariantMap& v );
 
     private:
-        Media*              m_media;
-        std::unique_ptr<Backend::IInput> m_input;
+        QSharedPointer<Media>               m_media;
+        std::unique_ptr<Backend::IInput>    m_input;
 
         QStringList         m_metaTags;
         QString             m_notes;
diff --git a/src/Media/Media.cpp b/src/Media/Media.cpp
index 45d8df2..2535964 100644
--- a/src/Media/Media.cpp
+++ b/src/Media/Media.cpp
@@ -140,14 +140,16 @@ Media::input() const
     return m_input.get();
 }
 
-Media* Media::fromVariant( const QVariant& v )
+QSharedPointer<Media>
+Media::fromVariant( const QVariant& v )
 {
     bool ok = false;
     auto mediaId = v.toLongLong( &ok );
     if ( ok == false )
-        return nullptr;
+        return QSharedPointer<Media>{};
     auto mlMedia = Core::instance()->mediaLibrary()->media( mediaId );
-    return new Media( mlMedia );
+    //FIXME: Is QSharedPointer exception safe in case its constructor throws an exception?
+    return QSharedPointer<Media>( new Media( mlMedia ) );
 }
 
 #ifdef HAVE_GUI
diff --git a/src/Media/Media.h b/src/Media/Media.h
index 7fd50d8..9ce224e 100644
--- a/src/Media/Media.h
+++ b/src/Media/Media.h
@@ -97,7 +97,7 @@ public:
     Backend::IInput*         input();
     const Backend::IInput*   input() const;
 
-    static Media* fromVariant( const QVariant& v );
+    static QSharedPointer<Media> fromVariant( const QVariant& v );
 
 #ifdef HAVE_GUI
     // This has to be called from the GUI thread.



More information about the Vlmc-devel mailing list