/*
 * Copyright (c) 1997-1999
 * Silicon Graphics Computer Systems, Inc.
 *
 * Copyright (c) 1999 
 * Boris Fomitchev
 *
 * This material is provided "as is", with absolutely no warranty expressed
 * or implied. Any use is at your own risk.
 *
 * Permission to use or copy this software for any purpose is hereby granted 
 * without fee, provided the above notices are retained on all copies.
 * Permission to modify the code and to distribute modified code is granted,
 * provided the above notices are retained, and a notice that the code was
 * modified is included with the above copyright notice.
 *
 */

#ifndef __SGI_STL_MEMORY
# define __SGI_STL_MEMORY

# ifndef __STL_OUTERMOST_HEADER_ID
#  define __STL_OUTERMOST_HEADER_ID 0x46
#  include <stl/_prolog.h>
# endif

# ifndef __SGI_STL_INTERNAL_ALLOC_H
#  include <stl/_alloc.h>
# endif

# ifndef __SGI_STL_INTERNAL_TEMPBUF_H
#  include <stl/_tempbuf.h>
# endif

# ifndef __SGI_STL_INTERNAL_RAW_STORAGE_ITER_H
#  include <stl/_raw_storage_iter.h>
# endif

# if defined (__STL_IMPORT_VENDOR_STD)

#  if defined (__MSL__)
#   include __STL_NATIVE_HEADER(limits)
#  endif

#  include __STL_NATIVE_HEADER(memory)

#  if defined (__MSL__) && (__MSL__ >= 0x2405	 && __MSL__ < 0x5201) 
/*  980401 vss  MSL 2.4  Pro 3 Release  */	
#   include <new_mem.h>
#  endif

# endif

#if defined __SUNPRO_CC
#pragma disable_warn
#endif
__STL_BEGIN_NAMESPACE
// implementation primitive
class __ptr_base {
public:
	void* _M_p;
	void  __set(const void* p) { _M_p = __CONST_CAST(void*,p); }
	void  __set(void* p) { _M_p = p; }
};

// interface common for all smart pointers
template <class _Tp>
class  __ptr_intf : protected __ptr_base {
public:
  _Tp* get() const { return __REINTERPRET_CAST(_Tp*,__CONST_CAST(void*,_M_p)); } 
# if defined (__SGI_STL_NO_ARROW_OPERATOR)
  // fbp : you would not instantiate auto_ptrs for builtins, would you ?
#  if !defined (__STL_NO_AUTO_PTR_PROXY_ARROW_OPERATOR)
#   ifdef __MVS__
#    pragma define(__arrow_op_dispatch<_Tp&, _Tp*>)
#   endif
    __arrow_op_dispatch<_Tp&, _Tp*> operator->() const {
    return __arrow_op_dispatch<_Tp&, _Tp*>(*get());
  }
#  endif
# else
  _Tp* operator->() const { return get(); }
# endif
  _Tp& operator*() const  { return *get(); }
  
  // this member is here to convert derived into base if needed
  // public for auto_ptr_handler
  void  __set(_Tp* p) { __ptr_base::__set(p); }

# if defined(__STL_MEMBER_TEMPLATES) && !defined(__STL_NO_TEMPLATE_CONVERSIONS)
  template<class _Tp1> operator __ptr_intf<_Tp1>() { return __ptr_intf<_Tp1>(get()); }
# endif
  
protected:
  __ptr_intf(_Tp* p = 0) { __ptr_base::__set(p); }
private:
	__ptr_intf<_Tp>(const __ptr_intf<_Tp>&);
};


template <class _Tp> class auto_ptr_ref {
public:
  __ptr_base& _M_r;
  _Tp* const _M_p;

  auto_ptr_ref(__ptr_base& __r, _Tp* __p) : _M_r(__r), _M_p(__p) {  }

  _Tp* release() const { _M_r.__set((void*)0); return _M_p; }

};

template<class _Tp> struct auto_ptr :  public __ptr_intf<_Tp> {
	
	typedef _Tp element_type;
	typedef auto_ptr<_Tp>           _Self;
	
	_Tp* release() {  
		_Tp* __px = this->get(); 
		this->_M_p = 0; 
		return __px; 
	}
	
	void reset(_Tp* __px=0) {
      _Tp* __pt = this->get();
      if (__px != __pt) 
        delete __pt; 
      this->__set(__px); 
    }
  
	auto_ptr() {}
	
	explicit auto_ptr(_Tp* __px) : __ptr_intf<_Tp>(__px) { }
	
#if defined (__STL_MEMBER_TEMPLATES)
# ifdef __STL_MSVC
	template<class _Tp1> auto_ptr(const auto_ptr<_Tp1>& __r)
		: __ptr_intf<_Tp>((_Tp1*)const_cast<auto_ptr<_Tp1> >(__r).release()) {}
	template<class _Tp1> auto_ptr<_Tp>& operator=(const auto_ptr<_Tp1>& __r) {
		reset((_Tp1*)const_cast<auto_ptr<_Tp1> >(__r).release());
		return *this;
	}
# else
# if !defined (__STL_NO_TEMPLATE_CONVERSIONS)
	template<class _Tp1> auto_ptr(auto_ptr<_Tp1>& __r)
		: __ptr_intf<_Tp>((_Tp1*)__r.release()) {}
# endif	
	template<class _Tp1> auto_ptr<_Tp>& operator=(auto_ptr<_Tp1>& __r) {
		reset((_Tp1*)__r.release());
		return *this;
	}
# endif
#endif /* __STL_MEMBER_TEMPLATES */
	
#ifdef __STL_MSVC
	auto_ptr(const _Self& __r) : __ptr_intf<_Tp>(const_cast<_Self&>(__r).release()) { }

	_Self& operator=(const _Self& __r)  {
		reset(const_cast<_Self&>(__r).release());
		return *this;
	}
#else	
	auto_ptr(_Self& __r) : __ptr_intf<_Tp>(__r.release()) { }

	_Self& operator=(_Self& __r)  {
		reset(__r.release());
		return *this;
	}
#endif

	~auto_ptr() { /* boris : reset(0) might be better */ delete this->get(); }
	
	auto_ptr(auto_ptr_ref<_Tp> __r) : __ptr_intf<_Tp>(__r.release()) {
	}
	
	_Self& operator=(auto_ptr_ref<_Tp> __r) {
		reset(__r.release());
		return *this;
	}

# if defined(__STL_MEMBER_TEMPLATES) && !defined(__STL_NO_TEMPLATE_CONVERSIONS)
	template<class _Tp1> operator auto_ptr_ref<_Tp1>() {
		return auto_ptr_ref<_Tp1>(*this, this->get());
	}
	template<class _Tp1> operator auto_ptr<_Tp1>() {
		return auto_ptr<_Tp1>(release());
	}
# else
	operator auto_ptr_ref<_Tp>()
		{ return auto_ptr_ref<_Tp>(*this, this->get()); }
# endif
	
};
__STL_END_NAMESPACE
#if defined __SUNPRO_CC
#pragma enable_warn
#endif

# if (__STL_OUTERMOST_HEADER_ID == 0x46)
#  include <stl/_epilog.h>
#  undef __STL_OUTERMOST_HEADER_ID
# endif

#endif /* __SGI_STL_MEMORY */

// Local Variables:
// mode:C++
// End:

