[vlc-commits] [Git][videolan/vlc][master] 5 commits: qt: use variadic template in Singleton to allow generic types

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Sat Apr 2 10:09:07 UTC 2022



Jean-Baptiste Kempf pushed to branch master at VideoLAN / VLC


Commits:
260eb142 by Fatih Uzunoglu at 2022-04-02T09:35:29+00:00
qt: use variadic template in Singleton to allow generic types

- - - - -
5b97f4cc by Fatih Uzunoglu at 2022-04-02T09:35:29+00:00
qt: use Singleton in DialogsProvider

- - - - -
824a3f81 by Fatih Uzunoglu at 2022-04-02T09:35:29+00:00
qt: use Singleton in ExtensionsDialogProvider

- - - - -
960a101e by Fatih Uzunoglu at 2022-04-02T09:35:29+00:00
qt: use Singleton in ExtensionsManager

- - - - -
63033c7c by Fatih Uzunoglu at 2022-04-02T09:35:29+00:00
qt: use singleton in OpenDialog

- - - - -


9 changed files:

- modules/gui/qt/dialogs/dialogs_provider.cpp
- modules/gui/qt/dialogs/dialogs_provider.hpp
- modules/gui/qt/dialogs/extensions/extensions.cpp
- modules/gui/qt/dialogs/extensions/extensions.hpp
- modules/gui/qt/dialogs/extensions/extensions_manager.cpp
- modules/gui/qt/dialogs/extensions/extensions_manager.hpp
- modules/gui/qt/dialogs/open/open.cpp
- modules/gui/qt/dialogs/open/open.hpp
- modules/gui/qt/util/singleton.hpp


Changes:

=====================================
modules/gui/qt/dialogs/dialogs_provider.cpp
=====================================
@@ -66,8 +66,6 @@
 #define I_OP_DIR_WINTITLE I_DIR_OR_FOLDER( N_("Open Directory"), \
                                            N_("Open Folder") )
 
