Fix the changelog wikifier script to properly handle multi-line entries
[amule.git] / src / ScopedPtr.h
blob2754e6c74cb8b7799fad81c3f12d0128c1434431
1 // -*- C++ -*-
2 // This file is part of the aMule Project.
3 //
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 )
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.
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
29 #include "OtherFunctions.h" // Needed for DeleteContents()
31 /**
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>
40 class CScopedPtr
42 public:
43 /** Constructor. Note that CScopedPtr takes ownership of the pointer. */
44 CScopedPtr(TYPE* ptr)
45 : m_ptr(ptr)
48 CScopedPtr()
50 m_ptr = new TYPE;
53 /** Frees the pointer owned by the instance. */
54 ~CScopedPtr() { delete m_ptr; }
56 //@{
57 /** Deference operators. */
58 TYPE& operator*() const { return *m_ptr; }
59 TYPE* operator->() const { return m_ptr; }
60 //@}
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. */
70 TYPE* release()
72 TYPE* ptr = m_ptr;
73 m_ptr = 0;
74 return ptr;
77 private:
78 //@{
79 //! A scoped pointer is neither copyable, nor assignable.
80 CScopedPtr(const CScopedPtr<TYPE>&);
81 CScopedPtr<TYPE>& operator=(const CScopedPtr<TYPE>&);
82 //@}
84 TYPE* m_ptr;
88 /**
89 * Similar to CScopedPtr, except that an array is expected.
91 * @see CScopedPtr
93 template <typename TYPE>
94 class CScopedArray
96 public:
97 /** Constructor. Note that CScopedArray takes ownership of the array. */
98 CScopedArray(TYPE* ptr)
99 : m_ptr(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; }
109 /** Accessor. */
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 */
123 TYPE* release()
125 TYPE* ptr = m_ptr;
126 m_ptr = 0;
127 return ptr;
131 private:
132 //@{
133 //! A scoped array is neither copyable, nor assignable.
134 CScopedArray(const CScopedArray<TYPE>&);
135 CScopedArray<TYPE>& operator=(const CScopedArray<TYPE>&);
136 //@}
138 TYPE* m_ptr;
143 * Similar to CScopedPtr, except that a STL container of pointers is expected
144 * which has to be freed with DeleteContents.
146 * @see CScopedPtr
148 template <typename STL_CONTAINER>
149 class CScopedContainer
151 public:
152 /** Constructor. Note that CScopedContainer takes ownership of the array. */
153 CScopedContainer(STL_CONTAINER* ptr)
154 : m_ptr(ptr)
157 CScopedContainer()
159 m_ptr = new STL_CONTAINER;
162 ~CScopedContainer()
164 if (m_ptr) {
165 DeleteContents(*m_ptr);
166 delete m_ptr;
170 //@{
171 /** Deference operators. */
172 STL_CONTAINER& operator*() const { return *m_ptr; }
173 //@}
176 /** Returns the actual pointer value. */
177 STL_CONTAINER* get() const { return m_ptr; }
179 private:
180 //@{
181 //! A scoped container is neither copyable, nor assignable.
182 CScopedContainer(const CScopedContainer<STL_CONTAINER>&);
183 CScopedContainer<STL_CONTAINER>& operator=(const CScopedContainer<STL_CONTAINER>&);
184 //@}
186 STL_CONTAINER* m_ptr;
189 #endif // SCOPEDPTR_H
190 // File_checked_for_headers