//--------------------------------------------------------------------------------
// <copyright file="LqrSMACLockableConcept.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>
//   Implements methods that boost classes need to lock and unlock synchronization
//   objects; for example:
//
//		class MyClass : public LockableConcept<boost::recursive_mutex>
//		{
//		public:
//			MyClass() : LockableConcept<boost::recursive_mutex>(myMutex) {}
//		private:
//			boost::recursive_mutex myMutex;
//		}
//
//		MyClass c;
//		boost::unique_lock<MyClass> lock(c);
//
//   Or to combine the mutex and the lockable concept in one class:
//
//		class MyClass : public LockableConceptMutex
//		{ }
//
//		MyClass c;
//		MyClass::Lock lock(c);
//
// </summary>
//
// <owner>Mike Cookson</owner>
//--------------------------------------------------------------------------------

#ifndef __LQRSMACLOCKABLECONCEPT_H__
#define __LQRSMACLOCKABLECONCEPT_H__

#include <boost/shared_ptr.hpp>
//#include <boost/thread/thread_time.hpp>
#include <boost/thread.hpp>
//#include <boost/thread/locks.hpp>
//#include <boost/thread/recursive_mutex.hpp>

#define LOCKABLE_CONCEPT_TYPEDEFS() \
	typedef LiquidRobotics::SMACFramework::ILockableConcept ILockableConcept; \
	typedef LiquidRobotics::SMACFramework::ILockableConceptLock ILockableConceptLock; \
	typedef LiquidRobotics::SMACFramework::LockableConceptMutex LockableConceptMutex

namespace LiquidRobotics {
namespace SMACFramework {

class ILockableConcept
{
public:

	typedef boost::unique_lock<ILockableConcept> Lock;
	typedef boost::shared_ptr<Lock> LockPtr;

	virtual ~ILockableConcept() { }

	virtual void lock() = 0;
	virtual bool try_lock() = 0;
	virtual void unlock() = 0;
};

typedef boost::unique_lock<ILockableConcept> ILockableConceptLock;

template<typename T>
class LockableConcept : public ILockableConcept
{
public:

	LockableConcept(T& _lockObject)
	: lockObject(_lockObject)
	{
	}

	virtual void lock()
	{
		return lockObject.lock();
	}

	virtual bool try_lock()
	{
		return lockObject.try_lock();
	}

	virtual void unlock()
	{
		return lockObject.unlock();
	}

protected:

	T& lockObject;
};

class LockableConceptMutex : public LockableConcept<boost::recursive_mutex>
{
public:

	typedef boost::recursive_mutex Mutex;
	typedef boost::shared_ptr<Mutex> MutexPtr;
	typedef boost::unique_lock<Mutex> MutexLock;
	typedef boost::shared_ptr<MutexLock> MutexLockPtr;
	typedef boost::shared_ptr<LockableConceptMutex> Ptr;
	typedef boost::weak_ptr<LockableConceptMutex> WeakPtr;

	LockableConceptMutex() : LockableConcept<Mutex>(mutex)
	{
	}

protected:

	Mutex mutex;
};

}
}

#endif
