Specialization of ConqurrentDeque::freeDeque and ObjectPool::release with Substitution failure is not an error (SFINAE)

master
Denes Matetelki 13 years ago
parent 8aecc59916
commit fdb6c5b7a1

@ -4,7 +4,7 @@ project (CPP_UTILS_LIB)
set(CMAKE_CXX_COMPILER "/usr/lib/colorgcc/bin/g++")
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} )
include_directories (../include)

@ -3,6 +3,7 @@
#include <deque>
#include <algorithm>
#include <type_traits>
#include "Mutex.hpp"
#include "ConditionVariable.hpp"
@ -15,11 +16,11 @@ class CancelledException {};
template <typename T>
class ConcurrentQueue
class ConcurrentDeque
{
public:
ConcurrentQueue()
ConcurrentDeque()
: m_queue()
, m_cancelled(false)
, m_mutex()
@ -28,9 +29,10 @@ public:
TRACE;
}
~ConcurrentQueue()
~ConcurrentDeque()
{
TRACE;
freeDeque();
}
@ -81,29 +83,32 @@ public:
return m_queue.empty();
}
void cancel(T a)
void cancel()
{
TRACE;
ScopedLock sl(m_mutex);
m_cancelled = true;
m_condVar.broadcast();
m_queue.clear();
freeDeque();
}
void cancel(T *a)
template<class U = T>
typename std::enable_if< std::is_pointer<U>::value >::type
freeDeque()
{
TRACE;
ScopedLock sl(m_mutex);
m_cancelled = true;
m_condVar.broadcast();
typename std::deque<T*>::iterator it;
for ( it = m_queue.begin(); it != m_queue.end(); ++it ) {
LOG( Logger::INFO, std::string("Deleting: ").append(*it).c_str() );
typename std::deque<T>::iterator it;
for ( it = m_queue.begin(); it != m_queue.end(); ++it )
delete *it;
m_queue.clear();
}
template<class U = T>
typename std::enable_if< !(std::is_pointer<U>::value) >::type
freeDeque()
{
TRACE;
m_queue.clear();
}
@ -116,8 +121,8 @@ public:
private:
ConcurrentQueue& operator=( const ConcurrentQueue& );
ConcurrentQueue( const ConcurrentQueue& );
ConcurrentDeque& operator=( const ConcurrentDeque& );
ConcurrentDeque( const ConcurrentDeque& );
std::deque<T> m_queue;
bool m_cancelled;

@ -17,7 +17,6 @@ public:
void create();
/// @note Shall this be a specialized ObjectPool::clear?
void clear();
private:

@ -1,7 +1,7 @@
#ifndef OBJECT_POOL_HPP
#define OBJECT_POOL_HPP
#include "ConcurrentQueue.hpp"
#include "ConcurrentDeque.hpp"
#include "Logger.hpp"
template <typename T>
@ -36,11 +36,14 @@ public:
return tmp;
}
void release(T object)
template<class U = T>
typename std::enable_if< std::is_pointer<U>::value >::type
release(T object)
{
TRACE;
if ( m_pool.cancelled() ) {
m_numberOfUsedObjects--;
delete object;
return;
}
@ -48,12 +51,13 @@ public:
m_numberOfUsedObjects--;
}
void release(T* object)
template<class U = T>
typename std::enable_if< !(std::is_pointer<U>::value) >::type
release(T object)
{
TRACE;
if ( m_pool.cancelled() ) {
m_numberOfUsedObjects--;
delete object;
return;
}
@ -61,21 +65,16 @@ public:
m_numberOfUsedObjects--;
}
void clear(T a)
void clear()
{
TRACE;
m_pool.cancel(a);
m_pool.cancel();
}
void clear(T* a)
{
TRACE;
m_pool.cancel(a);
}
private:
ConcurrentQueue<T> m_pool;
ConcurrentDeque<T> m_pool;
int m_numberOfUsedObjects;
};

@ -3,7 +3,7 @@
#include <vector>
#include "ConcurrentQueue.hpp"
#include "ConcurrentDeque.hpp"
#include "Task.hpp"
#include "Thread.hpp"
#include "Mutex.hpp"
@ -32,7 +32,7 @@ class ThreadPool
ThreadPool& operator=( const ThreadPool& );
std::vector<Thread*> m_threads;
ConcurrentQueue<Task*> m_tasks;
ConcurrentDeque<Task*> m_tasks;
};

@ -34,11 +34,15 @@ public:
op.add(a);
op.add(b);
TS_ASSERT_EQUALS( op.acquire(), a );
TS_ASSERT_EQUALS( op.acquire(), b );
int *tmp_a = op.acquire();
int *tmp_b = op.acquire();
TS_ASSERT_EQUALS( *tmp_a, *a );
TS_ASSERT_EQUALS( *tmp_b, *b );
delete a;
delete b;
// release will delete them
op.release(tmp_a);
op.release(tmp_b);
}
@ -100,7 +104,7 @@ public:
t1.join();
t2.join();
delete a;
// no need to delete "a", dtor of the ConqurrentDeque takes care of it
}

Loading…
Cancel
Save