Merge branch 'master' of gitorious.org:cpp_utils/cpp_utils

master
Denes Matetelki 13 years ago
commit 3920051e6f

@ -0,0 +1,69 @@
#ifndef MULTITON_H
#define MULTITON_H
#include <map>
// http://stackoverflow.com/questions/2346091/c-templated-class-implementation-of-the-multiton-pattern
/// @note Not thread-safe! Use some concurrent map!
template <typename Key, typename T> class Multiton
{
public:
static T& getRef( const Key& key )
{
return *getPtr(key);
}
static T* getPtr( const Key& key )
{
typename std::map<Key, T*>::const_iterator it = m_instances.find(key);
if ( it != m_instances.end() ) {
return (T*)(it->second);
}
T* instance = new T;
m_instances[key] = instance;
return instance;
}
static bool remove( const Key& key )
{
typename std::map<Key, T*>::const_iterator it = m_instances.find(key);
if ( it == m_instances.end() ) {
return false;
}
delete (*it).second;
m_instances.remove(it);
return true;
}
static void destroy()
{
typename std::map<Key, T*>::const_iterator it;
for ( it = m_instances.begin(); it != m_instances.end(); ++it ) {
delete (*it).second;
}
m_instances.clear();
}
protected:
Multiton() {}
virtual ~Multiton() {}
private:
Multiton(const Multiton&) {}
Multiton& operator= (const Multiton&) { return *this; }
static std::map<Key, T*> m_instances;
};
template <typename Key, typename T> std::map<Key, T*> Multiton<Key, T>::m_instances;
#endif // MULTITON_H

@ -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,41 @@
#ifndef SINGLETON_CALL_ONCE_HPP
#define SINGLETON_CALL_ONCE_HPP
#include <mutex>
template<typename T>
class Singleton_call_once
{
protected:
Singleton_call_once() {};
virtual ~Singleton_call_once() {};
private:
Singleton_call_once( const Singleton_call_once& );
Singleton_call_once& operator=( const Singleton_call_once& );
public:
static T* getInstance()
{
std::call_once(m_flag, &Singleton_call_once::do_init);
return m_instance;
}
private:
static void do_init()
{
m_instance = new T();
}
static T* m_instance;
static std::once_flag m_flag;
};
template<class T> T* Singleton_call_once<T>::m_instance = 0;
template<class T> std::once_flag Singleton_call_once<T>::m_flag;
#endif // SINGLETON_CALL_ONCE_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,18 @@ 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_Singelton_call_once.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,38 @@
#include <cxxtest/TestSuite.h>
#include "Common.hpp"
#include "Fixture.hpp"
#include "Multiton.hpp"
class TestMultitonSuite : public CxxTest::TestSuite
{
private:
class Dummy
{
public:
void sayHi() { LOG( Logger::FINEST, "Hi there!"); }
};
class DummyMultiton : public Multiton<std::string, Dummy>
{
};
public:
void testBasic( void )
{
TEST_HEADER;
DummyMultiton::getRef("foobar").sayHi();
DummyMultiton::getPtr("foobar")->sayHi();
DummyMultiton::destroy();
}
};

@ -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_call_once.hpp"
class TestSingletonCallOnceSuite : public CxxTest::TestSuite
{
private:
class BasicSingleton : public Singleton_call_once<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