[vlmc-devel] Core: Use a C++11 way of providing a singleton

Hugo Beauzée-Luyssen git at videolan.org
Fri Mar 4 17:14:07 CET 2016


vlmc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Wed Mar  2 14:42:06 2016 +0100| [c2492737610a2e0a652f099257059cf27cb2be46] | committer: Hugo Beauzée-Luyssen

Core: Use a C++11 way of providing a singleton

This is thread safe and guarantees Core will always be cleaned upon exit

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

 src/Main/Core.cpp | 7 +++++++
 src/Main/Core.h   | 8 +++-----
 src/Main/main.cpp | 1 -
 3 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/Main/Core.cpp b/src/Main/Core.cpp
index b5fce99..1bfa53c 100644
--- a/src/Main/Core.cpp
+++ b/src/Main/Core.cpp
@@ -174,3 +174,10 @@ Project* Core::currentProject()
 {
     return m_currentProject;
 }
+
+Core*
+Core::getInstance()
+{
+    static Core core;
+    return &core;
+}
diff --git a/src/Main/Core.h b/src/Main/Core.h
index 9ce6acd..2dedfc4 100644
--- a/src/Main/Core.h
+++ b/src/Main/Core.h
@@ -39,9 +39,7 @@ namespace Backend
 
 #include <QObject>
 
-#include <Tools/Singleton.hpp>
-
-class Core : public QObject, public Singleton<Core>
+class Core : public QObject
 {
     Q_OBJECT
 
@@ -60,6 +58,8 @@ class Core : public QObject, public Singleton<Core>
         bool                    restoreProject();
         bool                    isProjectLoaded();
 
+        static Core*            getInstance();
+
     signals:
         /**
          * @brief projectLoaded Will be emited right before a project starts loading.
@@ -86,8 +86,6 @@ class Core : public QObject, public Singleton<Core>
         AutomaticBackup*        m_automaticBackup;
         Workspace*              m_workspace;
         Project*                m_currentProject;
-
-        friend class Singleton<Core>;
 };
 
 #endif // CORE_H
diff --git a/src/Main/main.cpp b/src/Main/main.cpp
index a09ceb6..8236ecc 100644
--- a/src/Main/main.cpp
+++ b/src/Main/main.cpp
@@ -212,7 +212,6 @@ VLMCmain( int argc, char **argv )
 #else
     int res = VLMCCoremain( argc, argv );
 #endif
-    Core::destroyInstance();
     return res;
 }
 



More information about the Vlmc-devel mailing list