/*
 * 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 __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 ( __STL_REDEFINE_STD ) && defined (std) 
#    undef std
#    define __STL_RESUME_STD_FOR_MEMORY
#    define __STLPORT_NATIVE_PASS
#  endif

// fbp : should work with recursion
# if __MSL__ >= 0x5201
#  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

# if defined ( __STL_RESUME_STD_FOR_MEMORY )
#    undef __STL_RESUME_STD_FOR_MEMORY
#    define std STLPORT
#    undef __STLPORT_NATIVE_PASS
# endif

# 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 : public __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)
  __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); }
//  void  __set(const _Tp* p) const { __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); }
	__ptr_intf() {}
private:
   __ptr_intf(const __ptr_intf<_Tp>&);
};

class __auto_ptr_handler {
};

template <class _Tp>
class __default_ptr_alloc {
public:
	void deallocate(_Tp* __p, size_t) { delete __p; }
#if defined (__STL_MEMBER_TEMPLATES) /* && defined (__STL_FUNCTION_TMPL_PARTIAL_ORDER) */
	template<class _Tp1>
	__default_ptr_alloc(const __default_ptr_alloc<_Tp1>&) {}
#endif
	__default_ptr_alloc() {}
	__default_ptr_alloc(const __default_ptr_alloc<_Tp>&) {}
};

template <class _Tp, class _Alloc>
inline void _Dispose(__auto_ptr_handler, const _Tp* __p, _Alloc& __a) { __a.deallocate(__CONST_CAST(_Tp*,__p), 1UL); }

// didtinct template params are for const conversion
template <class _Tp, class _Tp1, class _Alloc>
inline void _Reset(const __auto_ptr_handler& __h, __ptr_intf<_Tp>& __ref, _Tp1* __px, _Alloc& __a) {
    if (__px != __ref.get()) _Dispose(__h, __ref.get(), __a), __ref.__set(__px); 
}

inline void _Release(const __auto_ptr_handler&, __ptr_base& __ref) {
    __ref._M_p = 0; 
}

// ref does not utilize alloc as we do assume all instances equal
// so assignment do not change alloc.
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; }

};

# ifdef __STL_LIMITED_DEFAULT_TEMPLATES
template<class _Tp> struct auto_ptr : 
	: public __ptr_intf<_Tp> , public __auto_ptr_handler , public __default_ptr_alloc<_Tp>
# else
template<class _Tp, class _Alloc = __default_ptr_alloc<_Tp> > struct auto_ptr
	: public __ptr_intf<_Tp> , public __auto_ptr_handler , public _Alloc
# endif
{	
	typedef _Tp element_type;

# ifdef __STL_LIMITED_DEFAULT_TEMPLATES
	typedef auto_ptr<_Tp>                   _Self;
# else
	typedef auto_ptr<_Tp, _Alloc>           _Self;
# endif
	
	_Tp* release() {  
		_Tp* __px = this->get(); 
		_Release(*this, *this); 
		return __px; 
	}
	
	void reset(_Tp* __px=0) { _Reset(*this, *this, __px, *this); }
	
	// no alloc param here as it is supposed to come with pointer
	auto_ptr() {}
	
	explicit auto_ptr(_Tp* __px) { this->__set(__px); }

	auto_ptr(_Tp* __px, const _Alloc& __a)  : _Alloc(__a) { }
	
#if defined (__STL_MEMBER_TEMPLATES) /* && defined (__STL_FUNCTION_TMPL_PARTIAL_ORDER) */
// the initialization here should prevent from assigning from uncompatible allocs
	template<class _Tp1, class _Alloc1> 
	//	auto_ptr<_Tp, _Alloc>(auto_ptr<_Tp1, _Alloc1>& __r) : _Alloc(__r) {
	auto_ptr(auto_ptr<_Tp1, _Alloc1>& __r) : _Alloc(__r) {
		this->__set(__STATIC_CAST(_Tp*,__r.release()));
	}
	
	template<class _Tp1, class _Alloc1> 
	auto_ptr<_Tp, _Alloc>& operator=(auto_ptr<_Tp1, _Alloc1>& __r) {
		reset(__r.release());
		return *this;
	}
#endif /* __STL_MEMBER_TEMPLATES */
	
	auto_ptr(_Self& __r) { this->__set(__r.release()); }
	
	_Self& operator=(_Self& __r)  {
		reset(__r.release());
		return *this;
	}

	~auto_ptr() { reset(0); }
	
	auto_ptr(auto_ptr_ref<_Tp> __r) {
		this->__set(__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());
	}
# ifndef _MSC_VER
	template<class _Tp1, class _Alloc1> operator auto_ptr<_Tp1, _Alloc1>() {
		return auto_ptr<_Tp1,_Alloc1>(release());
	}
# endif
# else
	operator auto_ptr_ref<_Tp>()
		{ return auto_ptr_ref<_Tp>(*this, this->get()); }
# endif
	
};

__STL_END_NAMESPACE

#endif /* __SGI_STL_MEMORY */

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

