The main reason for doing this is to allow refcounted objects to accept rvalue references in ctor and be able to std::move ctor rvalue arguments. Also, refcounted.h is now generated using pump.py instead of manually creating each ctor version. BUG= none Review-Url: https://codereview.webrtc.org/2425683003 Cr-Commit-Position: refs/heads/master@{#14687}
65 lines
1.8 KiB
Plaintext
65 lines
1.8 KiB
Plaintext
/*
|
|
* Copyright 2016 The WebRTC Project Authors. All rights reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
// To generate refcount.h from refcount.h.pump, execute:
|
|
// ./testing/gtest/scripts/pump.py ./webrtc/base/refcountedobject.h.pump
|
|
|
|
#ifndef WEBRTC_BASE_REFCOUNTEDOBJECT_H_
|
|
#define WEBRTC_BASE_REFCOUNTEDOBJECT_H_
|
|
|
|
#include <utility>
|
|
|
|
#include "webrtc/base/atomicops.h"
|
|
|
|
namespace rtc {
|
|
|
|
template <class T>
|
|
class RefCountedObject : public T {
|
|
public:
|
|
RefCountedObject() {}
|
|
|
|
$range i 0..10
|
|
$for i [[
|
|
$range j 0..i
|
|
template <$for j , [[class P$j]]>
|
|
$if i == 0 [[explicit ]]
|
|
RefCountedObject($for j , [[P$j&& p$j]]) : T($for j , [[std::forward<P$j>(p$j)]]) {}
|
|
]]
|
|
|
|
virtual int AddRef() const { return AtomicOps::Increment(&ref_count_); }
|
|
|
|
virtual int Release() const {
|
|
int count = AtomicOps::Decrement(&ref_count_);
|
|
if (!count) {
|
|
delete this;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
// Return whether the reference count is one. If the reference count is used
|
|
// in the conventional way, a reference count of 1 implies that the current
|
|
// thread owns the reference and no other thread shares it. This call
|
|
// performs the test for a reference count of one, and performs the memory
|
|
// barrier needed for the owning thread to act on the object, knowing that it
|
|
// has exclusive access to the object.
|
|
virtual bool HasOneRef() const {
|
|
return AtomicOps::AcquireLoad(&ref_count_) == 1;
|
|
}
|
|
|
|
protected:
|
|
virtual ~RefCountedObject() {}
|
|
|
|
mutable volatile int ref_count_ = 0;
|
|
};
|
|
|
|
} // namespace rtc
|
|
|
|
#endif // WEBRTC_BASE_REFCOUNTEDOBJECT_H_
|