Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2011 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/refcount.h.pump | |
| 13 | |
| 14 #ifndef WEBRTC_BASE_REFCOUNT_H_ | |
| 15 #define WEBRTC_BASE_REFCOUNT_H_ | |
| 16 | |
| 17 #include <string.h> | |
| 18 #include <utility> | |
| 19 | |
| 20 #include "webrtc/base/atomicops.h" | |
| 21 | |
| 22 namespace rtc { | |
| 23 | |
| 24 // Reference count interface. | |
| 25 class RefCountInterface { | |
|
tommi
2016/10/18 15:57:58
Can we have this definition in a non-generated hea
perkj_webrtc
2016/10/19 00:43:14
Done.
| |
| 26 public: | |
| 27 virtual int AddRef() const = 0; | |
| 28 virtual int Release() const = 0; | |
| 29 | |
| 30 protected: | |
| 31 virtual ~RefCountInterface() {} | |
| 32 }; | |
| 33 | |
| 34 | |
| 35 template <class T> | |
| 36 class RefCountedObject : public T { | |
| 37 public: | |
| 38 RefCountedObject() {} | |
| 39 | |
| 40 $range i 0..10 | |
| 41 $for i [[ | |
| 42 $range j 0..i | |
| 43 template <$for j , [[class P$j]]> | |
| 44 $if i == 0 [[explicit ]] | |
| 45 RefCountedObject($for j , [[P$j&& p$j]]) : T($for j , [[std::forward<P$j>(p$j) ]]) {} | |
| 46 ]] | |
| 47 | |
| 48 virtual int AddRef() const { return AtomicOps::Increment(&ref_count_); } | |
| 49 | |
| 50 virtual int Release() const { | |
| 51 int count = AtomicOps::Decrement(&ref_count_); | |
| 52 if (!count) { | |
| 53 delete this; | |
| 54 } | |
| 55 return count; | |
| 56 } | |
| 57 | |
| 58 // Return whether the reference count is one. If the reference count is used | |
| 59 // in the conventional way, a reference count of 1 implies that the current | |
| 60 // thread owns the reference and no other thread shares it. This call | |
| 61 // performs the test for a reference count of one, and performs the memory | |
| 62 // barrier needed for the owning thread to act on the object, knowing that it | |
| 63 // has exclusive access to the object. | |
| 64 virtual bool HasOneRef() const { | |
| 65 return AtomicOps::AcquireLoad(&ref_count_) == 1; | |
| 66 } | |
| 67 | |
| 68 protected: | |
| 69 virtual ~RefCountedObject() {} | |
| 70 | |
| 71 mutable volatile int ref_count_ = 0; | |
| 72 }; | |
| 73 | |
| 74 } // namespace rtc | |
| 75 | |
| 76 #endif // WEBRTC_BASE_REFCOUNT_H_ | |
| OLD | NEW |