CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shared_ptr.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_UTIL_SHARED_PTR_H_
6 #define CVMFS_UTIL_SHARED_PTR_H_
7 
8 #include <cstdlib>
9 
10 #include "atomic.h"
11 
12 #ifdef CVMFS_NAMESPACE_GUARD
13 namespace CVMFS_NAMESPACE_GUARD {
14 #endif // CVMFS_NAMESPACE_GUARD
15 
16 template <typename T>
17 class SharedPtr {
18  public:
19  typedef T element_type;
20 
21  SharedPtr() { // never throws
22  value_ = NULL;
23  count_ = NULL;
24  }
25 
26  template <class Y>
27  explicit SharedPtr(Y* p) {
28  value_ = static_cast<element_type*>(p);
29  count_ = new atomic_int64;
30  atomic_write64(count_, 1);
31  }
32 
33  ~SharedPtr() { // never throws
34  if (count_) {
35  atomic_dec64(count_);
36  if (atomic_read64(count_) == 0) {
37  delete value_;
38  delete count_;
39  }
40  }
41  }
42 
43  SharedPtr(SharedPtr const& r)
44  : value_(r.value_), count_(r.count_) { // never throws
45  if (count_) {
46  atomic_inc64(count_);
47  }
48  }
49 
50  template <class Y>
52  : value_(r.value_), count_(r.count_) { // never throws
53  if (count_) {
54  atomic_inc64(count_);
55  }
56  }
57 
58  SharedPtr& operator=(SharedPtr const& r) { // never throws
59  Reset();
60  value_ = r.value_;
61  count_ = r.count_;
62  if (count_) {
63  atomic_inc64(count_);
64  }
65  return *this;
66  }
67 
68  template <class Y>
69  SharedPtr& operator=(SharedPtr<Y> const& r) { // never throws
70  Reset();
71  value_ = r.value_;
72  count_ = r.count_;
73  if (count_) {
74  atomic_inc64(count_);
75  }
76  return *this;
77  }
78 
79  void Reset() { // never throws
80  if (count_) {
81  atomic_dec64(count_);
82  if (atomic_read64(count_) == 0) {
83  delete value_;
84  delete count_;
85  }
86  value_ = NULL;
87  count_ = NULL;
88  }
89  }
90 
91  template <class Y>
92  void Reset(Y* p) {
93  Reset();
94  value_ = static_cast<element_type*>(p);
95  count_ = new atomic_int64;
96  atomic_write64(count_, 1);
97  }
98 
99  T& operator*() const { // never throws
100  return *value_;
101  }
102 
103  T* operator->() const { // never throws
104  return value_;
105  }
106 
107  element_type* Get() const { // never throws
108  return value_;
109  }
110 
111  bool Unique() const { // never throws
112  return count_ && (atomic_read64(count_) == 1);
113  }
114 
115  int64_t UseCount() const { // never throws
116  return count_ ? atomic_read64(count_) : -1;
117  }
118 
119  operator void*() const { // never throws
120  return static_cast<void*>(value_);
121  }
122 
123  private:
124  element_type* value_;
126 };
127 
128 template <class T, class U>
129 bool operator==(SharedPtr<T> const& a,
130  SharedPtr<U> const& b) { // never throws
131  return a.value_ == b.value_;
132 }
133 
134 template <class T, class U>
135 bool operator!=(SharedPtr<T> const& a,
136  SharedPtr<U> const& b) { // never throws
137  return a.value_ != b.value_;
138 }
139 
140 template <class T, class U>
141 bool operator<(SharedPtr<T> const& a, SharedPtr<U> const& b) { // never throws
142  return a.value_ < b.value_;
143 }
144 
145 template <class T>
147  SharedPtr<T> const& p) { // never throws
148  return p.value_;
149 }
150 
151 template <class T, class U>
152 SharedPtr<T> StaticPointerCast(SharedPtr<U> const& r) { // never throws
153  return SharedPtr<T>(static_cast<T*>(r.value_));
154 }
155 
156 template <class T, class U>
157 SharedPtr<T> ConstPointerCast(SharedPtr<U> const& r) { // never throws
158  return SharedPtr<T>(const_cast<T*>(r.value_));
159 }
160 
161 template <class T, class U>
162 SharedPtr<T> DynamicPointerCast(SharedPtr<U> const& r) { // never throws
163  return SharedPtr<T>(dynamic_cast<T*>(r.value_));
164 }
165 
166 template <class T, class U>
168  return SharedPtr<T>(reinterpret_cast<T*>(r.value_));
169 }
170 
171 #ifdef CVMFS_NAMESPACE_GUARD
172 } // namespace CVMFS_NAMESPACE_GUARD
173 #endif
174 
175 #endif // CVMFS_UTIL_SHARED_PTR_H_
T & operator*() const
Definition: shared_ptr.h:99
void Reset()
Definition: shared_ptr.h:79
SharedPtr< T > ConstPointerCast(SharedPtr< U > const &r)
Definition: shared_ptr.h:157
int64_t atomic_int64
Definition: atomic.h:18
void Reset(Y *p)
Definition: shared_ptr.h:92
element_type * value_
Definition: shared_ptr.h:124
SharedPtr< T > DynamicPointerCast(SharedPtr< U > const &r)
Definition: shared_ptr.h:162
SharedPtr(SharedPtr< Y > const &r)
Definition: shared_ptr.h:51
SharedPtr< T >::element_type * GetPointer(SharedPtr< T > const &p)
Definition: shared_ptr.h:146
SharedPtr< T > ReinterpretPointerCast(SharedPtr< U > const &r)
Definition: shared_ptr.h:167
SharedPtr< T > StaticPointerCast(SharedPtr< U > const &r)
Definition: shared_ptr.h:152
~SharedPtr()
Definition: shared_ptr.h:33
atomic_int64 * count_
Definition: shared_ptr.h:125
SharedPtr & operator=(SharedPtr const &r)
Definition: shared_ptr.h:58
bool Unique() const
Definition: shared_ptr.h:111
bool operator!=(const cvmcache_hash &a, const cvmcache_hash &b)
int64_t UseCount() const
Definition: shared_ptr.h:115
SharedPtr(Y *p)
Definition: shared_ptr.h:27
bool operator==(const cvmcache_hash &a, const cvmcache_hash &b)
element_type * Get() const
Definition: shared_ptr.h:107
SharedPtr(SharedPtr const &r)
Definition: shared_ptr.h:43
T * operator->() const
Definition: shared_ptr.h:103
T element_type
Definition: shared_ptr.h:19
SharedPtr & operator=(SharedPtr< Y > const &r)
Definition: shared_ptr.h:69