mirror of
https://github.com/RetroDECK/ES-DE.git
synced 2025-03-06 14:27:43 +00:00
Improve singleton implementation
Still not thread-safe, but a bit better now. Should be made thread-safe and maybe get converted to a template, if possible.
This commit is contained in:
parent
e51dd35166
commit
6d499d4e3a
|
@ -12,7 +12,7 @@
|
||||||
const char * VolumeControl::mixerCard = "default";
|
const char * VolumeControl::mixerCard = "default";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::shared_ptr<VolumeControl> VolumeControl::sInstance;
|
std::weak_ptr<VolumeControl> VolumeControl::sInstance;
|
||||||
|
|
||||||
|
|
||||||
VolumeControl::VolumeControl()
|
VolumeControl::VolumeControl()
|
||||||
|
@ -31,6 +31,20 @@ VolumeControl::VolumeControl()
|
||||||
originalVolume = getVolume();
|
originalVolume = getVolume();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VolumeControl::VolumeControl(const VolumeControl & right)
|
||||||
|
{
|
||||||
|
sInstance = right.sInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
VolumeControl & VolumeControl::operator=(const VolumeControl & right)
|
||||||
|
{
|
||||||
|
if (this != &right) {
|
||||||
|
sInstance = right.sInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
VolumeControl::~VolumeControl()
|
VolumeControl::~VolumeControl()
|
||||||
{
|
{
|
||||||
//set original volume levels for system
|
//set original volume levels for system
|
||||||
|
@ -42,10 +56,12 @@ VolumeControl::~VolumeControl()
|
||||||
std::shared_ptr<VolumeControl> & VolumeControl::getInstance()
|
std::shared_ptr<VolumeControl> & VolumeControl::getInstance()
|
||||||
{
|
{
|
||||||
//check if an VolumeControl instance is already created, if not create one
|
//check if an VolumeControl instance is already created, if not create one
|
||||||
if (sInstance == nullptr) {
|
static std::shared_ptr<VolumeControl> sharedInstance = sInstance.lock();
|
||||||
sInstance = std::shared_ptr<VolumeControl>(new VolumeControl);
|
if (sharedInstance == nullptr) {
|
||||||
|
sharedInstance.reset(new VolumeControl);
|
||||||
|
sInstance = sharedInstance;
|
||||||
}
|
}
|
||||||
return sInstance;
|
return sharedInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
void VolumeControl::init()
|
void VolumeControl::init()
|
||||||
|
|
|
@ -39,9 +39,11 @@ class VolumeControl
|
||||||
int originalVolume;
|
int originalVolume;
|
||||||
int internalVolume;
|
int internalVolume;
|
||||||
|
|
||||||
static std::shared_ptr<VolumeControl> sInstance;
|
static std::weak_ptr<VolumeControl> sInstance;
|
||||||
|
|
||||||
VolumeControl();
|
VolumeControl();
|
||||||
|
VolumeControl(const VolumeControl & right);
|
||||||
|
VolumeControl & operator=(const VolumeControl & right);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static std::shared_ptr<VolumeControl> & getInstance();
|
static std::shared_ptr<VolumeControl> & getInstance();
|
||||||
|
@ -52,5 +54,5 @@ public:
|
||||||
int getVolume() const;
|
int getVolume() const;
|
||||||
void setVolume(int volume);
|
void setVolume(int volume);
|
||||||
|
|
||||||
virtual ~VolumeControl();
|
~VolumeControl();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue