Upstream tarball 9882
[amule.git] / src / ScopedPtr.h
blob119a6653d001cde84ba5723d194633180dfd7ac0
1 // -*- C++ -*-
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 = 0)
44 : m_ptr(ptr)
47 /** Frees the pointer owned by the instance. */
48 ~CScopedPtr() { delete m_ptr; }
50 //@{
51 /** Deference operators. */
52 TYPE& operator*() const { return *m_ptr; }
53 TYPE* operator->() const { return m_ptr; }
54 //@}
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. */
64 TYPE* release()
66 TYPE* ptr = m_ptr;
67 m_ptr = 0;
68 return ptr;
71 private:
72 //@{
73 //! A scoped pointer is neither copyable, nor assignable.
74 CScopedPtr(const CScopedPtr<TYPE>&);
75 CScopedPtr<TYPE>& operator=(const CScopedPtr<TYPE>&);
76 //@}
78 TYPE* m_ptr;
82 /**
83 * Similar to CScopedPtr, except that an array is expected.
85 * @see CScopedPtr
87 template <typename TYPE>
88 class CScopedArray
90 public:
91 /** Constructor. Note that CScopedArray takes ownership of the array. */
92 CScopedArray(TYPE* ptr = 0)
93 : m_ptr(ptr)
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; }
103 /** Accessor. */
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 */
117 TYPE* release()
119 TYPE* ptr = m_ptr;
120 m_ptr = 0;
121 return ptr;
125 private:
126 //@{
127 //! A scoped array is neither copyable, nor assignable.
128 CScopedArray(const CScopedArray<TYPE>&);
129 CScopedArray<TYPE>& operator=(const CScopedArray<TYPE>&);
130 //@}
132 TYPE* m_ptr;
135 #endif // SCOPEDPTR_H
136 // File_checked_for_headers