SmartPointer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011, SRI International (R)
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #ifndef __OpenKarto_SmartPointer_h__
21 #define __OpenKarto_SmartPointer_h__
22 
23 #include <OpenKarto/Referenced.h>
24 
25 namespace karto
26 {
27 
29 
30 
34 
38  template<class T>
40  {
41  public:
46  : m_pPointer(NULL)
47  {
48  }
49 
53  SmartPointer(const SmartPointer& rOther)
54  : m_pPointer(rOther.m_pPointer)
55  {
56  if (m_pPointer != NULL)
57  {
58  m_pPointer->Reference();
59  }
60  }
61 
65  template<class Other> SmartPointer(const SmartPointer<Other>& rOther)
66  : m_pPointer(rOther.m_pPointer)
67  {
68  if (m_pPointer != NULL)
69  {
70  m_pPointer->Reference();
71  }
72  }
73 
77  SmartPointer(T* pPointer)
78  : m_pPointer(pPointer)
79  {
80  if (m_pPointer != NULL)
81  {
82  m_pPointer->Reference();
83  }
84  }
85 
89  virtual ~SmartPointer()
90  {
91  Release();
92  }
93 
94  public:
98  inline void Release()
99  {
100  if (m_pPointer)
101  {
102  m_pPointer->Unreference();
103  }
104 
105  m_pPointer = NULL;
106  }
107 
112  T* Get() const
113  {
114  return m_pPointer;
115  }
116 
121  kt_bool IsValid() const
122  {
123  return m_pPointer != NULL;
124  }
125 
126  public:
131  operator T*() const
132  {
133  return m_pPointer;
134  }
135 
140  T& operator*() const
141  {
142  return *m_pPointer;
143  }
144 
149  T* operator->() const
150  {
151  return m_pPointer;
152  }
153 
157  inline SmartPointer& operator=(const SmartPointer& rOther)
158  {
159  if (m_pPointer==rOther.m_pPointer)
160  {
161  return *this;
162  }
163 
164  T* pTempPointer = m_pPointer;
165  m_pPointer = rOther.m_pPointer;
166 
167  if (m_pPointer)
168  {
169  m_pPointer->Reference();
170  }
171 
172  if (pTempPointer)
173  {
174  pTempPointer->Unreference();
175  }
176 
177  return *this;
178  }
179 
183  inline SmartPointer& operator=(T* pPointer)
184  {
185  if (m_pPointer == pPointer)
186  {
187  return *this;
188  }
189 
190  T* pTempPointer = m_pPointer;
191  m_pPointer = pPointer;
192 
193  if (m_pPointer)
194  {
195  m_pPointer->Reference();
196  }
197 
198  if (pTempPointer)
199  {
200  pTempPointer->Unreference();
201  }
202 
203  return *this;
204  }
205 
209  template<class Other> SmartPointer& operator=(const SmartPointer<Other>& rOther)
210  {
211  if (m_pPointer == rOther.m_pPointer)
212  {
213  return *this;
214  }
215 
216  T* pTempPointer = m_pPointer;
217  m_pPointer = rOther.m_pPointer;
218 
219  if (m_pPointer)
220  {
221  m_pPointer->Reference();
222  }
223 
224  if (pTempPointer)
225  {
226  pTempPointer->Unreference();
227  }
228 
229  return *this;
230  }
231 
232  private:
234 
235  template<class Other> friend class SmartPointer;
236  };
237 
239 
240 }
241 
242 #endif // __OpenKarto_SmartPointer_h__
bool kt_bool
Definition: Types.h:145
SmartPointer & operator=(T *pPointer)
Definition: SmartPointer.h:183
virtual ~SmartPointer()
Definition: SmartPointer.h:89
T * Get() const
Definition: SmartPointer.h:112
SmartPointer(T *pPointer)
Definition: SmartPointer.h:77
SmartPointer(const SmartPointer &rOther)
Definition: SmartPointer.h:53
T * operator->() const
Definition: SmartPointer.h:149
kt_bool IsValid() const
Definition: SmartPointer.h:121
SmartPointer & operator=(const SmartPointer< Other > &rOther)
Definition: SmartPointer.h:209
T & operator*() const
Definition: SmartPointer.h:140
SmartPointer(const SmartPointer< Other > &rOther)
Definition: SmartPointer.h:65
Definition: Any.cpp:20
SmartPointer & operator=(const SmartPointer &rOther)
Definition: SmartPointer.h:157


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Thu Jun 6 2019 19:20:24