CernVM-FS  2.10.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>
51  explicit SharedPtr(SharedPtr<Y> const& r)
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  if (this == &r)
60  return *this;
61 
62  Reset();
63  value_ = r.value_;
64  count_ = r.count_;
65  if (count_) {
66  atomic_inc64(count_);
67  }
68  return *this;
69  }
70 
71  template <class Y>
72  SharedPtr& operator=(SharedPtr<Y> const& r) { // never throws
73  Reset();
74  value_ = r.value_;
75  count_ = r.count_;
76  if (count_) {
77  atomic_inc64(count_);
78  }
79  return *this;
80  }
81 
82  void Reset() { // never throws
83  if (count_) {
84  atomic_dec64(count_);
85  if (atomic_read64(count_) == 0) {
86  delete value_;
87  delete count_;
88  }
89  value_ = NULL;
90  count_ = NULL;
91  }
92  }
93 
94  template <class Y>
95  void Reset(Y* p) {
96  Reset();
97  value_ = static_cast<element_type*>(p);
98  count_ = new atomic_int64;
99  atomic_write64(count_, 1);
100  }
101 
102  T& operator*() const { // never throws
103  return *value_;
104  }
105 
106  T* operator->() const { // never throws
107  return value_;
108  }
109 
110  element_type* Get() const { // never throws
111  return value_;
112  }
113 
114  bool Unique() const { // never throws
115  return count_ && (atomic_read64(count_) == 1);
116  }
117 
118  int64_t UseCount() const { // never throws
119  return count_ ? atomic_read64(count_) : -1;
120  }
121 
122  private:
123  element_type* value_;
125 };
126 
127 template <class T, class U>
128 bool operator==(SharedPtr<T> const& a,
129  SharedPtr<U> const& b) { // never throws
130  return a.value_ == b.value_;
131 }
132 
133 template <class T, class U>
134 bool operator!=(SharedPtr<T> const& a,
135  SharedPtr<U> const& b) { // never throws
136  return a.value_ != b.value_;
137 }
138 
139 template <class T, class U>
140 bool operator<(SharedPtr<T> const& a, SharedPtr<U> const& b) { // never throws
141  return a.value_ < b.value_;
142 }
143 
144 template <class T>
146  SharedPtr<T> const& p) { // never throws
147  return p.value_;
148 }
149 
150 template <class T, class U>
151 SharedPtr<T> StaticPointerCast(SharedPtr<U> const& r) { // never throws
152  return SharedPtr<T>(static_cast<T*>(r.value_));
153 }
154 
155 template <class T, class U>
156 SharedPtr<T> ConstPointerCast(SharedPtr<U> const& r) { // never throws
157  return SharedPtr<T>(const_cast<T*>(r.value_));
158 }
159 
160 template <class T, class U>
161 SharedPtr<T> DynamicPointerCast(SharedPtr<U> const& r) { // never throws
162  return SharedPtr<T>(dynamic_cast<T*>(r.value_));
163 }
164 
165 template <class T, class U>
167  return SharedPtr<T>(reinterpret_cast<T*>(r.value_));
168 }
169 
170 #ifdef CVMFS_NAMESPACE_GUARD
171 } // namespace CVMFS_NAMESPACE_GUARD
172 #endif
173 
174 #endif // CVMFS_UTIL_SHARED_PTR_H_
T & operator*() const
Definition: shared_ptr.h:102
void Reset()
Definition: shared_ptr.h:82
SharedPtr< T > ConstPointerCast(SharedPtr< U > const &r)
Definition: shared_ptr.h:156
int64_t atomic_int64
Definition: atomic.h:18
void Reset(Y *p)
Definition: shared_ptr.h:95
element_type * value_
Definition: shared_ptr.h:123
SharedPtr< T > DynamicPointerCast(SharedPtr< U > const &r)
Definition: shared_ptr.h:161
SharedPtr(SharedPtr< Y > const &r)
Definition: shared_ptr.h:51
SharedPtr< T >::element_type * GetPointer(SharedPtr< T > const &p)
Definition: shared_ptr.h:145
SharedPtr< T > ReinterpretPointerCast(SharedPtr< U > const &r)
Definition: shared_ptr.h:166
SharedPtr< T > StaticPointerCast(SharedPtr< U > const &r)
Definition: shared_ptr.h:151
~SharedPtr()
Definition: shared_ptr.h:33
atomic_int64 * count_
Definition: shared_ptr.h:124
SharedPtr & operator=(SharedPtr const &r)
Definition: shared_ptr.h:58
bool Unique() const
Definition: shared_ptr.h:114
bool operator!=(const cvmcache_hash &a, const cvmcache_hash &b)
int64_t UseCount() const
Definition: shared_ptr.h:118
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:110
SharedPtr(SharedPtr const &r)
Definition: shared_ptr.h:43
T * operator->() const
Definition: shared_ptr.h:106
T element_type
Definition: shared_ptr.h:19
SharedPtr & operator=(SharedPtr< Y > const &r)
Definition: shared_ptr.h:72