parent
dfe58dc4ce
commit
8302f2c691
@ -0,0 +1,53 @@
|
||||
#ifndef SINGLETON_DCLP_HPP
|
||||
#define SINGLETON_DCLP_HPP
|
||||
|
||||
#include <mutex>
|
||||
|
||||
|
||||
template<typename T>
|
||||
class Singleton_DCLP
|
||||
{
|
||||
protected:
|
||||
|
||||
Singleton_DCLP() {};
|
||||
virtual ~Singleton_DCLP() {};
|
||||
|
||||
private:
|
||||
|
||||
Singleton_DCLP( const Singleton_DCLP& );
|
||||
Singleton_DCLP& operator=( const Singleton_DCLP& );
|
||||
|
||||
public:
|
||||
|
||||
static T* getInstance()
|
||||
{
|
||||
if ( not m_instance )
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(m_lock);
|
||||
|
||||
// this is now the critical section
|
||||
|
||||
if ( not m_instance ) // re-check pinstance
|
||||
{
|
||||
// Douglas Schmidt proposed volatile
|
||||
// to prevent "agressive optimalizations"
|
||||
volatile T *temp = new T();
|
||||
m_instance = (T*)temp;
|
||||
}
|
||||
}
|
||||
|
||||
return m_instance;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
static std::mutex m_lock;
|
||||
static T* m_instance;
|
||||
};
|
||||
|
||||
template<class T> std::mutex Singleton_DCLP<T>::m_lock;
|
||||
template<class T> T* Singleton_DCLP<T>::m_instance = 0;
|
||||
|
||||
|
||||
#endif // SINGLETON_DCLP_HPP
|
@ -0,0 +1,28 @@
|
||||
#ifndef SINGLETON_MEYERS_HPP
|
||||
#define SINGLETON_MEYERS_HPP
|
||||
|
||||
template<typename T>
|
||||
class Singleton_meyers
|
||||
{
|
||||
protected:
|
||||
|
||||
Singleton_meyers() {};
|
||||
virtual ~Singleton_meyers() {};
|
||||
|
||||
private:
|
||||
|
||||
Singleton_meyers( const Singleton_meyers& );
|
||||
Singleton_meyers& operator=( const Singleton_meyers& );
|
||||
|
||||
public:
|
||||
|
||||
inline static T* getInstance()
|
||||
{
|
||||
/// @note static local initialization is thread safe in c++011
|
||||
static T instance;
|
||||
return &instance;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif // SINGLETON_MEYERS_HPP
|
@ -0,0 +1,35 @@
|
||||
#include <cxxtest/TestSuite.h>
|
||||
|
||||
#define private public // need to reach Singleton's private m_instance
|
||||
|
||||
#include "Common.hpp"
|
||||
#include "Fixture.hpp"
|
||||
#include "Singleton_DCLP.hpp"
|
||||
|
||||
|
||||
class TestSingletonDCLPSuite : public CxxTest::TestSuite
|
||||
{
|
||||
|
||||
private:
|
||||
|
||||
class BasicSingleton : public Singleton_DCLP<BasicSingleton>
|
||||
{
|
||||
public:
|
||||
int getSeven()
|
||||
{
|
||||
TRACE;
|
||||
return 7;
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
void testBasic( void )
|
||||
{
|
||||
TEST_HEADER;
|
||||
|
||||
TS_ASSERT_EQUALS( BasicSingleton::getInstance()->getSeven(), 7 );
|
||||
}
|
||||
|
||||
|
||||
};
|
@ -0,0 +1,35 @@
|
||||
#include <cxxtest/TestSuite.h>
|
||||
|
||||
#define private public // need to reach Singleton's private m_instance
|
||||
|
||||
#include "Common.hpp"
|
||||
#include "Fixture.hpp"
|
||||
#include "Singleton_DCLP.hpp"
|
||||
|
||||
|
||||
class TestSingletonDCLPSuite : public CxxTest::TestSuite
|
||||
{
|
||||
|
||||
private:
|
||||
|
||||
class BasicSingleton : public Singleton_DCLP<BasicSingleton>
|
||||
{
|
||||
public:
|
||||
int getSeven()
|
||||
{
|
||||
TRACE;
|
||||
return 7;
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
void testBasic( void )
|
||||
{
|
||||
TEST_HEADER;
|
||||
|
||||
TS_ASSERT_EQUALS( BasicSingleton::getInstance()->getSeven(), 7 );
|
||||
}
|
||||
|
||||
|
||||
};
|
@ -0,0 +1,35 @@
|
||||
#include <cxxtest/TestSuite.h>
|
||||
|
||||
#define private public // need to reach Singleton's private m_instance
|
||||
|
||||
#include "Common.hpp"
|
||||
#include "Fixture.hpp"
|
||||
#include "Singleton_meyers.hpp"
|
||||
|
||||
|
||||
class TestSingletonMeyersSuite : public CxxTest::TestSuite
|
||||
{
|
||||
|
||||
private:
|
||||
|
||||
class BasicSingleton : public Singleton_meyers<BasicSingleton>
|
||||
{
|
||||
public:
|
||||
int getSeven()
|
||||
{
|
||||
TRACE;
|
||||
return 7;
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
void testBasic( void )
|
||||
{
|
||||
TEST_HEADER;
|
||||
|
||||
TS_ASSERT_EQUALS( BasicSingleton::getInstance()->getSeven(), 7 );
|
||||
}
|
||||
|
||||
|
||||
};
|
Loading…
Reference in new issue