2 * Copyright (c) 1997-1999
3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
19 #ifndef _STLP_AUTO_PTR_H
20 # define _STLP_AUTO_PTR_H
23 // implementation primitive
27 void __set(const void* pp) { _M_p = __CONST_CAST(void*,pp); }
28 void __set(void* pp) { _M_p = pp; }
37 auto_ptr_ref(__ptr_base& __r, _Tp* __p) : _M_r(__r), _M_p(__p) { }
39 _Tp* release() const { _M_r.__set((void*)0); return _M_p; }
42 //explicitely defined as private to avoid warnings:
43 typedef auto_ptr_ref<_Tp> _Self;
44 _Self& operator = (_Self const&);
48 class auto_ptr : public __ptr_base {
50 typedef _Tp element_type;
51 typedef auto_ptr<_Tp> _Self;
53 _Tp* release() _STLP_NOTHROW {
54 _Tp* __px = this->get();
59 void reset(_Tp* __px = 0) _STLP_NOTHROW {
60 _Tp* __pt = this->get();
66 _Tp* get() const _STLP_NOTHROW
67 { return __REINTERPRET_CAST(_Tp*,__CONST_CAST(void*,_M_p)); }
69 #if !defined (_STLP_NO_ARROW_OPERATOR)
70 _Tp* operator->() const _STLP_NOTHROW {
71 _STLP_VERBOSE_ASSERT(get()!=0, _StlMsg_AUTO_PTR_NULL)
75 _Tp& operator*() const _STLP_NOTHROW {
76 _STLP_VERBOSE_ASSERT(get()!= 0, _StlMsg_AUTO_PTR_NULL)
80 explicit auto_ptr(_Tp* __px = 0) _STLP_NOTHROW { this->__set(__px); }
82 #if defined (_STLP_MEMBER_TEMPLATES)
83 # if !defined (_STLP_NO_TEMPLATE_CONVERSIONS)
84 template<class _Tp1> auto_ptr(auto_ptr<_Tp1>& __r) _STLP_NOTHROW {
85 _Tp* __conversionCheck = __r.release();
86 this->__set(__conversionCheck);
89 template<class _Tp1> auto_ptr<_Tp>& operator=(auto_ptr<_Tp1>& __r) _STLP_NOTHROW {
90 _Tp* __conversionCheck = __r.release();
91 reset(__conversionCheck);
96 auto_ptr(_Self& __r) _STLP_NOTHROW { this->__set(__r.release()); }
98 _Self& operator=(_Self& __r) _STLP_NOTHROW {
103 ~auto_ptr() _STLP_NOTHROW { /* boris : reset(0) might be better */ delete this->get(); }
105 auto_ptr(auto_ptr_ref<_Tp> __r) _STLP_NOTHROW
106 { this->__set(__r.release()); }
108 _Self& operator=(auto_ptr_ref<_Tp> __r) _STLP_NOTHROW {
109 reset(__r.release());
113 #if defined(_STLP_MEMBER_TEMPLATES) && !defined(_STLP_NO_TEMPLATE_CONVERSIONS)
114 template<class _Tp1> operator auto_ptr_ref<_Tp1>() _STLP_NOTHROW
115 { return auto_ptr_ref<_Tp1>(*this, this->get()); }
116 template<class _Tp1> operator auto_ptr<_Tp1>() _STLP_NOTHROW
117 { return auto_ptr<_Tp1>(release()); }
119 operator auto_ptr_ref<_Tp>() _STLP_NOTHROW
120 { return auto_ptr_ref<_Tp>(*this, this->get()); }
125 #endif /* _STLP_AUTO_PTR_H */