diff --git a/include/Singleton.hpp b/include/Singleton.hpp index 5d5b9b3..8b376af 100644 --- a/include/Singleton.hpp +++ b/include/Singleton.hpp @@ -1,9 +1,6 @@ #ifndef SINGLETON_HPP #define SINGLETON_HPP -#include "Common.hpp" -#include "Logger.hpp" - template class Singleton diff --git a/include/Singleton_DCLP.hpp b/include/Singleton_DCLP.hpp new file mode 100644 index 0000000..07ead7f --- /dev/null +++ b/include/Singleton_DCLP.hpp @@ -0,0 +1,53 @@ +#ifndef SINGLETON_DCLP_HPP +#define SINGLETON_DCLP_HPP + +#include + + +template +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 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 std::mutex Singleton_DCLP::m_lock; +template T* Singleton_DCLP::m_instance = 0; + + +#endif // SINGLETON_DCLP_HPP diff --git a/include/Singleton_meyers.hpp b/include/Singleton_meyers.hpp new file mode 100644 index 0000000..f065dbe --- /dev/null +++ b/include/Singleton_meyers.hpp @@ -0,0 +1,28 @@ +#ifndef SINGLETON_MEYERS_HPP +#define SINGLETON_MEYERS_HPP + +template +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 diff --git a/src/Logger.cpp b/src/Logger.cpp index 437e768..23b805f 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -3,6 +3,8 @@ #include //time #include "Colors.hpp" +#include "Common.hpp" + void Logger::init(std::ostream& log_stream ) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8aaa7d8..2f7f76a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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) diff --git a/test/run_test.sh b/test/run_test.sh index 06e1499..d225319 100755 --- a/test/run_test.sh +++ b/test/run_test.sh @@ -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}" diff --git a/test/test_Singelton_DCLP.hpp b/test/test_Singelton_DCLP.hpp new file mode 100644 index 0000000..d5089b3 --- /dev/null +++ b/test/test_Singelton_DCLP.hpp @@ -0,0 +1,35 @@ +#include + +#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 + { + public: + int getSeven() + { + TRACE; + return 7; + } + }; + +public: + + void testBasic( void ) + { + TEST_HEADER; + + TS_ASSERT_EQUALS( BasicSingleton::getInstance()->getSeven(), 7 ); + } + + +}; diff --git a/test/test_Singelton.hpp b/test/test_Singleton.hpp similarity index 100% rename from test/test_Singelton.hpp rename to test/test_Singleton.hpp diff --git a/test/test_Singleton_DCLP.hpp b/test/test_Singleton_DCLP.hpp new file mode 100644 index 0000000..d5089b3 --- /dev/null +++ b/test/test_Singleton_DCLP.hpp @@ -0,0 +1,35 @@ +#include + +#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 + { + public: + int getSeven() + { + TRACE; + return 7; + } + }; + +public: + + void testBasic( void ) + { + TEST_HEADER; + + TS_ASSERT_EQUALS( BasicSingleton::getInstance()->getSeven(), 7 ); + } + + +}; diff --git a/test/test_Singleton_meyers.hpp b/test/test_Singleton_meyers.hpp new file mode 100644 index 0000000..4047678 --- /dev/null +++ b/test/test_Singleton_meyers.hpp @@ -0,0 +1,35 @@ +#include + +#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 + { + public: + int getSeven() + { + TRACE; + return 7; + } + }; + +public: + + void testBasic( void ) + { + TEST_HEADER; + + TS_ASSERT_EQUALS( BasicSingleton::getInstance()->getSeven(), 7 ); + } + + +};