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
= 0)
47 /** Frees the pointer owned by the instance. */
48 ~CScopedPtr() { delete m_ptr
; }
51 /** Deference operators. */
52 TYPE
& operator*() const { return *m_ptr
; }
53 TYPE
* operator->() const { return m_ptr
; }
57 /** Returns the actual pointer value. */
58 TYPE
* get() const { return m_ptr
; }
60 /** Sets the actual pointer to a different value. The old pointer is freed. */
61 void reset(TYPE
* ptr
= 0) { delete m_ptr
; m_ptr
= ptr
; }
63 /** Returns the actual pointer. The scoped-ptr will thereafter contain NULL. */
73 //! A scoped pointer is neither copyable, nor assignable.
74 CScopedPtr(const CScopedPtr
<TYPE
>&);
75 CScopedPtr
<TYPE
>& operator=(const CScopedPtr
<TYPE
>&);
83 * Similar to CScopedPtr, except that an array is expected.
87 template <typename TYPE
>
91 /** Constructor. Note that CScopedArray takes ownership of the array. */
92 CScopedArray(TYPE
* ptr
= 0)
96 /** Constructor, allocating nr elements. */
97 CScopedArray(size_t nr
) { m_ptr
= new TYPE
[nr
]; }
99 /** Frees the array owned by this instance. */
100 ~CScopedArray() { delete[] m_ptr
; }
104 TYPE
& operator[](unsigned i
) const { return m_ptr
[i
]; }
107 /** @see CScopedPtr::get */
108 TYPE
* get() const { return m_ptr
; }
110 /** @see CScopedPtr::reset */
111 void reset(TYPE
* ptr
= 0) { delete[] m_ptr
; m_ptr
= ptr
; }
113 /** free the existing array and allocate a new one with nr elements */
114 void reset(size_t nr
) { delete[] m_ptr
; m_ptr
= new TYPE
[nr
]; }
116 /** @see CScopedPtr::release */
127 //! A scoped array is neither copyable, nor assignable.
128 CScopedArray(const CScopedArray
<TYPE
>&);
129 CScopedArray
<TYPE
>& operator=(const CScopedArray
<TYPE
>&);
135 #endif // SCOPEDPTR_H
136 // File_checked_for_headers