Claw  1.7.3
smart_ptr.tpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
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.
13 
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.
18 
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
22 
23  contact: julien.jorge@gamned.org
24 */
30 #include <cassert>
31 #include <cstdlib>
32 
33 /*----------------------------------------------------------------------------*/
37 template<typename T>
39  : m_ref_count(NULL), m_ptr(NULL)
40 {
41 
42 } // smart_ptr::smart_ptr()
43 
44 /*----------------------------------------------------------------------------*/
55 template<typename T>
57  : m_ref_count(NULL), m_ptr(NULL)
58 {
59  if (data)
60  {
61  m_ref_count = new unsigned int(1);
62  m_ptr = data;
63  }
64 } // smart_ptr::smart_ptr() [pointer]
65 
66 /*----------------------------------------------------------------------------*/
71 template<typename T>
73 {
74  copy( that );
75 } // smart_ptr::smart_ptr() [copy]
76 
77 /*----------------------------------------------------------------------------*/
81 template<typename T>
83 {
84  release();
85 } // smart_ptr::~smart_ptr()
86 
87 /*----------------------------------------------------------------------------*/
92 template<typename T>
95 {
96  if ( &that != this )
97  {
98  release();
99  copy( that );
100  }
101 
102  return *this;
103 } // smart_ptr::operator=()
104 
105 /*----------------------------------------------------------------------------*/
111 template<typename T>
113 {
114  return !(*this < that) && !(that < *this);
115 } // smart_ptr::operator==()
116 
117 /*----------------------------------------------------------------------------*/
123 template<typename T>
125 {
126  return (*this < that) || (that < *this);
127 } // smart_ptr::operator!=()
128 
129 /*----------------------------------------------------------------------------*/
136 template<typename T>
138 {
139  return m_ptr < that.m_ptr;
140 } // smart_ptr::operator<()
141 
142 /*----------------------------------------------------------------------------*/
148 template<typename T>
150 {
151  return !(that < *this);
152 } // smart_ptr::operator<=()
153 
154 /*----------------------------------------------------------------------------*/
160 template<typename T>
162 {
163  return that < *this;
164 } // smart_ptr::operator>()
165 
166 /*----------------------------------------------------------------------------*/
172 template<typename T>
174 {
175  return !(*this < that);
176 } // smart_ptr::operator>=()
177 
178 /*----------------------------------------------------------------------------*/
182 template<typename T>
185 {
186  return m_ptr;
187 } // smart_ptr::operator->()
188 
189 /*----------------------------------------------------------------------------*/
193 template<typename T>
196 {
197  return m_ptr;
198 } // smart_ptr::operator->()
199 
200 /*----------------------------------------------------------------------------*/
204 template<typename T>
207 {
208  return *m_ptr;
209 } // smart_ptr::operator*()
210 
211 /*----------------------------------------------------------------------------*/
215 template<typename T>
218 {
219  return *m_ptr;
220 } // smart_ptr::operator*()
221 
222 /*----------------------------------------------------------------------------*/
227 template<typename T>
228 void claw::memory::smart_ptr<T>::copy( const self_type& that )
229 {
230  assert( this != &that );
231 
232  m_ref_count = that.m_ref_count;
233  m_ptr = that.m_ptr;
234 
235  if (m_ref_count)
236  ++(*m_ref_count);
237 } // smart_ptr::copy()
238 
239 /*----------------------------------------------------------------------------*/
245 template<typename T>
247 {
248  if (m_ref_count)
249  if ( *m_ref_count )
250  {
251  --(*m_ref_count);
252 
253  if ( !(*m_ref_count) )
254  {
255  delete m_ptr;
256  delete m_ref_count;
257 
258  m_ref_count = NULL;
259  }
260 
261  m_ptr = NULL;
262  }
263 } // smart_ptr::release()