Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SharedPtr.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 #ifndef __SharedPtr_hh__
23 #define __SharedPtr_hh__
24 
25 #include "bias_config.h"
26 
27 #include <Base/Debug/Error.hh>
28 
29 #include <iostream>
30 
31 namespace BIAS {
32 
33  /** @class SharedPtrBase
34  * @test tested with TestSharedPtr.cpp
35  @relates SharedPtr
36  @brief pool ptr to the data and the reference count
37  @author woelk 09/2007 (c) www.vision-n.de */
38  template <class T>
40  {
41  public:
42  SharedPtrBase() : Ptr(0), Counter(0) {}
43  SharedPtrBase(T* ptr) : Ptr(ptr), Counter((ptr)?(1):(0)) {}
44  virtual ~SharedPtrBase(){}
45  T* Ptr;
46  unsigned Counter;
47  };
48 
49  // forward declaration of class
50  template <class T> class SharedPtr;
51 
52  // forward declaration of friend functions
53  template <class T>
54  T* Get(SharedPtr<T> &t);
55  template <class T>
56  T* const Get(const SharedPtr<T> &t);
57  template <class T>
58  unsigned RefCount(const SharedPtr<T>& t);
59  template <class T>
60  std::ostream& operator<<(std::ostream& os, const SharedPtr<T>& t);
61  template <class T, class U>
63  template <class T, class U>
65 
66 
67  /** @class SharedPtr
68  @ingroup g_utils
69  @brief pointer with reference count and automatic deletion
70 
71  This class is tested see Base/Common/Tests/TestSharedPtr.cpp
72 
73  @TODO !this class is not thread safe!
74 
75  @author woelk 09/2007 (c) www.vision-n.de */
76  template <class T>
77  class SharedPtr {
78  private:
79  /// needed to enable the expression "if (ptr)" while at the same time
80  /// preventing "delete ptr" from compiling
81  class Tester;
82  public:
83  SharedPtr();
84 
85  /// needed to enable assignement of SharedPtr<T>
86  /// to SharedPtr<const T>
87  template <class U>
88  SharedPtr(const SharedPtr<U> &pt);
89 
90  SharedPtr(const SharedPtr<T> &pt);
91 
92  explicit SharedPtr(T *pt);
93 
94  virtual ~SharedPtr();
95 
97 
98  T& operator*() const;
99 
100  T* operator->() const;
101 
102  /// enable the expression "if (!ptr)"
103  bool operator!() const;
104 
105  /// Enable the expression "if (ptr)", while at the same time preventing
106  /// "delete ptr" from compilling. This is done by providing an automatic
107  /// conversion operator to the "Tester" class. The tester class itself
108  /// introduces an additional "delete" function and thus makes "delete ptr"
109  /// ambiguous.
110  /// See Alexandrescu "Modern C++ design", chap 7.8, p 178
111  operator Tester*() const;
112 
113  /// comparisons
114  template <class U>
115  bool operator==(const SharedPtr<U>& rs) const;
116  template <class U>
117  bool operator!=(const SharedPtr<U>& rs) const;
118 
119  /// ordering
120  template <class U>
121  bool operator<(const SharedPtr<U>& rs) const;
122  template <class U>
123  bool operator<=(const SharedPtr<U>& rs) const;
124  template <class U>
125  bool operator>(const SharedPtr<U>& rs) const;
126  template <class U>
127  bool operator>=(const SharedPtr<U>& rs) const;
128 
129  // casts
130 
131  /// eqivalent to *this = dynamic_cast<SharedPtr<T> >(rs)
132  template <class U>
134 
135  /// eqivalent to *this = const_cast<SharedPtr<T> >(rs)
136  template <class U>
138 
139  /// access functions
140  friend T* Get<>(SharedPtr<T> &t);
141  friend T * const Get<>(const SharedPtr<T> &t);
142  friend unsigned RefCount<>(const SharedPtr<T>& t);
143 
144  /// stream operator
145  friend std::ostream& operator<<<>(std::ostream& os, const SharedPtr<T>& t);
146 
147  // friend class
148  template <class U> friend class SharedPtr;
149 
150  private:
151  /// hold the pointer and the reference counter
152  SharedPtrBase<T> *Data_;
153 
154  ///
155  void Increment_();
156 
157  void Decrement_();
158 
159 
160  }; // class
161 
162 
163 
164 
165 } // namespace
166 
167 #include "SharedPtrImpl.hh"
168 
169 #endif // __SharedPtr_hh__
SharedPtrBase(T *ptr)
Definition: SharedPtr.hh:43
T * Get(SharedPtr< T > &t)
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
SharedPtr< T > DynamicCast(const SharedPtr< U > &src)
SharedPtr< T > & operator=(const SharedPtr< T > &sp)
SharedPtr< T > ConstCast(const SharedPtr< U > &rs)
eqivalent to *this = const_cast&lt;SharedPtr&lt;T&gt; &gt;(rs)
unsigned RefCount(const SharedPtr< T > &t)
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
virtual ~SharedPtr()
T & operator*() const
pool ptr to the data and the reference count
Definition: SharedPtr.hh:39
SharedPtr< T > ConstCast(const SharedPtr< U > &src)
virtual ~SharedPtrBase()
Definition: SharedPtr.hh:44
T * operator->() const