Upstream tarball 20080512
[amule.git] / src / ScopedPtr.h
blobd0a02ad54860fd39fe6a6112f4a679952c232ed7
1 //
2 // This file is part of the aMule Project.
3 //
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 )
6 //
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
9 // respective authors.
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.
20 //
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
26 #ifndef SCOPEDPTR_H
27 #define SCOPEDPTR_H
30 /**
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>
39 class CScopedPtr
41 public:
42 /** Constructor. Note that CScopedPtr takes ownership of the pointer. */
43 CScopedPtr(TYPE* ptr);
45 /** Frees the pointer owned by the instance. */
46 ~CScopedPtr();
49 //@{
50 /** Deference operators. */
51 TYPE& operator*() const;
52 TYPE* operator->() const;
53 //@}
56 /** Returns the actual pointer value. */
57 TYPE* get() const;
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. */
63 TYPE* release();
65 private:
66 //@{
67 //! A scoped pointer is neither copyable, nor assignable.
68 CScopedPtr(const CScopedPtr<TYPE>&);
69 CScopedPtr<TYPE>& operator=(const CScopedPtr<TYPE>&);
70 //@}
72 TYPE* m_ptr;
76 /**
77 * Similar to CScopedPtr, except that an array is expected.
79 * @see CScopedPtr
81 template <typename TYPE>
82 class CScopedArray
84 public:
85 /** Constructor. Note that CScopedArray takes ownership of the array. */
86 CScopedArray(TYPE* ptr);
88 /** Frees the array owned by this instance. */
89 ~CScopedArray();
92 /** Accessor. */
93 TYPE& operator[](unsigned i) const;
96 /** @see CScopedPtr::get */
97 TYPE* get() const;
99 /** @see CScopedPtr::reset */
100 void reset(TYPE* ptr = 0);
102 /** @see CScopedPtr::release */
103 TYPE* release();
105 private:
106 //@{
107 //! A scoped array is neither copyable, nor assignable.
108 CScopedArray(const CScopedArray<TYPE>&);
109 CScopedArray<TYPE>& operator=(const CScopedArray<TYPE>&);
110 //@}
112 TYPE* m_ptr;
118 ////////////////////////////////////////////////////////////
119 // Implementations
121 template <typename TYPE>
122 CScopedPtr<TYPE>::CScopedPtr(TYPE* ptr)
123 : m_ptr(ptr)
128 template <typename TYPE>
129 CScopedPtr<TYPE>::~CScopedPtr()
131 delete m_ptr;
135 template <typename TYPE>
136 TYPE& CScopedPtr<TYPE>::operator*() const
138 return *m_ptr;
142 template <typename TYPE>
143 TYPE* CScopedPtr<TYPE>::operator->() const
145 return m_ptr;
149 template <typename TYPE>
150 TYPE* CScopedPtr<TYPE>::get() const
152 return m_ptr;
156 template <typename TYPE>
157 void CScopedPtr<TYPE>::reset(TYPE* ptr)
159 delete m_ptr;
160 m_ptr = ptr;
164 template <typename TYPE>
165 TYPE* CScopedPtr<TYPE>::release()
167 TYPE* ptr = m_ptr;
168 m_ptr = 0;
169 return ptr;
174 template <typename TYPE>
175 CScopedArray<TYPE>::CScopedArray(TYPE* ptr)
176 : m_ptr(ptr)
181 template <typename TYPE>
182 CScopedArray<TYPE>::~CScopedArray()
184 delete[] m_ptr;
188 template <typename TYPE>
189 TYPE& CScopedArray<TYPE>::operator[](unsigned i) const
191 return m_ptr[i];
195 template <typename TYPE>
196 TYPE* CScopedArray<TYPE>::get() const
198 return m_ptr;
202 template <typename TYPE>
203 void CScopedArray<TYPE>::reset(TYPE* ptr)
205 delete[] m_ptr;
206 m_ptr = ptr;
210 template <typename TYPE>
211 TYPE* CScopedArray<TYPE>::release()
213 TYPE* ptr = m_ptr;
214 m_ptr = 0;
215 return ptr;
219 #endif // SCOPEDPTR_H
220 // File_checked_for_headers