//------------------------------------------------------------------------------
// <copyright file="LqrSMACThreadSafeQueue.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
//   Declaration of ThreadSafeQueue<T>, which implements a thread-safe generic
//   FIFO queue
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#ifndef __LqrSMACThreadSafeQueue_H__
#define __LqrSMACThreadSafeQueue_H__

#include "LqrSMACLockableConcept.h"
#include "LqrSMACQueue.h"
#include <boost/thread/locks.hpp>
#include <boost/thread/recursive_mutex.hpp>

namespace LiquidRobotics {
namespace SMACFramework {

template<typename T>
class SHARED_LIB_PUBLIC ThreadSafeQueue
: public Queue<T>
, public LockableConceptMutex
{
public:

	typedef Queue<T> Base;
	typedef boost::shared_ptr<ThreadSafeQueue> Ptr;
	typedef typename Base::size_type size_type;

	ThreadSafeQueue(size_type initialCapacity = 0)
	: Base(initialCapacity)
	{
	}

	virtual size_type GetCapacity() const
	{
		Lock lock(const_cast< ThreadSafeQueue<T>& >(*this));	// I'm considering a lock not to violate const
		return Base::GetCapacity();
	}

	virtual void SetCapacity(size_type newCapacity)
	{
		Lock lock(*this);
		Base::SetCapacity(newCapacity);
	}

	virtual void Clear()
	{
		Lock lock(*this);
		Base::Clear();
	}

	virtual bool IsEmpty() const
	{
		Lock lock(const_cast< ThreadSafeQueue<T>& >(*this));	// I'm considering a lock not to violate const
		return Base::IsEmpty();
	}

	virtual bool IsFull() const
	{
		Lock lock(const_cast< ThreadSafeQueue<T>& >(*this));	// I'm considering a lock not to violate const
		return Base::IsFull();
	}

	virtual const T& Peek() const
	{
		Lock lock(const_cast< ThreadSafeQueue<T>& >(*this));	// I'm considering a lock not to violate const
		return Base::Peek();
	}

	virtual T Read()
	{
		Lock lock(*this);
		return Base::Read();
	}

	virtual bool ReadTest(T& result)
	{
		Lock lock(*this);
		return Base::ReadTest(result);
	}

	virtual size_type Size() const
	{
		Lock lock(const_cast< ThreadSafeQueue<T>& >(*this));	// I'm considering a lock not to violate const
		return Base::Size();
	}

	virtual size_type SpaceAvailable() const
	{
		Lock lock(const_cast< ThreadSafeQueue<T>& >(*this));	// I'm considering a lock not to violate const
		return Base::SpaceAvailable();
	}

	virtual bool Write(const T& newItem, bool throwOnError = true)
	{
		Lock lock(*this);
		return Base::Write(newItem, throwOnError);
	}

	virtual void WriteExpand(const T& newItem)
	{
		Lock lock(*this);
		Base::WriteExpand(newItem);
	}
};

}
}

#endif
