[vlc-commits] [Git][videolan/vlc][master] 13 commits: skins2: vout_window: use unique_ptr for timer

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Sun May 15 11:55:28 UTC 2022



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


Commits:
4bd4926f by Alexandre Janniaux at 2022-05-15T11:35:56+00:00
skins2: vout_window: use unique_ptr for timer

The destructor is still needed to release the unique_ptr while keeping
the forward declaration though.

- - - - -
6a377bc4 by Alexandre Janniaux at 2022-05-15T11:35:56+00:00
skins2: async_queue: use std::unique_ptr

- - - - -
d777327c by Alexandre Janniaux at 2022-05-15T11:35:56+00:00
skins2: async_queue: use using= instead of typedef

- - - - -
aac62921 by Alexandre Janniaux at 2022-05-15T11:35:56+00:00
skins2: async_queue: rewrite flush with cxx helpers

- - - - -
8d69ef27 by Alexandre Janniaux at 2022-05-15T11:35:56+00:00
skins2: os_loop: remove redundant destructor

The parent object is correctly defined as virtual, and this object
doesn't allocate nor deallocate anything.

- - - - -
f5993d15 by Alexandre Janniaux at 2022-05-15T11:35:56+00:00
skins2: win32_loop: cleanup constructor/destructor

- - - - -
06a72f35 by Alexandre Janniaux at 2022-05-15T11:35:56+00:00
skins2: x11_loop: cleanup constructor/destructor

- - - - -
0d52e4b3 by Alexandre Janniaux at 2022-05-15T11:35:56+00:00
skins2: os_loop: use std::unique_ptr

- - - - -
24163477 by Alexandre Janniaux at 2022-05-15T11:35:56+00:00
skins2: skin_main: remove thread cancellation

Thread cancellation is not used so there is no point in preventing
cancellation. It didn't work in C++ anyway.

- - - - -
cb958b1f by Alexandre Janniaux at 2022-05-15T11:35:56+00:00
skins2: theme: use unique_ptr

- - - - -
572ae846 by Alexandre Janniaux at 2022-05-15T11:35:56+00:00
skins2: use static_cast instead of C-like cast

- - - - -
cc83e3d6 by Alexandre Janniaux at 2022-05-15T11:35:56+00:00
skins2: use lambda to name window operations

- - - - -
a925e717 by Alexandre Janniaux at 2022-05-15T11:35:56+00:00
skins2: cmd_callbacks: remove no-op destructors

The parent class destructor is already virtual, so there is no need for
an empty destructor. The destructor has been useless since the commit
a18c4d65700229eff926ef1381524bb21a7aab95.

- - - - -


15 changed files:

- modules/gui/skins2/commands/async_queue.cpp
- modules/gui/skins2/commands/async_queue.hpp
- modules/gui/skins2/commands/cmd_callbacks.hpp
- modules/gui/skins2/commands/cmd_change_skin.cpp
- modules/gui/skins2/commands/cmd_layout.cpp
- modules/gui/skins2/src/os_loop.hpp
- modules/gui/skins2/src/skin_common.hpp
- modules/gui/skins2/src/skin_main.cpp
- modules/gui/skins2/src/theme_loader.cpp
- modules/gui/skins2/src/vout_window.cpp
- modules/gui/skins2/src/vout_window.hpp
- modules/gui/skins2/win32/win32_loop.cpp
- modules/gui/skins2/win32/win32_loop.hpp
- modules/gui/skins2/x11/x11_loop.cpp
- modules/gui/skins2/x11/x11_loop.hpp


Changes:

=====================================
modules/gui/skins2/commands/async_queue.cpp
=====================================
@@ -30,24 +30,14 @@
 AsyncQueue::AsyncQueue( intf_thread_t *pIntf ): SkinObject( pIntf ),
     m_cmdFlush( this )
 {
-    // Initialize the mutex
-    vlc_mutex_init( &m_lock );
-
     // Create a timer
     OSFactory *pOsFactory = OSFactory::instance( pIntf );
-    m_pTimer = pOsFactory->createOSTimer( m_cmdFlush );
+    m_pTimer.reset(pOsFactory->createOSTimer(m_cmdFlush));
 
     // Flush the queue every 10 ms
     m_pTimer->start( 10, false );
 }
 
