#ifndef _T_JPtrStack
#define _T_JPtrStack

/******************************************************************************
 JPtrStack.tmpl

	This class provides extra functions that are useful when storing
	a stack of pointers.

	BASE CLASS = public JStack<S,T>

	Copyright © 1996-97 John Lindal. All rights reserved.

 ******************************************************************************/

#include <JPtrStack.h>

/******************************************************************************
 Constructor

 ******************************************************************************/

template <class S, class T>
JPtrStack<S,T>::JPtrStack()
	:
	JStack<S,T>()
{
}

/******************************************************************************
 ClearDelete

	Delete all the elements and remove them from the stack.

 ******************************************************************************/

template <class S, class T>
void
JPtrStack<S,T>::ClearDelete()
{
	S* list           = GetElements();
	const JSize count = list->GetElementCount();
	for (JIndex i=1; i<=count; i++)
		{
		T elementPtr = list->GetElement(i);
		delete elementPtr;
		}

	Clear();
}

/******************************************************************************
 UnwindDelete

	Delete the top few elements and remove them from the stack.

 ******************************************************************************/

template <class S, class T>
void
JPtrStack<S,T>::UnwindDelete
	(
	const JSize numToUnwind
	)
{
	S* list           = GetElements();
	const JSize count = list->GetElementCount();
	for (JIndex i=1; i<=numToUnwind && i<=count; i++)
		{
		T elementPtr = list->GetElement(count -i+1);
		delete elementPtr;
		}

	Unwind(numToUnwind);
}

#endif

// Instantiate the template for the specified type.

#if defined JTemplateType && ! defined JOnlyWantTemplateDefinition
	#define JTemplateName JPtrStack
	#include <instantiate_template.h>
	#undef JTemplateName
#endif
