/*
	Trig Wars.
	Copyright (c) 2008, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef POOLABLE_H
#define POOLABLE_H

//=====================================================================================================================
//=====================================================================================================================

namespace Game
{
	//=================================================================================================================
	//=================================================================================================================
	
	template<typename ConcreteType> class Pool;

	//=================================================================================================================
	/// @brief		This is a base class for any object that can be pooled. It contains internal pointers used by
	///				the pool the object is contained in.
	/// @remarks	This object cannot be mixed and matched between different pools. Doing so will cause undefined 
	///				behaviour.
	//=================================================================================================================

	class Poolable
	{
		public:
		
			//=========================================================================================================
			// Functions
			//=========================================================================================================
						
				/// Default constructor: initialises poolable fields in debug mode. Values are assumed in release.
				
				#ifdef DEBUG
				
					Poolable()
					{
						m_poolable_parent		= 0;
						m_poolable_in_free_list	= true;
						m_poolable_next			= 0;
						m_poolable_prev			= 0;						
					}

					/// DEBUG ONLY: Tells if the poolable object has been allocated from the pool
					
					inline bool IsAlloced(){ return m_poolable_in_free_list == false; }
					
					/// DEBUG ONLY: Tells if the poolable object has been deallocated and is free to use by it's pool
					
					inline bool IsDealloced(){ return m_poolable_in_free_list; }
					
				#endif						
				
				//=====================================================================================================
				/// @brief		Destructor for poolable object. Does nothing in release. Validates that the object has 
				///				been removed from the pool in debug.
				///	@pre		The object must not be in a pool anymore
				//=====================================================================================================
				
				virtual ~Poolable()
				{
					ENSURE( m_poolable_parent	== 0 , "The object must not be in a pool anymore" );
					ENSURE( m_poolable_next		== 0 , "The object must not be in a pool anymore" );
					ENSURE( m_poolable_prev		== 0 , "The object must not be in a pool anymore" );
				}				
		
			//=========================================================================================================
			// Friends
			//=========================================================================================================
			
				/// Pool is the only object that should have access to the members of this class
				
				template<typename ConcreteType> friend class Pool;
	
		private:
			
			//=========================================================================================================
			// Variables
			//=========================================================================================================
				
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// Next item the linked list of poolable items contained in the parent pool. 
				/// This linked list pointer may either point to the list of free poolable items 
				/// or used poolable items.
			    //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
			    
				Poolable * m_poolable_next;
			    
			    //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// Previous item the linked list of poolable items contained in the parent pool. 
				/// This linked list may either be the list of free poolable items or used poolable items.
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
			    
				Poolable * m_poolable_prev;
				
				#ifdef DEBUG
				
					/// This variable is used in debug mode to make sure the object is not mixed across pools
					
					Pool<Poolable> * m_poolable_parent;
					
					/// This variable is used in debug mode to hold what list (free or used) the object is in
					
					bool m_poolable_in_free_list;
					
				#endif
	};
};

#endif 
