Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SharedPtrImpl.hh
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9  BIAS is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation; either version 2.1 of the License, or
12  (at your option) any later version.
13 
14  BIAS 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
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with BIAS; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 #ifndef __SharedPtrImpl_hh__
24 #define __SharedPtrImpl_hh__
25 
26 #include <Base/Debug/Exception.hh>
27 #include <Base/Common/Constraints.hh>
28 
29 namespace BIAS {
30 
31  ///////////////////////////////////////////////////////////////////////
32  // access functions
33  ///////////////////////////////////////////////////////////////////////
34 
35  /** @relates SharedPtr
36  @brief access function to the raw pointer
37  @author woelk 09/2007 */
38  template <class T>
40  { return (t.Data_)?(t.Data_->Ptr):(0); }
41 
42  /** @relates SharedPtr
43  @brief access function to the raw pointer
44  @author woelk 09/2007 */
45  template <class T>
46  T* const Get(const SharedPtr<T>& t)
47  { return (t.Data_)?(t.Data_->Ptr):(0); }
48 
49  /** @relates SharedPtr
50  @brief access function to the reference count
51  @author woelk 09/2007 */
52  template <class T>
53  unsigned RefCount(const SharedPtr<T>& t)
54  { return (t.Data_)?(t.Data_->Counter):(0u); }
55 
56  /** @relates SharedPtr
57  @brief DynamicCast function
58  @author woelk 02/2008 */
59  template <class T, class U>
61  {
62  SharedPtr<T> res;
63  return res.DynamicCast(src);
64  }
65 
66  /** @relates SharedPtr
67  @brief DynamicCast function
68  @author woelk 02/2008 */
69  template <class T, class U>
71  {
72  SharedPtr<T> res;
73  return res.ConstCast(src);
74  }
75 
76  ///////////////////////////////////////////////////////////////////////
77  // constructors
78  ///////////////////////////////////////////////////////////////////////
79 
80  template <class T>
82  : Data_(0)
83  {}
84 
85  template <class T>
87  : Data_(pt.Data_)
88  {
89  //std::cout << "copied data "<<Data_<<"\n";
90  Increment_();
91  }
92 
93  template <class T>
94  template <class U>
96  : Data_(reinterpret_cast<SharedPtrBase<T> *>(pt.Data_))
97  {
98  // check if T = const U
99  //SameButConst<U,T> t;
100  // to allow assignemnet from child to father
102 
103  //std::cout << "copied data "<<Data_<<"\n";
104  Increment_();
105  }
106 
107 
108  template <class T>
110  : Data_(0)
111  {
112  if (pt){
113  Data_ = new SharedPtrBase<T>(pt);
114  //std::cout << "allocated data "<<Data_<<"\n";
115  } else {
116  //std::cout << "called constructor with null ptr\n";
117  }
118  }
119 
120  template <class T>
122  {
123  Decrement_();
124  }
125 
126  template <class T>
128  {
129  Decrement_();
130  Data_ = sp.Data_;
131  Increment_();
132  return *this;
133  }
134 
135  ///////////////////////////////////////////////////////////////////////
136  // access functions
137  ///////////////////////////////////////////////////////////////////////
138 
139  template <class T>
141  {
142  if (!Data_){
143  BIASERR("Trying to reference a null pointer");
144  BIASABORT;
145  BEXCEPTION("Trying to reference a null pointer");
146  }
147  BIASASSERT(Data_->Ptr);
148  return *(Data_->Ptr);
149  }
150 
151  template <class T>
153  {
154  if (!Data_){
155  BIASERR("Trying to access a null pointer");
156  BIASABORT;
157  BEXCEPTION("Trying to access a null pointer");
158  }
159  BIASASSERT(Data_->Ptr);
160  return Data_->Ptr;
161  }
162 
163 
164  ///////////////////////////////////////////////////////////////////////
165  // if (!ptr) && if(ptr)
166  ///////////////////////////////////////////////////////////////////////
167 
168  template <class T>
170  {
171  return (Data_ == 0);
172  }
173 
174  /// enable the expression "if (ptr)",
175  /// see Alexandrescu "Modern C++ design", chap 7.8, p 178
176  template <class T>
178  {
179  if (Data_ == 0) return 0;
180  static Tester test;
181  return &test;
182  }
183 
184  ///////////////////////////////////////////////////////////////////////
185  // comparison
186  ///////////////////////////////////////////////////////////////////////
187 
188  template <class T>
189  template <class U>
191  {
192  return (Get(*this) == Get(rs));
193  }
194 
195  template <class T>
196  template <class U>
198  {
199  return (Get(*this) != Get(rs));
200  }
201 
202  ///////////////////////////////////////////////////////////////////////
203  // ordering
204  ///////////////////////////////////////////////////////////////////////
205 
206  template <class T>
207  template <class U>
208  bool SharedPtr<T>::operator<(const SharedPtr<U> &rs) const
209  {
210  return (Get(*this) < Get(rs));
211  }
212  template <class T>
213  template <class U>
215  {
216  return (Get(*this) <= Get(rs));
217  }
218  template <class T>
219  template <class U>
220  bool SharedPtr<T>::operator>(const SharedPtr<U> &rs) const
221  {
222  return (Get(*this) > Get(rs));
223  }
224  template <class T>
225  template <class U>
227  {
228  return (Get(*this) >= Get(rs));
229  }
230 
231  ///////////////////////////////////////////////////////////////////////
232  // cast
233  ///////////////////////////////////////////////////////////////////////
234 
235  template <class T>
236  template <class U>
238  {
239  T *tmp = dynamic_cast<T *>(Get(rs));
240  if (tmp){
241  Data_ = reinterpret_cast<SharedPtrBase<T> *>(rs.Data_);
242  Increment_();
243  } else {
244  *this = SharedPtr<T>(NULL);
245  }
246  return *this;
247  }
248 
249  template <class T>
250  template <class U>
252  {
253  T *tmp = const_cast<T *>(Get(rs));
254  if (tmp){
255  Data_ = reinterpret_cast<SharedPtrBase<T> *>(rs.Data_);
256  Increment_();
257  } else {
258  *this = SharedPtr<T>(NULL);
259  }
260  return *this;
261  }
262 
263 
264  ///////////////////////////////////////////////////////////////////////
265  // stream operator
266  ///////////////////////////////////////////////////////////////////////
267 
268  template <class T>
269  std::ostream& operator<<(std::ostream& os, const SharedPtr<T>& t)
270  {
271  os << Get(t);
272  return os;
273  }
274 
275  ///////////////////////////////////////////////////////////////////////
276  // privates
277  ///////////////////////////////////////////////////////////////////////
278 
279  template <class T>
280  class SharedPtr<T>::Tester
281  {
282  void operator delete(void *) {};
283  };
284 
285  template <class T>
287  {
288  if (Data_) {
289  BIASASSERT(Data_->Ptr);
290  Data_->Counter++;
291  //std::cout << "incremented "<<Data_->Ptr<<" to "<<Data_->Counter<<"\n";
292  }
293  }
294 
295  template <class T>
296  void SharedPtr<T>::Decrement_()
297  {
298  if (Data_){
299  BIASASSERT(Data_->Counter>0);
300  BIASASSERT(Data_->Ptr);
301  Data_->Counter--;
302  if (Data_->Counter==0){
303  // std::cout << "no references left, deleting ptr "
304  // <<Data_->Ptr<<"..."<<std::flush;
305  delete Data_->Ptr;
306  Data_->Ptr = 0;
307  // std::cout << "finished\n";
308 
309  // std::cout << "deleting data "<<Data_<<" ... "<<std::flush;
310  delete Data_;
311  Data_ = 0;
312  // std::cout << "finished\n";
313 
314  }
315  }
316  }
317 
318 }
319 
320 #endif // __SharedPtrImpl_hh__
bool operator<=(const SharedPtr< U > &rs) const
T * Get(SharedPtr< T > &t)
compile time check if B is (grand)father of T Found it in Bjarne Stroustrup&#39;s FAQ: http://public...
Definition: Constraints.hh:38
bool operator<(const SharedPtr< U > &rs) const
ordering
bool operator!=(const SharedPtr< U > &rs) const
pointer with reference count and automatic deletion
Definition: SharedPtr.hh:50
bool operator!() const
enable the expression &quot;if (!ptr)&quot;
bool operator==(const SharedPtr< U > &rs) const
comparisons
T * Get(SharedPtr< T > &t)
access function to the raw pointer
SharedPtr< T > & operator=(const SharedPtr< T > &sp)
SharedPtr< T > DynamicCast(const SharedPtr< U > &src)
DynamicCast function.
SharedPtr< T > ConstCast(const SharedPtr< U > &src)
DynamicCast function.
SharedPtr< T > ConstCast(const SharedPtr< U > &rs)
eqivalent to *this = const_cast&lt;SharedPtr&lt;T&gt; &gt;(rs)
SharedPtr< T > DynamicCast(const SharedPtr< U > &rs)
eqivalent to *this = dynamic_cast&lt;SharedPtr&lt;T&gt; &gt;(rs)
bool operator>=(const SharedPtr< U > &rs) const
bool operator>(const SharedPtr< U > &rs) const
T *const Get(const SharedPtr< T > &t)
access function to the raw pointer
virtual ~SharedPtr()
unsigned RefCount(const SharedPtr< T > &t)
access function to the reference count
T & operator*() const
pool ptr to the data and the reference count
Definition: SharedPtr.hh:39
T * operator->() const