Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ref_counted_ptr.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
30 
31 #ifndef PAGESPEED_KERNEL_UTIL_REF_COUNTED_PTR_H_
32 #define PAGESPEED_KERNEL_UTIL_REF_COUNTED_PTR_H_
33 
34 #include "base/logging.h"
37 
38 namespace net_instaweb {
39 
40 template<class T>
41 class RefCounted {
42  public:
43  RefCounted() : ref_count_(0) {}
44  ~RefCounted() { DCHECK_EQ(0, ref_count_.value()); }
45 
46  void Release() {
47  if (ref_count_.BarrierIncrement(-1) == 0) {
48  delete static_cast<T*>(this);
49  }
50  }
51 
52  void AddRef() {
53  ref_count_.NoBarrierIncrement(1);
54  }
55 
56  bool HasOneRef() {
57  return (ref_count_.value() == 1);
58  }
59 
60  private:
61  AtomicInt32 ref_count_;
62 
63 };
64 
68 template<class T>
70  public:
71  RefCountedPtr() : ptr_(NULL) {}
72  explicit RefCountedPtr(T* t) : ptr_(t) {
73  if (t != NULL) {
74  t->AddRef();
75  }
76  }
77 
79  : ptr_(src.ptr_) {
80  if (ptr_ != NULL) {
81  ptr_->AddRef();
82  }
83  }
84 
85  template<class U>
86  explicit RefCountedPtr(const RefCountedPtr<U>& src)
87  : ptr_(static_cast<T*>(src.ptr_)) {
88  if (ptr_ != NULL) {
89  ptr_->AddRef();
90  }
91  }
92 
93  ~RefCountedPtr() {
94  if (ptr_ != NULL) {
95  ptr_->Release();
96  }
97  }
98 
101  if (other.ptr_ != NULL) {
102  other.ptr_->AddRef();
103  }
104  if (ptr_ != NULL) {
105  ptr_->Release();
106  }
107  ptr_ = other.ptr_;
108  return *this;
109  }
110 
111  template<class U>
113  if (other.ptr_ != NULL) {
114  other.ptr_->AddRef();
115  }
116  if (ptr_ != NULL) {
117  ptr_->Release();
118  }
119  ptr_ = static_cast<T*>(other.ptr_);
120  return *this;
121  }
122 
123  T* operator->() const { return ptr_; }
124  T* get() const { return ptr_; }
125 
129  bool unique() const { return !this->ptr_ || this->ptr_->HasOneRef(); }
130 
131  template<typename U>
132  RefCountedPtr<U> StaticCast() const {
133  return RefCountedPtr<U>(static_cast<U*>(this->get()));
134  }
135 
136  void clear() {
137  *this = RefCountedPtr();
138  }
139  void reset(T* ptr) {
140  *this = RefCountedPtr(ptr);
141  }
142  void reset(const RefCountedPtr& src) {
143  *this = src;
144  }
145 
146  private:
147  template <class U> friend class RefCountedPtr;
148  operator void*() const;
149  operator T*() const;
150 
153  T* ptr_;
154 };
155 
161 template<class T>
163  public:
164  RefCountedObj() : data_ptr_(new Data()) {}
165  explicit RefCountedObj(const T& val) : data_ptr_(new Data(val)) {}
166 
170  bool unique() const { return data_ptr_.unique(); }
171 
172  T* get() { return &data_ptr_->value; }
173  const T* get() const { return &data_ptr_->value; }
174  T* operator->() { return &data_ptr_->value; }
175  const T* operator->() const { return &data_ptr_->value; }
176  T& operator*() { return data_ptr_->value; }
177  const T& operator*() const { return data_ptr_->value; }
178 
182  void reset(const T& val) { data_ptr_.reset(new Data(val)); }
183 
184  protected:
185  struct Data : public RefCounted<Data> {
186  Data() {}
187  explicit Data(const T& val) : value(val) {}
188  T value;
189  };
190 
191  RefCountedPtr<Data> data_ptr_;
192 
193  private:
194  operator void*() const;
195  operator T*() const;
196 
198 };
199 
209 #define REFCOUNT_FRIEND_DECLARATION(class_name) \
210  friend class net_instaweb::RefCounted<class_name>
211 
212 }
213 
214 #endif
int32 BarrierIncrement(int32 amount)
Definition: atomic_int32.h:101
Definition: atomic_int32.h:72
Definition: ref_counted_ptr.h:41
Definition: ref_counted_ptr.h:185
bool unique() const
Definition: ref_counted_ptr.h:129
Definition: ref_counted_ptr.h:69
RefCountedPtr< T > & operator=(const RefCountedPtr< T > &other)
Definition: ref_counted_ptr.h:99
bool unique() const
Definition: ref_counted_ptr.h:170
void reset(const T &val)
Definition: ref_counted_ptr.h:182
int32 NoBarrierIncrement(int32 amount)
Definition: atomic_int32.h:95
int32 value() const
Return the value currently stored. Has acquire semantics (see above).
Definition: atomic_int32.h:83
Definition: ref_counted_ptr.h:162