OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NATIVE_CLIENT_SRC_INCLUDE_REF_COUNTED_H_ | 5 #ifndef NATIVE_CLIENT_SRC_INCLUDE_REF_COUNTED_H_ |
6 #define NATIVE_CLIENT_SRC_INCLUDE_REF_COUNTED_H_ | 6 #define NATIVE_CLIENT_SRC_INCLUDE_REF_COUNTED_H_ |
7 | 7 |
8 #include "native_client/src/shared/platform/nacl_sync_checked.h" | 8 #include "native_client/src/shared/platform/nacl_sync_checked.h" |
9 | 9 |
10 namespace nacl { | 10 namespace nacl { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 // class MyFoo : public nacl::RefCounted<MyFoo> { | 58 // class MyFoo : public nacl::RefCounted<MyFoo> { |
59 // ... | 59 // ... |
60 // private: | 60 // private: |
61 // friend class nacl::RefCounted<MyFoo>; | 61 // friend class nacl::RefCounted<MyFoo>; |
62 // ~MyFoo(); | 62 // ~MyFoo(); |
63 // }; | 63 // }; |
64 // | 64 // |
65 // You should always make your destructor private, to avoid any code deleting | 65 // You should always make your destructor private, to avoid any code deleting |
66 // the object accidently while there are references to it. | 66 // the object accidently while there are references to it. |
67 template <class T> | 67 template <class T> |
68 class RefCounted : public subtle::RefCountedBase { | 68 class RefCountedThreadSafe : public subtle::RefCountedBase { |
69 public: | 69 public: |
70 RefCounted() { } | 70 RefCountedThreadSafe() {} |
71 ~RefCounted() { } | |
72 | 71 |
73 void AddRef() const { | 72 void AddRef() const { |
74 subtle::RefCountedBase::AddRef(); | 73 subtle::RefCountedBase::AddRef(); |
75 } | 74 } |
76 | 75 |
77 void Release() const { | 76 void Release() const { |
78 if (subtle::RefCountedBase::Release()) { | 77 if (subtle::RefCountedBase::Release()) { |
79 delete static_cast<const T*>(this); | 78 delete static_cast<const T*>(this); |
80 } | 79 } |
81 } | 80 } |
82 | 81 |
| 82 protected: |
| 83 ~RefCountedThreadSafe() {} |
| 84 |
83 private: | 85 private: |
84 RefCounted(const RefCounted<T>&); | 86 RefCountedThreadSafe(const RefCountedThreadSafe<T>&); |
85 void operator=(const RefCounted<T>&); | 87 void operator=(const RefCountedThreadSafe<T>&); |
86 }; | 88 }; |
87 | 89 |
88 // | 90 // |
89 // A wrapper for some piece of data so we can place other things in | 91 // A wrapper for some piece of data so we can place other things in |
90 // scoped_refptrs<>. | 92 // scoped_refptrs<>. |
91 // | 93 // |
92 template<typename T> | 94 template<typename T> |
93 class RefCountedData : public nacl::RefCounted< nacl::RefCountedData<T> > { | 95 class RefCountedData |
| 96 : public RefCountedThreadSafe<RefCountedData<T> > { |
94 public: | 97 public: |
95 RefCountedData() : data() {} | 98 RefCountedData() : data() {} |
96 RefCountedData(const T& in_value) : data(in_value) {} | 99 RefCountedData(const T& in_value) : data(in_value) {} |
97 | 100 |
98 T data; | 101 T data; |
99 }; | 102 }; |
100 | 103 |
101 } // namespace nacl | 104 } // namespace nacl |
102 | 105 |
103 // | 106 // |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 // scoped_refptr<MyFoo> a = new MyFoo(); | 147 // scoped_refptr<MyFoo> a = new MyFoo(); |
145 // scoped_refptr<MyFoo> b; | 148 // scoped_refptr<MyFoo> b; |
146 // | 149 // |
147 // b = a; | 150 // b = a; |
148 // // now, |a| and |b| each own a reference to the same MyFoo object. | 151 // // now, |a| and |b| each own a reference to the same MyFoo object. |
149 // } | 152 // } |
150 // | 153 // |
151 template <class T> | 154 template <class T> |
152 class scoped_refptr { | 155 class scoped_refptr { |
153 public: | 156 public: |
154 scoped_refptr() : ptr_((T*)0) { | 157 scoped_refptr() : ptr_((T*)0) {} |
155 } | |
156 | 158 |
157 scoped_refptr(T* p) : ptr_(p) { | 159 scoped_refptr(T* p) : ptr_(p) { |
158 if (ptr_) | 160 if (ptr_) |
159 ptr_->AddRef(); | 161 ptr_->AddRef(); |
160 } | 162 } |
161 | 163 |
162 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { | 164 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { |
163 if (ptr_) | 165 if (ptr_) |
164 ptr_->AddRef(); | 166 ptr_->AddRef(); |
165 } | 167 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 }; | 226 }; |
225 | 227 |
226 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without | 228 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without |
227 // having to retype all the template arguments | 229 // having to retype all the template arguments |
228 template <typename T> | 230 template <typename T> |
229 scoped_refptr<T> make_scoped_refptr(T* t) { | 231 scoped_refptr<T> make_scoped_refptr(T* t) { |
230 return scoped_refptr<T>(t); | 232 return scoped_refptr<T>(t); |
231 } | 233 } |
232 | 234 |
233 #endif // NATIVE_CLIENT_SRC_INCLUDE_REF_COUNTED_H_ | 235 #endif // NATIVE_CLIENT_SRC_INCLUDE_REF_COUNTED_H_ |
OLD | NEW |