-
-AsyncQueue::~AsyncQueue()
-{
-    delete( m_pTimer );
-}
-
-
 AsyncQueue *AsyncQueue::instance( intf_thread_t *pIntf )
 {
     if( ! pIntf->p_sys->p_queue )
@@ -73,7 +63,7 @@ void AsyncQueue::destroy( intf_thread_t *pIntf )
 
 void AsyncQueue::push( const CmdGenericPtr &rcCommand, bool removePrev )
 {
-    vlc_mutex_lock( &m_lock );
+    vlc::threads::mutex_locker guard {m_lock};
 
     if( removePrev )
     {
@@ -81,8 +71,6 @@ void AsyncQueue::push( const CmdGenericPtr &rcCommand, bool removePrev )
         remove( rcCommand.get()->getType(), rcCommand );
     }
     m_cmdList.push_back( rcCommand );
-
-    vlc_mutex_unlock( &m_lock );
 }
 
 
@@ -110,28 +98,21 @@ void AsyncQueue::remove( const std::string &rType, const CmdGenericPtr &rcComman
 
 void AsyncQueue::flush()
 {
-    while (true)
+    while (!m_cmdList.empty())
     {
-        vlc_mutex_lock( &m_lock );
-
-        if( m_cmdList.size() > 0 )
+        CmdGenericPtr cCommand;
         {
+            vlc::threads::mutex_locker guard {m_lock};
             // Pop the first command from the queue
-            CmdGenericPtr cCommand = m_cmdList.front();
+            cCommand = m_cmdList.front();
             m_cmdList.pop_front();
+        }
 
-            // Unlock the mutex to avoid deadlocks if another thread wants to
-            // enqueue/remove a command while this one is processed
-            vlc_mutex_unlock( &m_lock );
+        // Unlock the mutex to avoid deadlocks if another thread wants to
+        // enqueue/remove a command while this one is processed
 
-            // Execute the command
-            cCommand.get()->execute();
-        }
-        else
-        {
-            vlc_mutex_unlock( &m_lock );
-            break;
-        }
+        // Execute the command
+        cCommand.get()->execute();
     }
 }
 


=====================================
modules/gui/skins2/commands/async_queue.hpp
=====================================
@@ -28,6 +28,9 @@
 
 #include <list>
 #include <string>
+#include <memory>
+
+#include <vlc_cxx_helpers.hpp>
 
 class OSTimer;
 
@@ -55,16 +58,15 @@ public:
 
 private:
     /// Command queue
-    typedef std::list<CmdGenericPtr> cmdList_t;
+    using cmdList_t = std::list<CmdGenericPtr>;
     cmdList_t m_cmdList;
     /// Timer
-    OSTimer *m_pTimer;
+    std::unique_ptr<OSTimer> m_pTimer;
     /// Mutex
-    vlc_mutex_t m_lock;
+    vlc::threads::mutex m_lock;
 
     // Private because it is a singleton
     AsyncQueue( intf_thread_t *pIntf );
-    virtual ~AsyncQueue();
 
     // Callback to flush the queue
     DEFINE_CALLBACK( AsyncQueue, Flush );


=====================================
modules/gui/skins2/commands/cmd_callbacks.hpp
=====================================
@@ -38,9 +38,6 @@ public:
           m_label( label ), m_pfExecute( func )
     {
     }
-    virtual ~CmdCallback()
-    {
-    }
     virtual void execute()
     {
         if( !m_pfExecute )
@@ -68,10 +65,6 @@ public:
         vlc_cond_init( &m_wait );
     }
 
-    virtual ~CmdExecuteBlock()
-    {
-    }
-
     static void executeWait( const CmdGenericPtr& rcCommand  )
     {
         CmdExecuteBlock& rCmd = (CmdExecuteBlock&)*rcCommand.get();


=====================================
modules/gui/skins2/commands/cmd_change_skin.cpp
=====================================
@@ -36,7 +36,7 @@
 void CmdChangeSkin::execute()
 {
     // Save the old theme to restore it in case of problem
-    Theme *pOldTheme = getIntf()->p_sys->p_theme;
+    auto pOldTheme = std::move(getIntf()->p_sys->p_theme);
 
     if( pOldTheme )
     {
@@ -52,7 +52,7 @@ void CmdChangeSkin::execute()
         // Everything went well
         msg_Info( getIntf(), "new theme successfully loaded (%s)",
                  m_file.c_str() );
-        delete pOldTheme;
+        pOldTheme.reset();
 
         // restore vout config
         VoutManager::instance( getIntf() )->restoreVoutConfig( true );
@@ -61,9 +61,9 @@ void CmdChangeSkin::execute()
     {
         msg_Warn( getIntf(), "a problem occurred when loading the new theme,"
                   " restoring the previous one" );
-        getIntf()->p_sys->p_theme = pOldTheme;
+        getIntf()->p_sys->p_theme = std::move(pOldTheme);
         VoutManager::instance( getIntf() )->restoreVoutConfig( false );
-        pOldTheme->getWindowManager().restoreVisibility();
+        getIntf()->p_sys->p_theme->getWindowManager().restoreVisibility();
     }
     else
     {


=====================================
modules/gui/skins2/commands/cmd_layout.cpp
=====================================
@@ -34,7 +34,7 @@ CmdLayout::CmdLayout( intf_thread_t *pIntf, TopWindow &rWindow,
 
 void CmdLayout::execute()
 {
-    Theme *p_theme = getIntf()->p_sys->p_theme;
+    auto &p_theme = getIntf()->p_sys->p_theme;
     if( p_theme )
         p_theme->getWindowManager().setActiveLayout( m_rWindow, m_rLayout );
 }


=====================================
modules/gui/skins2/src/os_loop.hpp
=====================================
@@ -31,8 +31,6 @@
 class OSLoop: public SkinObject
 {
 public:
-    virtual ~OSLoop() { }
-
     /// Enter the main loop
     virtual void run() = 0;
 


=====================================
modules/gui/skins2/src/skin_common.hpp
=====================================
@@ -36,6 +36,7 @@
 #include <vlc_fs.h>
 
 #include <string>
+#include <memory>
 
 class AsyncQueue;
 class Logger;
@@ -98,7 +99,7 @@ struct intf_sys_t
     /// Factory for OS specific classes
     OSFactory *p_osFactory;
     /// Main OS specific message loop
-    OSLoop *p_osLoop;
+    std::unique_ptr<OSLoop> p_osLoop;
     /// Variable manager
     VarManager *p_varManager;
     /// VLC state handler
@@ -111,7 +112,7 @@ struct intf_sys_t
     ThemeRepository *p_repository;
 
     /// Current theme
-    Theme *p_theme;
+    std::unique_ptr<Theme> p_theme;
 
     /// synchronisation at start of interface
     vlc_thread_t thread;


=====================================
modules/gui/skins2/src/skin_main.cpp
=====================================
@@ -83,7 +83,6 @@ static int Open( vlc_object_t *p_this )
     p_intf->p_sys->p_dialogs = NULL;
     p_intf->p_sys->p_interpreter = NULL;
     p_intf->p_sys->p_osFactory = NULL;
-    p_intf->p_sys->p_osLoop = NULL;
     p_intf->p_sys->p_varManager = NULL;
     p_intf->p_sys->p_voutManager = NULL;
     p_intf->p_sys->p_vlcProc = NULL;
@@ -156,14 +155,12 @@ static void Close( vlc_object_t *p_this )
 //---------------------------------------------------------------------------
 static void *Run( void * p_obj )
 {
-    int canc = vlc_savecancel();
-
     intf_thread_t *p_intf = (intf_thread_t *)p_obj;
 
     bool b_error = false;
     char *skin_last = NULL;
-    ThemeLoader *pLoader = NULL;
     OSLoop *loop = NULL;
+    std::unique_ptr<ThemeLoader> pLoader;
 
     // Initialize singletons
     if( OSFactory::instance( p_intf ) == NULL )
@@ -223,7 +220,7 @@ static void *Run( void * p_obj )
 
     // Load a theme
     skin_last = config_GetPsz( "skins2-last" );
-    pLoader = new ThemeLoader( p_intf );
+    pLoader = std::make_unique<ThemeLoader>(p_intf);
 
     if( !skin_last || !pLoader->load( skin_last ) )
     {
@@ -234,7 +231,7 @@ static void *Run( void * p_obj )
         msg_Err( p_intf, "no skins found : exiting");
     }
 
-    delete pLoader;
+    pLoader.reset();
     free( skin_last );
 
     // Get the instance of OSLoop
@@ -254,10 +251,7 @@ static void *Run( void * p_obj )
     if( p_intf->p_sys->p_theme )
     {
         p_intf->p_sys->p_theme->saveConfig();
-
-        delete p_intf->p_sys->p_theme;
-        p_intf->p_sys->p_theme = NULL;
-
+        p_intf->p_sys->p_theme.reset();
         msg_Dbg( p_intf, "current theme deleted" );
     }
 
@@ -282,7 +276,6 @@ end:
         vlc_sem_post( &p_intf->p_sys->init_wait );
     }
 
-    vlc_restorecancel(canc);
     return NULL;
 }
 
@@ -315,7 +308,7 @@ static void WindowSetFullscreen( vout_window_t *pWnd, const char * );
 
 static int WindowEnable( vout_window_t *pWnd, const vout_window_cfg_t *cfg )
 {
-    vout_window_skins_t* sys = (vout_window_skins_t *)pWnd->sys;
+    vout_window_skins_t* sys = static_cast<vout_window_skins_t*>(pWnd->sys);
     intf_thread_t *pIntf = sys->pIntf;
 
     sys->cfg = *cfg;
@@ -409,16 +402,17 @@ static void WindowSetFullscreen( vout_window_t *pWnd, const char * )
     pQueue->push( CmdGenericPtr( pCmd ) );
 }
 
-static const struct vout_window_operations window_ops = {
-    WindowEnable,
-    WindowDisable,
-    WindowResize,
-    WindowClose,
-    WindowSetState,
-    WindowUnsetFullscreen,
-    WindowSetFullscreen,
-    NULL,
-};
+static const auto window_ops = []{
+    struct vout_window_operations ops {};
+    ops.enable = WindowEnable;
+    ops.disable = WindowDisable;
+    ops.resize = WindowResize;
+    ops.destroy = WindowClose;
+    ops.set_state = WindowSetState;
+    ops.unset_fullscreen = WindowUnsetFullscreen;
+    ops.set_fullscreen = WindowSetFullscreen;
+    return ops;
+}();
 
 static int WindowOpen( vout_window_t *pWnd )
 {


=====================================
modules/gui/skins2/src/theme_loader.cpp
=====================================
@@ -111,7 +111,7 @@ bool ThemeLoader::load( const std::string &fileName )
     if( ! extract( fileName ) && ! parse( path, fileName ) )
         return false;
 
-    Theme *pNewTheme = getIntf()->p_sys->p_theme;
+    auto &pNewTheme = getIntf()->p_sys->p_theme;
     if( !pNewTheme )
         return false;
 
@@ -308,7 +308,7 @@ bool ThemeLoader::parse( const std::string &path, const std::string &xmlFile )
 
     // Build and store the theme
     Builder builder( getIntf(), parser.getData(), path );
-    getIntf()->p_sys->p_theme = builder.build();
+    getIntf()->p_sys->p_theme.reset(builder.build());
 
     return true;
 }


=====================================
modules/gui/skins2/src/vout_window.cpp
=====================================
@@ -50,19 +50,11 @@ VoutWindow::VoutWindow( intf_thread_t *pIntf, vout_window_t* pWnd,
     {
         updateWindowConfiguration( m_pWnd );
 
-        m_pTimer = pOsFactory->createOSTimer( m_cmdHideMouse );
-    }
-}
-
-
-VoutWindow::~VoutWindow()
-{
-    if( m_pWnd )
-    {
-        delete m_pTimer;
+        m_pTimer.reset(pOsFactory->createOSTimer( m_cmdHideMouse ));
     }
 }
 
+VoutWindow::~VoutWindow() = default;
 
 void VoutWindow::setCtrlVideo( CtrlVideo* pCtrlVideo )
 {


=====================================
modules/gui/skins2/src/vout_window.hpp
=====================================
@@ -27,6 +27,7 @@
 #include "dialogs.hpp"
 #include "../commands/cmd_generic.hpp"
 #include <vlc_vout_window.h>
+#include <memory>
 
 class OSGraphics;
 class OSTimer;
@@ -40,7 +41,7 @@ public:
 
     VoutWindow( intf_thread_t *pIntf, struct vout_window_t* pWnd,
                 int width, int height, GenericWindow* pParent = NULL );
-    virtual ~VoutWindow();
+    ~VoutWindow();
 
     /// Make some functions public
     //@{
@@ -98,7 +99,7 @@ private:
     GenericWindow* m_pParentWindow;
 
     // Cursor timer
-    OSTimer *m_pTimer;
+    std::unique_ptr<OSTimer> m_pTimer;
     int mouse_hide_timeout;
     DEFINE_CALLBACK( VoutWindow, HideMouse );
 };


=====================================
modules/gui/skins2/win32/win32_loop.cpp
=====================================
@@ -99,27 +99,19 @@ Win32Loop::Win32Loop( intf_thread_t *pIntf ): OSLoop( pIntf )
     virtKeyToVlcKey[VK_MEDIA_PLAY_PAUSE] = KEY_MEDIA_PLAY_PAUSE;
 }
 
-
-Win32Loop::~Win32Loop()
-{
-}
-
-
 OSLoop *Win32Loop::instance( intf_thread_t *pIntf )
 {
+
     if( pIntf->p_sys->p_osLoop == NULL )
-    {
-        OSLoop *pOsLoop = new Win32Loop( pIntf );
-        pIntf->p_sys->p_osLoop = pOsLoop;
-    }
-    return pIntf->p_sys->p_osLoop;
+        pIntf->p_sys->p_osLoop = std::make_unique<Win32Loop>(pIntf);
+    return pIntf->p_sys->p_osLoop.get();
 }
 
 
 void Win32Loop::destroy( intf_thread_t *pIntf )
 {
-    delete pIntf->p_sys->p_osLoop;
-    pIntf->p_sys->p_osLoop = NULL;
+    assert(pIntf->p_sys->p_osLoop);
+    pIntf->p_sys->p_osLoop.reset();
 }
 
 


=====================================
modules/gui/skins2/win32/win32_loop.hpp
=====================================
@@ -35,6 +35,8 @@ class GenericWindow;
 class Win32Loop: public OSLoop
 {
 public:
+    Win32Loop( intf_thread_t *pIntf );
+
     /// Get the instance of Win32Loop
     static OSLoop *instance( intf_thread_t *pIntf );
 
@@ -52,9 +54,6 @@ public:
                                            WPARAM wParam, LPARAM lParam );
 
 private:
-    // Private because it is a singleton
-    Win32Loop( intf_thread_t *pIntf );
-    virtual ~Win32Loop();
 
     /// Map associating special (i.e. non ascii) virtual key codes with
     /// internal vlc key codes


=====================================
modules/gui/skins2/x11/x11_loop.cpp
=====================================
@@ -105,27 +105,17 @@ X11Loop::X11Loop( intf_thread_t *pIntf, X11Display &rDisplay ):
     }
 }
 
-
-X11Loop::~X11Loop()
-{
-}
-
-
 OSLoop *X11Loop::instance( intf_thread_t *pIntf, X11Display &rDisplay )
 {
-    if( pIntf->p_sys->p_osLoop == NULL )
-    {
-        OSLoop *pOsLoop = new X11Loop( pIntf, rDisplay );
-        pIntf->p_sys->p_osLoop = pOsLoop;
-    }
-    return pIntf->p_sys->p_osLoop;
+    if (pIntf->p_sys->p_osLoop == NULL)
+        pIntf->p_sys->p_osLoop = std::make_unique<X11Loop>(pIntf, rDisplay);
+    return pIntf->p_sys->p_osLoop.get();
 }
 
 
 void X11Loop::destroy( intf_thread_t *pIntf )
 {
-    delete pIntf->p_sys->p_osLoop;
-    pIntf->p_sys->p_osLoop = NULL;
+    pIntf->p_sys->p_osLoop.reset();
 }
 
 


=====================================
modules/gui/skins2/x11/x11_loop.hpp
=====================================
@@ -28,6 +28,7 @@
 #include "../src/os_loop.hpp"
 
 #include <map>
+#include <memory>
 
 class X11Display;
 class GenericWindow;
@@ -36,6 +37,8 @@ class GenericWindow;
 class X11Loop: public OSLoop
 {
 public:
+    X11Loop( intf_thread_t *pIntf, X11Display &rDisplay );
+
     /// Get the instance of X11Loop
     static OSLoop *instance( intf_thread_t *pIntf, X11Display &rDisplay );
 
@@ -70,10 +73,6 @@ private:
     }
     static int X11ModToMod( unsigned state );
 
-    // Private because it's a singleton
-    X11Loop( intf_thread_t *pIntf, X11Display &rDisplay );
-    virtual ~X11Loop();
-
     /// Handle the next X11 event
     void handleX11Event();
 };



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/5756807860da7eb58bbf316c1d5e0dbd4494f6cc...a925e7178cf8b75e76bba5c12eab903d6461d168

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/5756807860da7eb58bbf316c1d5e0dbd4494f6cc...a925e7178cf8b75e76bba5c12eab903d6461d168
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