/*	XAutoPtr.h
 *
 *		Support templates that I can use that are not yet defined
 *	for me.
 */

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

  YAAF is Copyright 1997-1999 by William Woody.

  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.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  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.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

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


#ifndef PandaWave_XAUTOPTR
#define PandaWave_XAUTOPTR

#include <XConfig.h>

/************************************************************************/
/*																		*/
/*	Heap Management Stuff												*/
/*																		*/
/************************************************************************/

/*$	autoptr
 *
 *		Automatic pointer, like auto_ptr, except that I did it myself.
 *
 *	The rules are simple: this is a substitute for T*ptr, except that
 *	I cannot manipulate the pointer, I cannot copy to another pointer,
 *	and I must either explicitly release the pointer to the world
 *	(like TWindow objects) or return the value--both cause this pointer
 *	to become NULL.
 */

template <class T> class autoptr {
	public:
		/*$	autoptr::autoptr
		 *
		 *		Construction Class. This creates an automatic pointer
		 *	on the stack. This can be initialized with an object or
		 *	without.
		 */
		
		autoptr(T *ptr = 0)			{ v = ptr; }
		
		/*$	autoptr::~autoptr
		 *
		 *		Destruction class. If this still points to an object,
		 *	this calls the object's destructor. This makes this an
		 *	exception safe mechanism for storing temporary pointers.
		 */
		
		~autoptr()					{ if (v) delete v; }
		
		/*$	autoptr::operator =
		 *
		 *		Initializes this with a pointer.
		 */
		
		const autoptr& operator = (T* ptr) { v = ptr; return *this; }
		
		/*
		 *	Access to pointer
		 */
		
		/*
		 *	!!!	If the compiler barfs on the next line, it's because
		 *	!!! you declared an autoptr<> with a fundamental type, such
		 *	!!! as an int, a double, or a float.
		 */
		
		/*$	autoptr::operator ->
		 *
		 *		This emulates the normal -> operator to allow access
		 *	to fields in a class or structure.
		 */
		
		T* operator ->()			{ return v; }	// ERROR ^^^
		
		/*$	autoptr::operator *
		 *
		 *		This emulates the normal * operator to permit access
		 *	to the contents of a class or structure.
		 */
		
		T& operator *()				{ return *v; }
		
		/*$	autoptr::operator !
		 *
		 *		This returns true if the pointer is NULL.
		 */
		
		int operator !()			{ return (v == 0); }

		/*$	autoptr::operator ==
		 *
		 *		This returns true if two pointers are equal.
		 */
		
		int operator == (const T* a) { return (v == a); }

		/*$	autoptr::operator !=
		 *
		 *		This returns true if two pointers are not equal.
		 */

		int operator != (const T* a) { return (v != a); }

		/*$	autoptr::operator ==
		 *
		 *		This returns true if two pointers are equal.
		 */

		int operator == (const autoptr& ptr) { return (v == ptr.v); }

		/*$	autoptr::operator !=
		 *
		 *		This returns true if two pointers are not equal.
		 */

		int operator != (const autoptr& ptr) { return (v != ptr.v); }
		
		/*
		 *	Random things I can do to this pointer
		 */
		
		/*$	autoptr::Detach
		 *
		 *		Detaches this pointer. This zeros out this pointer, in
		 *	order to prevent the pointer from being returned.
		 */
		
		void Detach()				{ v = 0; }

		/*$	autoptr::Return
		 *
		 *		Returns this pointer and detaches it. This is used
		 *	when you wish to return the pointer or to put it into
		 *	another structure, detaching this.
		 */
		
		T*   Return()				{ T *a; a = v; v = 0; return a; }

		/*$	autoptr::Ptr
		 *
		 *		Returns this pointer without detaching. This is used
		 *	when you need access to the object.
		 */
		
		T*   Ptr()					{ return v; }
		
	private:
		const autoptr& operator = (const autoptr& ptr) { return ptr; } // prohibit
		
		T *v;
};

#endif // PandaWave_XAUTOPTR
