Skip to content

Commit c0cc4f9

Browse files
authored
Merge pull request #282 from Libvisual/singleton-enhancements
Core: Enhance and clean up singleton classes.
2 parents 31e7eaa + 64c6a75 commit c0cc4f9

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
lines changed

libvisual/libvisual/lv_libvisual.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,14 @@ namespace LV
9797
};
9898
}
9999

100-
template <>
101-
LV_API System* Singleton<System>::m_instance = nullptr;
102-
103100
void System::init (int& argc, char**& argv)
104101
{
105102
if (m_instance) {
106-
visual_log (VISUAL_LOG_WARNING, "Attempt to initialize LV a second time");
103+
visual_log (VISUAL_LOG_WARNING, "Attempt to initialize LV a second time.");
107104
return;
108105
}
109106

110-
m_instance = new System (argc, argv);
107+
m_instance.reset (new System {argc, argv});
111108
}
112109

113110
std::string System::get_version () const
@@ -136,7 +133,7 @@ namespace LV
136133
}
137134

138135
System::System (int& argc, char**& argv)
139-
: m_impl(new Impl)
136+
: m_impl {std::make_unique<Impl> ()}
140137
{
141138
(void)argc;
142139
(void)argv;

libvisual/libvisual/lv_libvisual.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@
4141
//! Libvisual namespace
4242
namespace LV {
4343

44-
class LV_API System;
45-
template <>
46-
LV_API System* Singleton<System>::m_instance;
47-
48-
class LV_API System
44+
class LV_API System final
4945
: public Singleton<System>
5046
{
5147
public:
@@ -60,7 +56,7 @@ namespace LV {
6056

6157
System (System const&) = delete;
6258

63-
virtual ~System ();
59+
~System () override;
6460

6561
/**
6662
* Returns the Libvisual version.

libvisual/libvisual/lv_plugin_registry.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,18 @@ namespace LV {
7979
return ref;
8080
}
8181

82-
template <>
83-
LV_API PluginRegistry* Singleton<PluginRegistry>::m_instance = nullptr;
84-
8582
void PluginRegistry::init ()
8683
{
87-
if (!m_instance)
88-
m_instance = new PluginRegistry;
84+
if (m_instance) {
85+
visual_log (VISUAL_LOG_WARNING, "Attempt to initialize plugin registry a second time.");
86+
return;
87+
}
88+
89+
m_instance.reset (new PluginRegistry {});
8990
}
9091

9192
PluginRegistry::PluginRegistry ()
92-
: m_impl (new Impl)
93+
: m_impl {std::make_unique<Impl> ()}
9394
{
9495
visual_log (VISUAL_LOG_DEBUG, "Initializing plugin registry");
9596

libvisual/libvisual/lv_plugin_registry.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,21 @@ namespace LV {
1616
typedef ::VisPluginType PluginType;
1717

1818
class LV_API PluginRegistry;
19-
template<>
20-
LV_API PluginRegistry* Singleton<PluginRegistry>::m_instance;
2119

2220
//! Manages the registry of plugins
2321
//!
2422
//! @note This is a singleton class. Its only instance must
2523
//! be accessed via the instance() method.
2624
//!
27-
class LV_API PluginRegistry
25+
class LV_API PluginRegistry final
2826
: public Singleton<PluginRegistry>
2927
{
3028
public:
3129

3230
PluginRegistry (PluginRegistry const&) = delete;
3331

3432
/** Destructor */
35-
virtual ~PluginRegistry ();
33+
~PluginRegistry () override;
3634

3735
/**
3836
* Adds an extra plugin search path.

libvisual/libvisual/lv_singleton.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef _LV_SINGLETON_HPP
22
#define _LV_SINGLETON_HPP
33

4+
#include <memory>
5+
46
namespace LV {
57

68
//! Singleton class template.
@@ -24,20 +26,28 @@ namespace LV {
2426
//! Returns the singleton instance
2527
//!
2628
//! @return singleton instance
27-
static T* instance () {
28-
return m_instance;
29+
static T* instance ()
30+
{
31+
return m_instance.get ();
32+
}
33+
34+
//! Returns the singleton instance as const.
35+
//!
36+
//! @return Singleton instance.
37+
static T const* const_instance ()
38+
{
39+
return m_instance.get ();
2940
}
3041

3142
//! Destroys the singleton instance
3243
static void destroy ()
3344
{
34-
delete m_instance;
35-
m_instance = nullptr;
45+
m_instance.reset ();
3646
}
3747

3848
protected:
3949

40-
static T* m_instance;
50+
inline static std::unique_ptr<T> m_instance {};
4151

4252
Singleton () = default;
4353
};

0 commit comments

Comments
 (0)