[vlmc-devel] Backend: Use exception to handle invalid services

Yikai Lu git at videolan.org
Sun Jun 26 06:01:50 CEST 2016


vlmc | branch: master | Yikai Lu <luyikei.qmltu at gmail.com> | Sat Jun 25 20:12:52 2016 +0900| [63a2ee3bc1c9db4d453647d7eaa5d07938750a56] | committer: Yikai Lu

Backend: Use exception to handle invalid services

> https://code.videolan.org/videolan/vlmc/commit/63a2ee3bc1c9db4d453647d7eaa5d07938750a56
---

 src/Backend/IService.h                            | 11 +++++++++++
 src/Backend/MLT/MLTConsumer.cpp                   |  2 ++
 src/Backend/MLT/MLTFilter.cpp                     |  2 ++
 src/Backend/MLT/MLTProducer.cpp                   |  4 ++++
 src/Backend/MLT/MLTTransition.cpp                 |  2 ++
 src/EffectsEngine/EffectHelper.cpp                | 20 +++++++++++++++++---
 src/EffectsEngine/EffectHelper.h                  |  2 ++
 src/Gui/effectsengine/EffectInstanceListModel.cpp |  9 ++++++---
 src/Gui/timeline/GraphicsEffectItem.cpp           |  2 +-
 src/Gui/timeline/TracksView.cpp                   | 13 +++++++++++--
 10 files changed, 58 insertions(+), 9 deletions(-)

diff --git a/src/Backend/IService.h b/src/Backend/IService.h
index 45190e9..e0133de 100644
--- a/src/Backend/IService.h
+++ b/src/Backend/IService.h
@@ -25,11 +25,22 @@
 #define ISERVICE_H
 
 #include <string>
+#include <exception>
 
 namespace Backend
 {
     class IProfile;
     class IFilter;
+
+    class InvalidServiceException : public std::exception
+    {
+    public:
+        virtual const char* what() const throw() override
+        {
+            return "We couldn't create a service.";
+        }
+    };
+
     class IService
     {
     public:
diff --git a/src/Backend/MLT/MLTConsumer.cpp b/src/Backend/MLT/MLTConsumer.cpp
index e141fff..956091b 100644
--- a/src/Backend/MLT/MLTConsumer.cpp
+++ b/src/Backend/MLT/MLTConsumer.cpp
@@ -46,6 +46,8 @@ MLTConsumer::MLTConsumer( Backend::IProfile& profile, const char *id, Backend::I
     MLTProfile& mltProfile = static_cast<MLTProfile&>( profile );
     m_consumer = new Mlt::Consumer( *mltProfile.m_profile, id );
     m_service = m_consumer;
+    if ( isValid() == false )
+        throw InvalidServiceException();
 }
 
 MLTConsumer::~MLTConsumer()
diff --git a/src/Backend/MLT/MLTFilter.cpp b/src/Backend/MLT/MLTFilter.cpp
index a527426..5c154ef 100644
--- a/src/Backend/MLT/MLTFilter.cpp
+++ b/src/Backend/MLT/MLTFilter.cpp
@@ -157,6 +157,8 @@ MLTFilter::MLTFilter( Backend::IProfile& profile, const char* id )
     MLTProfile& mltProfile = static_cast<MLTProfile&>( profile );
     m_filter = new Mlt::Filter( *mltProfile.m_profile, id );
     m_service = m_filter;
+    if ( isValid() == false )
+        throw InvalidServiceException();
 }
 
 MLTFilter::MLTFilter( const char *id )
diff --git a/src/Backend/MLT/MLTProducer.cpp b/src/Backend/MLT/MLTProducer.cpp
index 399ea5b..5466b11 100644
--- a/src/Backend/MLT/MLTProducer.cpp
+++ b/src/Backend/MLT/MLTProducer.cpp
@@ -67,6 +67,8 @@ MLTProducer::MLTProducer( Mlt::Producer* producer, IProducerEventCb* callback )
     m_service  = producer;
     setCallback( callback );
     calcTracks();
+    if ( isValid() == false )
+        throw InvalidServiceException();
 }
 
 MLTProducer::MLTProducer( IProfile& profile, const char* path, IProducerEventCb* callback )
@@ -78,6 +80,8 @@ MLTProducer::MLTProducer( IProfile& profile, const char* path, IProducerEventCb*
     m_service  = m_producer;
     setCallback( callback );
     calcTracks();
+    if ( isValid() == false )
+        throw InvalidServiceException();
 }
 
 MLTProducer::MLTProducer( const char* path, IProducerEventCb* callback )
diff --git a/src/Backend/MLT/MLTTransition.cpp b/src/Backend/MLT/MLTTransition.cpp
index b440f9f..1f458c0 100644
--- a/src/Backend/MLT/MLTTransition.cpp
+++ b/src/Backend/MLT/MLTTransition.cpp
@@ -32,6 +32,8 @@ MLTTransition::MLTTransition( Backend::IProfile& profile, const char *id )
     MLTProfile& mltProfile = static_cast<MLTProfile&>( profile );
     m_transition = new Mlt::Transition( *mltProfile.m_profile, id );
     m_service = m_transition;
+    if ( isValid() == false )
+        throw InvalidServiceException();
 }
 
 MLTTransition::~MLTTransition()
diff --git a/src/EffectsEngine/EffectHelper.cpp b/src/EffectsEngine/EffectHelper.cpp
index b6e1d08..cbc1aec 100644
--- a/src/EffectsEngine/EffectHelper.cpp
+++ b/src/EffectsEngine/EffectHelper.cpp
@@ -26,6 +26,7 @@
 #include "Project/Project.h"
 #include "Workflow/MainWorkflow.h"
 #include "Backend/MLT/MLTFilter.h"
+#include "Backend/IService.h"
 #include "Backend/IBackend.h"
 #include <mlt++/MltProperties.h>
 
@@ -46,12 +47,19 @@ conv( std::string str, SettingValue::Type type )
 EffectHelper::EffectHelper( const char* id, qint64 begin, qint64 end,
                             const QString &uuid ) :
     Helper( uuid ),
-    m_filter( new Backend::MLT::MLTFilter( id ) ),
     m_service( nullptr ),
     m_filterInfo( nullptr )
 {
-    if ( m_filter->isValid() == false )
-        return;
+    try
+    {
+        m_filter = new Backend::MLT::MLTFilter( id );
+    }
+    catch ( Backend::InvalidServiceException& e )
+    {
+        m_filter = nullptr;
+        throw e;
+    }
+
     m_filter->setBoundaries( begin, end );
     initParams();
 }
@@ -253,6 +261,12 @@ EffectHelper::setBoundaries( qint64 begin, qint64 end )
     m_filter->setBoundaries( begin, end );
 }
 
+bool
+EffectHelper::isValid() const
+{
+    return m_filter != nullptr && m_filter->isValid();
+}
+
 void
 EffectHelper::setTarget( Backend::IService* service )
 {
diff --git a/src/EffectsEngine/EffectHelper.h b/src/EffectsEngine/EffectHelper.h
index 5f3c0bb..311ea07 100644
--- a/src/EffectsEngine/EffectHelper.h
+++ b/src/EffectsEngine/EffectHelper.h
@@ -64,6 +64,8 @@ class   EffectHelper : public Workflow::Helper
         virtual qint64  length() const override;
         virtual void    setBoundaries( qint64 begin, qint64 end ) override;
 
+        bool    isValid() const;
+
         void                setTarget( Backend::IService* service );
         Backend::IService*  target();
 
diff --git a/src/Gui/effectsengine/EffectInstanceListModel.cpp b/src/Gui/effectsengine/EffectInstanceListModel.cpp
index 439736d..d0f711e 100644
--- a/src/Gui/effectsengine/EffectInstanceListModel.cpp
+++ b/src/Gui/effectsengine/EffectInstanceListModel.cpp
@@ -106,10 +106,13 @@ EffectInstanceListModel::add( const QString &effectName )
     if ( effectName.isEmpty() == true )
         return nullptr;
     beginInsertRows( QModelIndex(), m_service->filterCount(), m_service->filterCount() );
-    auto helper = new EffectHelper( effectName );
-    if ( helper->filter()->isValid() == false )
+    EffectHelper* helper = nullptr;
+    try
+    {
+        helper = new EffectHelper( effectName  );
+    }
+    catch( Backend::InvalidServiceException& e )
     {
-        delete helper;
         return nullptr;
     }
     m_service->attach( *helper->filter() );
diff --git a/src/Gui/timeline/GraphicsEffectItem.cpp b/src/Gui/timeline/GraphicsEffectItem.cpp
index b73eef8..9952df9 100644
--- a/src/Gui/timeline/GraphicsEffectItem.cpp
+++ b/src/Gui/timeline/GraphicsEffectItem.cpp
@@ -45,7 +45,7 @@ GraphicsEffectItem::GraphicsEffectItem( EffectHelper *helper ) :
         m_effectHelper( helper ),
         m_container( nullptr )
 {
-    if ( m_effectHelper->filter()->isValid() == false )
+    if ( m_effectHelper->isValid() == false )
         return;
     setWidth( m_effectHelper->length() );
     m_effect = helper->filterInfo();
diff --git a/src/Gui/timeline/TracksView.cpp b/src/Gui/timeline/TracksView.cpp
index 2a3ecba..d8b6121 100644
--- a/src/Gui/timeline/TracksView.cpp
+++ b/src/Gui/timeline/TracksView.cpp
@@ -357,8 +357,17 @@ TracksView::dragEnterEvent( QDragEnterEvent *event )
 void
 TracksView::effectDragEnterEvent( QDragEnterEvent *event )
 {
-    m_dragEffectItem = new GraphicsEffectItem( new EffectHelper( qPrintable( event->mimeData()->data( "vlmc/effect_name") ) ) );
-    if ( m_dragEffectItem->effectHelper()->filter()->isValid() == true )
+    try
+    {
+        m_dragEffectItem = new GraphicsEffectItem( new EffectHelper( qPrintable( event->mimeData()->data( "vlmc/effect_name") ) ) );
+    }
+    catch( Backend::InvalidServiceException& e )
+    {
+        delete m_dragEffectItem;
+        m_dragEffectItem = nullptr;
+    }
+
+    if ( m_dragEffectItem != nullptr )
     {
         m_dragEffectItem->setHeight( m_dragEffectItem->itemHeight() );
         m_dragEffectItem->m_tracksView = this;



More information about the Vlmc-devel mailing list