williamr@2
|
1 |
/*
|
williamr@2
|
2 |
* Copyright (c) 1997-1999
|
williamr@2
|
3 |
* Silicon Graphics Computer Systems, Inc.
|
williamr@2
|
4 |
*
|
williamr@2
|
5 |
* Copyright (c) 1999
|
williamr@2
|
6 |
* Boris Fomitchev
|
williamr@2
|
7 |
*
|
williamr@2
|
8 |
* This material is provided "as is", with absolutely no warranty expressed
|
williamr@2
|
9 |
* or implied. Any use is at your own risk.
|
williamr@2
|
10 |
*
|
williamr@2
|
11 |
* Permission to use or copy this software for any purpose is hereby granted
|
williamr@2
|
12 |
* without fee, provided the above notices are retained on all copies.
|
williamr@2
|
13 |
* Permission to modify the code and to distribute modified code is granted,
|
williamr@2
|
14 |
* provided the above notices are retained, and a notice that the code was
|
williamr@2
|
15 |
* modified is included with the above copyright notice.
|
williamr@2
|
16 |
*
|
williamr@2
|
17 |
*/
|
williamr@2
|
18 |
|
williamr@2
|
19 |
#ifndef _STLP_AUTO_PTR_H
|
williamr@2
|
20 |
# define _STLP_AUTO_PTR_H
|
williamr@2
|
21 |
|
williamr@2
|
22 |
_STLP_BEGIN_NAMESPACE
|
williamr@2
|
23 |
// implementation primitive
|
williamr@2
|
24 |
class __ptr_base {
|
williamr@2
|
25 |
public:
|
williamr@2
|
26 |
void* _M_p;
|
williamr@4
|
27 |
void __set(const void* pp) { _M_p = __CONST_CAST(void*,pp); }
|
williamr@4
|
28 |
void __set(void* pp) { _M_p = pp; }
|
williamr@2
|
29 |
};
|
williamr@2
|
30 |
|
williamr@4
|
31 |
template <class _Tp>
|
williamr@4
|
32 |
class auto_ptr_ref {
|
williamr@2
|
33 |
public:
|
williamr@2
|
34 |
__ptr_base& _M_r;
|
williamr@2
|
35 |
_Tp* const _M_p;
|
williamr@2
|
36 |
|
williamr@2
|
37 |
auto_ptr_ref(__ptr_base& __r, _Tp* __p) : _M_r(__r), _M_p(__p) { }
|
williamr@2
|
38 |
|
williamr@2
|
39 |
_Tp* release() const { _M_r.__set((void*)0); return _M_p; }
|
williamr@2
|
40 |
|
williamr@4
|
41 |
private:
|
williamr@4
|
42 |
//explicitely defined as private to avoid warnings:
|
williamr@4
|
43 |
typedef auto_ptr_ref<_Tp> _Self;
|
williamr@4
|
44 |
_Self& operator = (_Self const&);
|
williamr@2
|
45 |
};
|
williamr@2
|
46 |
|
williamr@4
|
47 |
template<class _Tp>
|
williamr@4
|
48 |
class auto_ptr : public __ptr_base {
|
williamr@2
|
49 |
public:
|
williamr@2
|
50 |
typedef _Tp element_type;
|
williamr@4
|
51 |
typedef auto_ptr<_Tp> _Self;
|
williamr@2
|
52 |
|
williamr@4
|
53 |
_Tp* release() _STLP_NOTHROW {
|
williamr@2
|
54 |
_Tp* __px = this->get();
|
williamr@2
|
55 |
this->_M_p = 0;
|
williamr@2
|
56 |
return __px;
|
williamr@2
|
57 |
}
|
williamr@2
|
58 |
|
williamr@4
|
59 |
void reset(_Tp* __px = 0) _STLP_NOTHROW {
|
williamr@2
|
60 |
_Tp* __pt = this->get();
|
williamr@2
|
61 |
if (__px != __pt)
|
williamr@2
|
62 |
delete __pt;
|
williamr@2
|
63 |
this->__set(__px);
|
williamr@2
|
64 |
}
|
williamr@2
|
65 |
|
williamr@4
|
66 |
_Tp* get() const _STLP_NOTHROW
|
williamr@4
|
67 |
{ return __REINTERPRET_CAST(_Tp*,__CONST_CAST(void*,_M_p)); }
|
williamr@2
|
68 |
|
williamr@4
|
69 |
#if !defined (_STLP_NO_ARROW_OPERATOR)
|
williamr@4
|
70 |
_Tp* operator->() const _STLP_NOTHROW {
|
williamr@2
|
71 |
_STLP_VERBOSE_ASSERT(get()!=0, _StlMsg_AUTO_PTR_NULL)
|
williamr@2
|
72 |
return get();
|
williamr@2
|
73 |
}
|
williamr@4
|
74 |
#endif
|
williamr@4
|
75 |
_Tp& operator*() const _STLP_NOTHROW {
|
williamr@4
|
76 |
_STLP_VERBOSE_ASSERT(get()!= 0, _StlMsg_AUTO_PTR_NULL)
|
williamr@2
|
77 |
return *get();
|
williamr@2
|
78 |
}
|
williamr@2
|
79 |
|
williamr@4
|
80 |
explicit auto_ptr(_Tp* __px = 0) _STLP_NOTHROW { this->__set(__px); }
|
williamr@2
|
81 |
|
williamr@2
|
82 |
#if defined (_STLP_MEMBER_TEMPLATES)
|
williamr@4
|
83 |
# if !defined (_STLP_NO_TEMPLATE_CONVERSIONS)
|
williamr@4
|
84 |
template<class _Tp1> auto_ptr(auto_ptr<_Tp1>& __r) _STLP_NOTHROW {
|
williamr@2
|
85 |
_Tp* __conversionCheck = __r.release();
|
williamr@2
|
86 |
this->__set(__conversionCheck);
|
williamr@2
|
87 |
}
|
williamr@4
|
88 |
# endif
|
williamr@4
|
89 |
template<class _Tp1> auto_ptr<_Tp>& operator=(auto_ptr<_Tp1>& __r) _STLP_NOTHROW {
|
williamr@2
|
90 |
_Tp* __conversionCheck = __r.release();
|
williamr@2
|
91 |
reset(__conversionCheck);
|
williamr@2
|
92 |
return *this;
|
williamr@2
|
93 |
}
|
williamr@4
|
94 |
#endif
|
williamr@2
|
95 |
|
williamr@4
|
96 |
auto_ptr(_Self& __r) _STLP_NOTHROW { this->__set(__r.release()); }
|
williamr@2
|
97 |
|
williamr@4
|
98 |
_Self& operator=(_Self& __r) _STLP_NOTHROW {
|
williamr@2
|
99 |
reset(__r.release());
|
williamr@2
|
100 |
return *this;
|
williamr@2
|
101 |
}
|
williamr@2
|
102 |
|
williamr@4
|
103 |
~auto_ptr() _STLP_NOTHROW { /* boris : reset(0) might be better */ delete this->get(); }
|
williamr@2
|
104 |
|
williamr@4
|
105 |
auto_ptr(auto_ptr_ref<_Tp> __r) _STLP_NOTHROW
|
williamr@4
|
106 |
{ this->__set(__r.release()); }
|
williamr@2
|
107 |
|
williamr@4
|
108 |
_Self& operator=(auto_ptr_ref<_Tp> __r) _STLP_NOTHROW {
|
williamr@2
|
109 |
reset(__r.release());
|
williamr@2
|
110 |
return *this;
|
williamr@2
|
111 |
}
|
williamr@2
|
112 |
|
williamr@4
|
113 |
#if defined(_STLP_MEMBER_TEMPLATES) && !defined(_STLP_NO_TEMPLATE_CONVERSIONS)
|
williamr@4
|
114 |
template<class _Tp1> operator auto_ptr_ref<_Tp1>() _STLP_NOTHROW
|
williamr@4
|
115 |
{ return auto_ptr_ref<_Tp1>(*this, this->get()); }
|
williamr@4
|
116 |
template<class _Tp1> operator auto_ptr<_Tp1>() _STLP_NOTHROW
|
williamr@4
|
117 |
{ return auto_ptr<_Tp1>(release()); }
|
williamr@4
|
118 |
#else
|
williamr@4
|
119 |
operator auto_ptr_ref<_Tp>() _STLP_NOTHROW
|
williamr@2
|
120 |
{ return auto_ptr_ref<_Tp>(*this, this->get()); }
|
williamr@4
|
121 |
#endif
|
williamr@2
|
122 |
};
|
williamr@2
|
123 |
_STLP_END_NAMESPACE
|
williamr@2
|
124 |
|
williamr@2
|
125 |
#endif /* _STLP_AUTO_PTR_H */
|
williamr@2
|
126 |
|
williamr@2
|
127 |
// Local Variables:
|
williamr@2
|
128 |
// mode:C++
|
williamr@2
|
129 |
// End:
|