-DialogsProvider* DialogsProvider::instance = NULL;
-
 DialogsProvider::DialogsProvider( qt_intf_t *_p_intf )
     : QObject( NULL ), p_intf( _p_intf )
 {


=====================================
modules/gui/qt/dialogs/dialogs_provider.hpp
=====================================
@@ -36,6 +36,8 @@
 
 #include "playlist/playlist_item.hpp"
 
+#include "util/singleton.hpp"
+
 #include <QObject>
 #include <QStringList>
 
@@ -64,29 +66,23 @@ class QEvent;
 class QSignalMapper;
 class VLCMenuBar;
 
-class DialogsProvider : public QObject
+class DialogsProvider : public QObject, public Singleton<DialogsProvider>
 {
     Q_OBJECT
     friend class VLCMenuBar;
+    friend class Singleton<DialogsProvider>;
 
 public:
     static DialogsProvider *getInstance()
     {
+        const auto instance = Singleton<DialogsProvider>::getInstance<false>();
         assert( instance );
         return instance;
     }
     static DialogsProvider *getInstance( qt_intf_t *p_intf )
     {
-        if( !instance )
-            instance = new DialogsProvider( p_intf );
-        return instance;
+        return Singleton<DialogsProvider>::getInstance( p_intf );
     }
-    static void killInstance()
-    {
-        delete instance;
-        instance = NULL;
-    }
-
     QStringList showSimpleOpen( const QString& help = QString(),
                                 int filters = EXT_FILTER_MEDIA |
                                 EXT_FILTER_VIDEO | EXT_FILTER_AUDIO |
@@ -110,9 +106,8 @@ protected:
     void customEvent( QEvent *);
 
 private:
-    DialogsProvider( qt_intf_t *);
+    DialogsProvider( qt_intf_t * );
     virtual ~DialogsProvider();
-    static DialogsProvider *instance;
 
     void loadMediaFile( es_format_category_e category, int filter, const QString& dialogTitle );
 


=====================================
modules/gui/qt/dialogs/extensions/extensions.cpp
=====================================
@@ -39,8 +39,6 @@
 #include <QKeyEvent>
 #include "widgets/native/customwidgets.hpp"
 
-ExtensionsDialogProvider *ExtensionsDialogProvider::instance = NULL;
-
 static void DialogCallback( extension_dialog_t *p_ext_dialog,
                             void *p_data );
 
@@ -49,6 +47,9 @@ ExtensionsDialogProvider::ExtensionsDialogProvider( qt_intf_t *_p_intf,
                                                     extensions_manager_t *p_mgr )
         : QObject( NULL ), p_intf( _p_intf ), p_extensions_manager( p_mgr )
 {
+    assert(p_intf);
+    assert(p_extensions_manager);
+
     vlc_dialog_provider_set_ext_callback( p_intf, DialogCallback, NULL );
 
     connect( this, &ExtensionsDialogProvider::SignalDialog,


=====================================
modules/gui/qt/dialogs/extensions/extensions.hpp
=====================================
@@ -25,6 +25,7 @@
 
 #include "qt.hpp"
 #include <vlc_extensions.h>
+#include "util/singleton.hpp"
 
 #include "assert.h"
 
@@ -38,17 +39,16 @@ class QKeyEvent;
 class ExtensionsDialogProvider;
 class ExtensionDialog;
 
-class ExtensionsDialogProvider : public QObject
+class ExtensionsDialogProvider : public QObject, public Singleton<ExtensionsDialogProvider>
 {
     /** This is the dialog provider for Extensions dialogs
-     * @todo Make this class be a public Singleton<EDP>
      * @todo Add a setExtManager() function (with vlc_object_hold)
      **/
+    friend class Singleton<ExtensionsDialogProvider>;
 
     Q_OBJECT
 
 private:
-    static ExtensionsDialogProvider *instance;
     qt_intf_t *p_intf;
     extensions_manager_t *p_extensions_manager;
 
@@ -58,30 +58,15 @@ private slots:
     ExtensionDialog* UpdateExtDialog( extension_dialog_t *p_dialog );
 
 public:
-    ExtensionsDialogProvider( qt_intf_t *p_intf,
-                              extensions_manager_t *p_mgr );
-    virtual ~ExtensionsDialogProvider();
-
-    static ExtensionsDialogProvider* getInstance( qt_intf_t *p_intf = NULL,
-                                                  extensions_manager_t *p_mgr = NULL )
-    {
-        if( !instance )
-        {
-            assert( p_intf != NULL && p_mgr != NULL );
-            instance = new ExtensionsDialogProvider( p_intf, p_mgr );
-        }
-        return instance;
-    }
-    static void killInstance()
-    {
-        delete instance;
-        instance = NULL;
-    }
-
     void ManageDialog( extension_dialog_t *p_dialog );
 
 signals:
     void SignalDialog( extension_dialog_t *p_dialog );
+
+private:
+    ExtensionsDialogProvider( qt_intf_t *p_intf = nullptr,
+                             extensions_manager_t *p_mgr = nullptr );
+    virtual ~ExtensionsDialogProvider();
 };
 
 


=====================================
modules/gui/qt/dialogs/extensions/extensions_manager.cpp
=====================================
@@ -36,15 +36,10 @@
 #define MENU_GET_ACTION(a) ( (uint16_t)( ((uint32_t)a) >> 16 ) )
 #define MENU_GET_EXTENSION(a) ( (uint16_t)( ((uint32_t)a) & 0xFFFF ) )
 
-ExtensionsManager* ExtensionsManager::instance = NULL;
-
 ExtensionsManager::ExtensionsManager( qt_intf_t *_p_intf, QObject *parent )
         : QObject( parent ), p_intf( _p_intf ), p_extensions_manager( NULL )
         , p_edp( NULL )
 {
-    assert( ExtensionsManager::instance == NULL );
-    instance = this;
-
     menuMapper = new QSignalMapper( this );
     connect( menuMapper, QSIGNALMAPPER_MAPPEDINT_SIGNAL, this, &ExtensionsManager::triggerMenu );
     connect( THEMIM, &PlayerController::playingStateChanged, this, &ExtensionsManager::playingChanged );


=====================================
modules/gui/qt/dialogs/extensions/extensions_manager.hpp
=====================================
@@ -31,6 +31,7 @@
 
 #include "qt.hpp"
 #include <player/player_controller.hpp>
+#include "util/singleton.hpp"
 
 #include <QObject>
 
@@ -39,26 +40,12 @@ class QSignalMapper;
 
 class ExtensionsDialogProvider;
 
-class ExtensionsManager : public QObject
+class ExtensionsManager : public QObject, public Singleton<ExtensionsManager>
 {
+    friend class Singleton<ExtensionsManager>;
+
     Q_OBJECT
 public:
-    static ExtensionsManager *getInstance( qt_intf_t *_p_intf,
-                                           QObject *_parent = 0 )
-    {
-        if( !instance )
-            instance = new ExtensionsManager( _p_intf, _parent );
-        return instance;
-    }
-    static void killInstance()
-    {
-        delete instance;
-        instance = NULL;
-    }
-
-    ExtensionsManager( qt_intf_t *p_intf, QObject *parent );
-    virtual ~ExtensionsManager();
-
     inline bool isLoaded() { return p_extensions_manager != NULL; }
     inline bool cannotLoad() { return b_unloading || b_failed; }
     inline bool isUnloading() { return b_unloading; }
@@ -72,6 +59,10 @@ public:
         return p_extensions_manager;
     }
 
+private:
+    ExtensionsManager( qt_intf_t *p_intf, QObject *parent = nullptr );
+    virtual ~ExtensionsManager();
+
 public slots:
     bool loadExtensions();
     void unloadExtensions();


=====================================
modules/gui/qt/dialogs/open/open.cpp
=====================================
@@ -36,16 +36,15 @@
 # define DEBUG_QT 1
 #endif
 
-OpenDialog *OpenDialog::instance = NULL;
-
 OpenDialog* OpenDialog::getInstance(  qt_intf_t *p_intf,
         bool b_rawInstance, int _action_flag, bool b_selectMode )
 {
-    /* Creation */
-    if( !instance )
-        instance = new OpenDialog( nullptr, p_intf, b_selectMode,
-                                   _action_flag );
-    else if( !b_rawInstance )
+    const auto instance = Singleton<OpenDialog>::getInstance(nullptr,
+                                                             p_intf,
+                                                             b_selectMode,
+                                                             _action_flag);
+
+    if( !b_rawInstance )
     {
         /* Request the instance but change small details:
            - Button menu */


=====================================
modules/gui/qt/dialogs/open/open.hpp
=====================================
@@ -32,6 +32,7 @@
 
 #include "widgets/native/qvlcframe.hpp"
 #include "dialogs/open/open_panels.hpp"
+#include "util/singleton.hpp"
 
 /* Auto-generated from .ui files */
 #include "ui_open.h"
@@ -55,20 +56,16 @@ enum {
 
 class QString;
 
-class OpenDialog : public QVLCDialog
+class OpenDialog : public QVLCDialog, public Singleton<OpenDialog>
 {
+    friend Singleton<OpenDialog>;
+
     Q_OBJECT
 public:
     static OpenDialog * getInstance(qt_intf_t *p_intf,
                                 bool b_rawInstance = false, int _action_flag = 0,
                                 bool b_selectMode = false );
 
-    static void killInstance()
-    {
-        delete instance;
-        instance = NULL;
-    }
-
     void showTab( int = OPEN_FILE_TAB );
     QString getMRL( bool b = true );
 
@@ -87,8 +84,6 @@ private:
                 int _action_flag = 0 );
     virtual ~OpenDialog();
 
-    static OpenDialog *instance;
-
     QString optionsMRL;
     QString storedMethod;
     QStringList itemsMRL;


=====================================
modules/gui/qt/util/singleton.hpp
=====================================
@@ -29,27 +29,34 @@
 
 #include "qt.hpp"
 
+
 template <typename T>
-class       Singleton
+class Singleton
 {
 public:
-    static T*      getInstance( qt_intf_t *p_intf = NULL )
+    template <bool create, class = typename std::enable_if<!create>::type>
+    static T* getInstance( void )
     {
         vlc::threads::mutex_locker lock( m_mutex );
-        if ( m_instance == NULL )
-            m_instance = new T( p_intf );
         return m_instance;
     }
 
-    static void    killInstance()
+    template <class T2 = T, typename... Args>
+    static T* getInstance( Args&&... args )
     {
         vlc::threads::mutex_locker lock( m_mutex );
-        if ( m_instance != NULL )
-        {
-            delete m_instance;
-            m_instance = NULL;
-        }
+        if ( !m_instance )
+          m_instance = new T2( std::forward<Args>( args )... );
+        return m_instance;
     }
+
+    static void killInstance()
+    {
+        vlc::threads::mutex_locker lock( m_mutex );
+        delete m_instance;
+        m_instance = nullptr;
+    }
+
 protected:
     Singleton(){}
     virtual ~Singleton(){}
@@ -59,13 +66,11 @@ protected:
     Singleton<T>&   operator=(const Singleton<T>&);
 
 private:
-    static T*      m_instance;
+    static T* m_instance;
     static vlc::threads::mutex m_mutex;
 };
-
 template <typename T>
-T*  Singleton<T>::m_instance = NULL;
-
+T* Singleton<T>::m_instance = nullptr;
 template <typename T>
 vlc::threads::mutex Singleton<T>::m_mutex;
 



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/6c90087263add6f73939994b18119c4c813e6a74...63033c7cb7835f6d70b5f63d18d9f1d416256092

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/6c90087263add6f73939994b18119c4c813e6a74...63033c7cb7835f6d70b5f63d18d9f1d416256092
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list