From b97736a0a89ecbc3aa2ece133cc0a4839bd1e043 Mon Sep 17 00:00:00 2001 From: Christian Gmeiner Date: Wed, 7 Jan 2009 14:59:04 +0100 Subject: better singleton template With the old singleton pattern it could happen that two instances are created. See... static T& Instance() { static T m_Instance; return m_Instance; } static T* InstanceP() { static T* m_InstanceP = new T; return m_InstanceP; } --- Makefile | 6 +++--- dxr3.c | 14 +++++--------- dxr3singleton.h | 30 ++++++++++++++---------------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 0efa259..d8183a6 100644 --- a/Makefile +++ b/Makefile @@ -20,10 +20,10 @@ CXXFLAGS = -O2 -Wall -Woverloaded-virtual ### The directory environment: -VDRDIR = ../../.. -LIBDIR = ../../lib +VDRDIR = /usr/include/vdr +LIBDIR = . TMPDIR = /tmp -FFMDIR = /usr/local/include/ffmpeg +FFMDIR = /usr//include/libavcodec EM8300 = /usr/include ### Allow user defined options to overwrite defaults: diff --git a/dxr3.c b/dxr3.c index 264aa72..bfa715b 100644 --- a/dxr3.c +++ b/dxr3.c @@ -33,8 +33,7 @@ eOSState cDxr3OsdItem::ProcessKey(eKeys Key) { case DXR3_RESET_HARDWARE: cDxr3Interface::Instance().ResetHardware(); - if (cDxr3Device::InstanceP()) - cDxr3Device::InstanceP()->Reset(); + cDxr3Device::Instance().Reset(); break; case DXR3_FORCE_LETTER_BOX: @@ -44,14 +43,12 @@ eOSState cDxr3OsdItem::ProcessKey(eKeys Key) case DXR3_ANALOG_OUT: cDxr3ConfigData::Instance().SetUseDigitalOut(0); - if (cDxr3Device::InstanceP()) - cDxr3Device::InstanceP()->Reset(); + cDxr3Device::Instance().Reset(); break; case DXR3_DIGITAL_OUT: cDxr3ConfigData::Instance().SetUseDigitalOut(1); - if (cDxr3Device::InstanceP()) - cDxr3Device::InstanceP()->Reset(); + cDxr3Device::Instance().Reset(); break; } } @@ -123,8 +120,7 @@ void cMenuSetupDxr3::Store(void) cDxr3Interface::Instance().SetBrightness(newBrightness); cDxr3Interface::Instance().SetContrast(newContrast); cDxr3Interface::Instance().SetSaturation(newSaturation); - if (cDxr3Device::InstanceP()) - cDxr3Device::InstanceP()->Reset(); + cDxr3Device::Instance().Reset(); } // ================================== @@ -175,7 +171,7 @@ cPluginDxr3::~cPluginDxr3() // ================================== bool cPluginDxr3::Initialize() { - cDxr3Device::InstanceP(); + cDxr3Device::Instance(); return true; } diff --git a/dxr3singleton.h b/dxr3singleton.h index b289328..ebfbe42 100644 --- a/dxr3singleton.h +++ b/dxr3singleton.h @@ -28,27 +28,25 @@ Is a nice solution to use only one instance of a class. */ -template -class Singleton -{ +template +class Singleton { +public: + virtual ~Singleton() {} + static T& Instance(); + protected: Singleton() {} - virtual ~Singleton() {} -public: - static T& Instance() - { - static T m_Instance; - return m_Instance; - } - - static T* InstanceP() - { - static T* m_InstanceP = new T; - return m_InstanceP; - } +private: + Singleton(const Singleton&); }; +template +T& Singleton::Instance() { + static T instance; + return instance; +} + #endif /*_DXR3_SINGLETON_H_*/ // Local variables: -- cgit v1.2.3