2 // This file is part of the aMule Project.
4 // Copyright (c) 2006-2008 Mikkel Schubert ( xaignar@users.sourceforge.net )
5 // Copyright (c) 2006-2008 aMule Team ( admin@amule.org / http://www.amule.org )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 * CScopedPtr is a simple smart pointer.
33 * This class is a replacement for std::auto_ptr, with simpler
34 * copying schematics, in that it doesn't allow copying or
35 * assignment, compared to auto_ptr, which allows only one
36 * instance to own a pointer (swapping at assignment).
38 template <typename TYPE
>
42 /** Constructor. Note that CScopedPtr takes ownership of the pointer. */
43 CScopedPtr(TYPE
* ptr
);
45 /** Frees the pointer owned by the instance. */
50 /** Deference operators. */
51 TYPE
& operator*() const;
52 TYPE
* operator->() const;
56 /** Returns the actual pointer value. */
59 /** Sets the actual pointer to a different value. The old pointer is freed. */
60 void reset(TYPE
* ptr
= 0);
62 /** Returns the actual pointer. The scoped-ptr will thereafter contain NULL. */
67 //! A scoped pointer is neither copyable, nor assignable.
68 CScopedPtr(const CScopedPtr
<TYPE
>&);
69 CScopedPtr
<TYPE
>& operator=(const CScopedPtr
<TYPE
>&);
77 * Similar to CScopedPtr, except that an array is expected.
81 template <typename TYPE
>
85 /** Constructor. Note that CScopedArray takes ownership of the array. */
86 CScopedArray(TYPE
* ptr
);
88 /** Frees the array owned by this instance. */
93 TYPE
& operator[](unsigned i
) const;
96 /** @see CScopedPtr::get */
99 /** @see CScopedPtr::reset */
100 void reset(TYPE
* ptr
= 0);
102 /** @see CScopedPtr::release */
107 //! A scoped array is neither copyable, nor assignable.
108 CScopedArray(const CScopedArray
<TYPE
>&);
109 CScopedArray
<TYPE
>& operator=(const CScopedArray
<TYPE
>&);
118 ////////////////////////////////////////////////////////////
121 template <typename TYPE
>
122 CScopedPtr
<TYPE
>::CScopedPtr(TYPE
* ptr
)
128 template <typename TYPE
>
129 CScopedPtr
<TYPE
>::~CScopedPtr()
135 template <typename TYPE
>
136 TYPE
& CScopedPtr
<TYPE
>::operator*() const
142 template <typename TYPE
>
143 TYPE
* CScopedPtr
<TYPE
>::operator->() const
149 template <typename TYPE
>
150 TYPE
* CScopedPtr
<TYPE
>::get() const
156 template <typename TYPE
>
157 void CScopedPtr
<TYPE
>::reset(TYPE
* ptr
)
164 template <typename TYPE
>
165 TYPE
* CScopedPtr
<TYPE
>::release()
174 template <typename TYPE
>
175 CScopedArray
<TYPE
>::CScopedArray(TYPE
* ptr
)
181 template <typename TYPE
>
182 CScopedArray
<TYPE
>::~CScopedArray()
188 template <typename TYPE
>
189 TYPE
& CScopedArray
<TYPE
>::operator[](unsigned i
) const
195 template <typename TYPE
>
196 TYPE
* CScopedArray
<TYPE
>::get() const
202 template <typename TYPE
>
203 void CScopedArray
<TYPE
>::reset(TYPE
* ptr
)
210 template <typename TYPE
>
211 TYPE
* CScopedArray
<TYPE
>::release()
219 #endif // SCOPEDPTR_H
220 // File_checked_for_headers