]> git.sesse.net Git - vlc/commitdiff
Adding a generic singleton pattern implementation
authorHugo Beauzee-Luyssen <beauze.h@gmail.com>
Mon, 21 Dec 2009 14:10:05 +0000 (15:10 +0100)
committerJean-Baptiste Kempf <jb@videolan.org>
Fri, 25 Dec 2009 17:16:20 +0000 (18:16 +0100)
Signed-off-by: Jean-Baptiste Kempf <jb@videolan.org>
modules/gui/qt4/Modules.am
modules/gui/qt4/util/singleton.hpp [new file with mode: 0644]

index 9ce1baf6d12e7b687ac13fd926539d785e9da7e1..15f842cf1cb1138c425f815c5f7bc8cda2f786cf 100644 (file)
@@ -302,7 +302,9 @@ noinst_HEADERS = \
        util/qvlcframe.hpp \
        util/qvlcapp.hpp \
        util/qt_dirs.hpp \
-       util/registry.hpp
+       util/registry.hpp \
+       util/singleton.hpp
+
 
 EXTRA_DIST += \
        vlc.qrc \
diff --git a/modules/gui/qt4/util/singleton.hpp b/modules/gui/qt4/util/singleton.hpp
new file mode 100644 (file)
index 0000000..f6f6050
--- /dev/null
@@ -0,0 +1,63 @@
+/*****************************************************************************
+ * singleton.hpp: Generic Singleton pattern implementation
+ ****************************************************************************
+ * Copyright (C) 2009 VideoLAN
+ *
+ * Authors: Hugo Beauzee-Luyssen <beauze.h # gmail - com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef _SINGLETON_HPP_
+#define _SINGLETON_HPP_
+
+#include <stdlib.h>
+#include "qt4.hpp"
+
+template <typename T>
+class       Singleton
+{
+public:
+    static T*      getInstance( intf_thread_t *p_intf = NULL )
+    {
+        if ( m_instance == NULL )
+            m_instance = new T( p_intf );
+        return m_instance;
+    }
+
+    static void    killInstance()
+    {
+        if ( m_instance != NULL )
+        {
+            delete m_instance;
+            m_instance = NULL;
+        }
+    }
+protected:
+    Singleton(){}
+    virtual ~Singleton(){}
+    /* Not implemented since these methods should *NEVER* been called.
+    If they do, it probably won't compile :) */
+    Singleton(const Singleton<T>&);
+    Singleton<T>&   operator=(const Singleton<T>&);
+
+private:
+    static T*      m_instance;
+};
+
+template <typename T>
+T*  Singleton<T>::m_instance = NULL;
+
+#endif // _SINGLETON_HPP_