2 // This file is part of the aMule Project.
4 // Copyright (c) 2006-2011 Mikkel Schubert ( xaignar@users.sourceforge.net )
5 // Copyright (c) 2006-2011 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
29 #include "OtherFunctions.h" // Needed for DeleteContents()
32 * CScopedPtr is a simple smart pointer.
34 * This class is a replacement for std::auto_ptr, with simpler
35 * copying schematics, in that it doesn't allow copying or
36 * assignment, compared to auto_ptr, which allows only one
37 * instance to own a pointer (swapping at assignment).
39 template <typename TYPE
>
43 /** Constructor. Note that CScopedPtr takes ownership of the pointer. */
53 /** Frees the pointer owned by the instance. */
54 ~CScopedPtr() { delete m_ptr
; }
57 /** Deference operators. */
58 TYPE
& operator*() const { return *m_ptr
; }
59 TYPE
* operator->() const { return m_ptr
; }
63 /** Returns the actual pointer value. */
64 TYPE
* get() const { return m_ptr
; }
66 /** Sets the actual pointer to a different value. The old pointer is freed. */
67 void reset(TYPE
* ptr
= 0) { delete m_ptr
; m_ptr
= ptr
; }
69 /** Returns the actual pointer. The scoped-ptr will thereafter contain NULL. */
79 //! A scoped pointer is neither copyable, nor assignable.
80 CScopedPtr(const CScopedPtr
<TYPE
>&);
81 CScopedPtr
<TYPE
>& operator=(const CScopedPtr
<TYPE
>&);
89 * Similar to CScopedPtr, except that an array is expected.
93 template <typename TYPE
>
97 /** Constructor. Note that CScopedArray takes ownership of the array. */
98 CScopedArray(TYPE
* ptr
)
102 /** Constructor, allocating nr elements. */
103 CScopedArray(size_t nr
) { m_ptr
= new TYPE
[nr
]; }
105 /** Frees the array owned by this instance. */
106 ~CScopedArray() { delete[] m_ptr
; }
110 TYPE
& operator[](unsigned i
) const { return m_ptr
[i
]; }
113 /** @see CScopedPtr::get */
114 TYPE
* get() const { return m_ptr
; }
116 /** @see CScopedPtr::reset */
117 void reset(TYPE
* ptr
= 0) { delete[] m_ptr
; m_ptr
= ptr
; }
119 /** free the existing array and allocate a new one with nr elements */
120 void reset(size_t nr
) { delete[] m_ptr
; m_ptr
= new TYPE
[nr
]; }
122 /** @see CScopedPtr::release */
133 //! A scoped array is neither copyable, nor assignable.
134 CScopedArray(const CScopedArray
<TYPE
>&);
135 CScopedArray
<TYPE
>& operator=(const CScopedArray
<TYPE
>&);
143 * Similar to CScopedPtr, except that a STL container of pointers is expected
144 * which has to be freed with DeleteContents.
148 template <typename STL_CONTAINER
>
149 class CScopedContainer
152 /** Constructor. Note that CScopedContainer takes ownership of the array. */
153 CScopedContainer(STL_CONTAINER
* ptr
)
159 m_ptr
= new STL_CONTAINER
;
165 DeleteContents(*m_ptr
);
171 /** Deference operators. */
172 STL_CONTAINER
& operator*() const { return *m_ptr
; }
176 /** Returns the actual pointer value. */
177 STL_CONTAINER
* get() const { return m_ptr
; }
181 //! A scoped container is neither copyable, nor assignable.
182 CScopedContainer(const CScopedContainer
<STL_CONTAINER
>&);
183 CScopedContainer
<STL_CONTAINER
>& operator=(const CScopedContainer
<STL_CONTAINER
>&);
186 STL_CONTAINER
* m_ptr
;
189 #endif // SCOPEDPTR_H
190 // File_checked_for_headers