new singleton implementations

master
Denes Matetelki 13 years ago
parent dfe58dc4ce
commit 8302f2c691

@ -1,9 +1,6 @@
#ifndef SINGLETON_HPP
#define SINGLETON_HPP
#include "Common.hpp"
#include "Logger.hpp"
template<typename T>
class Singleton

@ -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

@ -3,6 +3,8 @@
#include <time.h> //time
#include "Colors.hpp"
#include "Common.hpp"
void Logger::init(std::ostream& log_stream )
{

@ -1,5 +1,5 @@
set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow "
"-ggdb -fprofile-arcs -ftest-coverage " )
"-ggdb -fprofile-arcs -ftest-coverage --std=c++0x " )
add_definitions( ${CXX_FLAGS} )
# add_definitions( -DNO_TRACE )
@ -16,15 +16,17 @@ if(CXXTEST_FOUND)
generated_main.cpp
Fixture.hpp
test_Singelton.hpp
test_Mutex.hpp
test_ScopedLock.hpp
test_ConditionalVariable.hpp
test_Thread.hpp
test_ThreadPool.hpp
test_Semaphore.hpp
test_Timer.hpp
test_Common.hpp
test_Singleton.hpp
test_Singleton_meyers.hpp
test_Singleton_DCLP.hpp
# test_Mutex.hpp
# test_ScopedLock.hpp
# test_ConditionalVariable.hpp
# test_Thread.hpp
# test_ThreadPool.hpp
# test_Semaphore.hpp
# test_Timer.hpp
# test_Common.hpp
# test_TimerThreadMultimap.hpp
)
target_link_libraries(test CppUtils gcov)

@ -46,6 +46,9 @@ rm -f ./leak.log.core.*
# cxxtest output
rm -f $test.out
#rm $(find .. -name *.gcda)
find .. -name *.gcda -exec rm -rf {} \;
echo -e "${pre}Run tests${post}"

@ -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…
Cancel
Save