2 CLAW - a C++ Library Absolutely Wonderful
4 CLAW is a free library without any particular aim but being useful to
7 Copyright (C) 2005-2011 Julien Jorge
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 contact: julien.jorge@gamned.org
27 * \brief Implementation of the claw::memory::smart_ptr class.
28 * \author Julien Jorge
33 /*----------------------------------------------------------------------------*/
35 * \brief Default constructor.
38 claw::memory::smart_ptr<T>::smart_ptr()
39 : m_ref_count(NULL), m_ptr(NULL)
42 } // smart_ptr::smart_ptr()
44 /*----------------------------------------------------------------------------*/
46 * \brief Constructor from a pointer.
47 * \param data Pointer on the data.
49 * \b Warning: this constructor allows expressions like
51 * <tt>int a; smart_ptr<int> p(&a);</tt>
53 * Nevertheless, you should never fo that.
56 claw::memory::smart_ptr<T>::smart_ptr( pointer data )
57 : m_ref_count(NULL), m_ptr(NULL)
61 m_ref_count = new unsigned int(1);
64 } // smart_ptr::smart_ptr() [pointer]
66 /*----------------------------------------------------------------------------*/
68 * \brief Copy constructor.
69 * \param that The smart_pointer to copy.
72 claw::memory::smart_ptr<T>::smart_ptr( const self_type& that )
75 } // smart_ptr::smart_ptr() [copy]
77 /*----------------------------------------------------------------------------*/
79 * \brief Destructor. The memory is freed only if no more smart_ptr point on it.
82 claw::memory::smart_ptr<T>::~smart_ptr()
85 } // smart_ptr::~smart_ptr()
87 /*----------------------------------------------------------------------------*/
89 * \brief Assignment operator.
90 * \param that The smart_ptr to copy.
93 typename claw::memory::smart_ptr<T>::self_type&
94 claw::memory::smart_ptr<T>::operator=( const self_type& that )
103 } // smart_ptr::operator=()
105 /*----------------------------------------------------------------------------*/
107 * \brief Equality operator.
108 * \param that The pointer to compare to.
109 * \return !(*this < that) && !(that < *this).
112 bool claw::memory::smart_ptr<T>::operator==( const self_type& that ) const
114 return !(*this < that) && !(that < *this);
115 } // smart_ptr::operator==()
117 /*----------------------------------------------------------------------------*/
119 * \brief Disequality operator.
120 * \param that The pointer to compare to.
121 * \return (*this < that) || (that < *this).
124 bool claw::memory::smart_ptr<T>::operator!=( const self_type& that ) const
126 return (*this < that) || (that < *this);
127 } // smart_ptr::operator!=()
129 /*----------------------------------------------------------------------------*/
131 * \brief "Less than" operator.
132 * \param that The pointer to compare to.
133 * \return True if the address pointed by \a this is lower than the address
134 * pointed by \a that.
137 bool claw::memory::smart_ptr<T>::operator<( const self_type& that ) const
139 return m_ptr < that.m_ptr;
140 } // smart_ptr::operator<()
142 /*----------------------------------------------------------------------------*/
144 * \brief "Less or equal" operator.
145 * \param that The pointer to compare to.
146 * \return !(that < *this).
149 bool claw::memory::smart_ptr<T>::operator<=( const self_type& that ) const
151 return !(that < *this);
152 } // smart_ptr::operator<=()
154 /*----------------------------------------------------------------------------*/
156 * \brief "Greater than" operator.
157 * \param that The pointer to compare to.
158 * \return \a that < *this.
161 bool claw::memory::smart_ptr<T>::operator>( const self_type& that ) const
164 } // smart_ptr::operator>()
166 /*----------------------------------------------------------------------------*/
168 * \brief "Greater or equal" operator.
169 * \param that The pointer to compare to.
170 * \return !(*this < that).
173 bool claw::memory::smart_ptr<T>::operator>=( const self_type& that ) const
175 return !(*this < that);
176 } // smart_ptr::operator>=()
178 /*----------------------------------------------------------------------------*/
180 * \brief Dereference operator.
183 typename claw::memory::smart_ptr<T>::pointer
184 claw::memory::smart_ptr<T>::operator->()
187 } // smart_ptr::operator->()
189 /*----------------------------------------------------------------------------*/
191 * \brief Dereference operator.
194 typename claw::memory::smart_ptr<T>::pointer
195 claw::memory::smart_ptr<T>::operator->() const
198 } // smart_ptr::operator->()
200 /*----------------------------------------------------------------------------*/
202 * \brief Dereference operator.
205 typename claw::memory::smart_ptr<T>::reference
206 claw::memory::smart_ptr<T>::operator*()
209 } // smart_ptr::operator*()
211 /*----------------------------------------------------------------------------*/
213 * \brief Dereference operator.
216 typename claw::memory::smart_ptr<T>::reference
217 claw::memory::smart_ptr<T>::operator*() const
220 } // smart_ptr::operator*()
222 /*----------------------------------------------------------------------------*/
224 * \brief Copy a smart_ptr.
225 * \param that The smart_pointer to copy.
228 void claw::memory::smart_ptr<T>::copy( const self_type& that )
230 assert( this != &that );
232 m_ref_count = that.m_ref_count;
237 } // smart_ptr::copy()
239 /*----------------------------------------------------------------------------*/
241 * \brief Release the allocated memory.
243 * The memory is release only if no more smart_ptr point on it.
246 void claw::memory::smart_ptr<T>::release()
253 if ( !(*m_ref_count) )
263 } // smart_ptr::release()