/***************************************************************************
                Queue Class
                -----------------------------------------
    begin      : 2012/10/14
    copyright  : (C) 2010 SolutionBox Inc.
    author     : service 1 team
    email      : svc1@solbox.com
    version    : 1.0

    CopyRight(C) 2010 SolutionBox Inc. All Rights reserved.
    Redistribution and use in source and binary forms, with or with out
    modification, are not permitted in outside of SolutionBox Inc.
***************************************************************************/


#include "DataQueue.h"
#include "Data.h"

using namespace std;

#define MX_LOCK_DATA()\
do \
{\
	pthread_mutex_lock(&m_mxData);\
} while( 0 )

#define MX_UNLOCK_DATA()\
do \
{\
	pthread_mutex_unlock(&m_mxData);\
} while( 0 )
#define MX_LOCK_MAXSIZE()\
do \
{\
	pthread_mutex_lock(&m_mxMaxSize);\
} while( 0 )

#define MX_UNLOCK_MAXSIZE()\
do \
{\
	pthread_mutex_unlock(&m_mxMaxSize);\
} while( 0 )


template <class DataClass>
CQueue<DataClass>::CQueue()
//CQueue::CQueue()
	: m_nMaxSize(DEFAULT_MAX_QUEUE_SIZE)
{
	pthread_mutex_init(&m_mxData, NULL);
	pthread_mutex_init(&m_mxMaxSize, NULL);
}
template <class DataClass>
CQueue<DataClass>::~CQueue()
{
	pthread_mutex_destroy(&m_mxData);
	pthread_mutex_destroy(&m_mxMaxSize);
}

template <class DataClass>
void CQueue<DataClass>::Clear(void)
{
	//clear queue
	MX_LOCK_DATA();
	while ( !m_qData.empty() )
	{
		Pop();
	}
	MX_UNLOCK_DATA();
}

template <class DataClass>
bool CQueue<DataClass>::Push(DataClass& userData)
{
	if ( GetCurrentMaxSize() != DEFAULT_MAX_QUEUE_SIZE && GetSize() >= GetCurrentMaxSize() )
	{
		return false;
	}
	MX_LOCK_DATA();
	m_qData.push(userData);
	MX_UNLOCK_DATA();
	return true;
}
template <class DataClass>
bool CQueue<DataClass>::Pop(void)
{
	if ( GetSize() < 0 )
	{
		return false;
	}
	MX_LOCK_DATA();
	m_qData.pop();
	MX_UNLOCK_DATA();
	return true;
}

template <class DataClass>
DataClass CQueue<DataClass>::Front(void)
{
	return m_qData.front();
}
template <class DataClass>
int  CQueue<DataClass>::GetCurrentMaxSize(void)
{
	return m_nMaxSize;
}
template <class DataClass>
bool CQueue<DataClass>::SetCurrentMaxSize(int nMaxSize)
{
	if (nMaxSize < 0)
	{
		return false;
	}
	MX_LOCK_MAXSIZE();
	m_nMaxSize = nMaxSize;
	MX_UNLOCK_MAXSIZE();
	return true;
}
template <class DataClass>
int  CQueue<DataClass>::GetSize(void)
{
	return m_qData.size();
}

template class CQueue<CFileInfo>;
