OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 // To generate refcount.h from refcount.h.pump, execute: | |
12 // ./testing/gtest/scripts/pump.py ./webrtc/base/refcountedobject.h.pump | |
13 | |
14 #ifndef WEBRTC_BASE_REFCOUNTEDOBJECT_H_ | |
15 #define WEBRTC_BASE_REFCOUNTEDOBJECT_H_ | |
16 | |
17 #include <utility> | |
18 | |
19 #include "webrtc/base/atomicops.h" | |
20 | |
21 namespace rtc { | |
22 | |
23 template <class T> | |
24 class RefCountedObject : public T { | |
25 public: | |
26 RefCountedObject() {} | |
27 | |
28 $range i 0..10 | |
29 $for i [[ | |
30 $range j 0..i | |
31 template <$for j , [[class P$j]]> | |
32 $if i == 0 [[explicit ]] | |
33 RefCountedObject($for j , [[P$j&& p$j]]) : T($for j , [[std::forward<P$j>(p$j)
]]) {} | |
34 ]] | |
35 | |
36 virtual int AddRef() const { return AtomicOps::Increment(&ref_count_); } | |
37 | |
38 virtual int Release() const { | |
39 int count = AtomicOps::Decrement(&ref_count_); | |
40 if (!count) { | |
41 delete this; | |
42 } | |
43 return count; | |
44 } | |
45 | |
46 // Return whether the reference count is one. If the reference count is used | |
47 // in the conventional way, a reference count of 1 implies that the current | |
48 // thread owns the reference and no other thread shares it. This call | |
49 // performs the test for a reference count of one, and performs the memory | |
50 // barrier needed for the owning thread to act on the object, knowing that it | |
51 // has exclusive access to the object. | |
52 virtual bool HasOneRef() const { | |
53 return AtomicOps::AcquireLoad(&ref_count_) == 1; | |
54 } | |
55 | |
56 protected: | |
57 virtual ~RefCountedObject() {} | |
58 | |
59 mutable volatile int ref_count_ = 0; | |
60 }; | |
61 | |
62 } // namespace rtc | |
63 | |
64 #endif // WEBRTC_BASE_REFCOUNTEDOBJECT_H_ | |
OLD | NEW |