2014-05-13 18:00:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2011 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-04-30 14:16:07 +02:00
|
|
|
#ifndef WEBRTC_BASE_REFCOUNT_H_
|
|
|
|
|
#define WEBRTC_BASE_REFCOUNT_H_
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2015-04-30 14:16:07 +02:00
|
|
|
#include "webrtc/base/atomicops.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
|
|
|
|
|
// Reference count interface.
|
|
|
|
|
class RefCountInterface {
|
|
|
|
|
public:
|
2015-10-12 15:50:43 +02:00
|
|
|
virtual int AddRef() const = 0;
|
|
|
|
|
virtual int Release() const = 0;
|
2014-05-13 18:00:26 +00:00
|
|
|
protected:
|
|
|
|
|
virtual ~RefCountInterface() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
class RefCountedObject : public T {
|
|
|
|
|
public:
|
2015-12-07 14:29:14 -08:00
|
|
|
RefCountedObject() : ref_count_(0) {
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2015-12-07 14:29:14 -08:00
|
|
|
template<typename P>
|
|
|
|
|
explicit RefCountedObject(P p) : T(p), ref_count_(0) {
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2015-12-07 14:29:14 -08:00
|
|
|
template<typename P1, typename P2>
|
|
|
|
|
RefCountedObject(P1 p1, P2 p2) : T(p1, p2), ref_count_(0) {
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2015-12-07 14:29:14 -08:00
|
|
|
template<typename P1, typename P2, typename P3>
|
|
|
|
|
RefCountedObject(P1 p1, P2 p2, P3 p3) : T(p1, p2, p3), ref_count_(0) {
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2015-12-07 14:29:14 -08:00
|
|
|
template<typename P1, typename P2, typename P3, typename P4>
|
2014-05-13 18:00:26 +00:00
|
|
|
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4)
|
2015-12-07 14:29:14 -08:00
|
|
|
: T(p1, p2, p3, p4), ref_count_(0) {
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2015-12-07 14:29:14 -08:00
|
|
|
template<typename P1, typename P2, typename P3, typename P4, typename P5>
|
2014-05-13 18:00:26 +00:00
|
|
|
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
|
2015-12-07 14:29:14 -08:00
|
|
|
: T(p1, p2, p3, p4, p5), ref_count_(0) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename P1, typename P2, typename P3, typename P4, typename P5,
|
|
|
|
|
typename P6>
|
2015-04-02 12:30:51 +02:00
|
|
|
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6)
|
2015-12-07 14:29:14 -08:00
|
|
|
: T(p1, p2, p3, p4, p5, p6), ref_count_(0) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename P1, typename P2, typename P3, typename P4, typename P5,
|
|
|
|
|
typename P6, typename P7>
|
2015-04-02 12:30:51 +02:00
|
|
|
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7)
|
2015-12-07 14:29:14 -08:00
|
|
|
: T(p1, p2, p3, p4, p5, p6, p7), ref_count_(0) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename P1, typename P2, typename P3, typename P4, typename P5,
|
|
|
|
|
typename P6, typename P7, typename P8>
|
2015-04-02 12:30:51 +02:00
|
|
|
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8)
|
2015-12-07 14:29:14 -08:00
|
|
|
: T(p1, p2, p3, p4, p5, p6, p7, p8), ref_count_(0) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename P1, typename P2, typename P3, typename P4, typename P5,
|
|
|
|
|
typename P6, typename P7, typename P8, typename P9>
|
|
|
|
|
RefCountedObject(
|
|
|
|
|
P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9)
|
|
|
|
|
: T(p1, p2, p3, p4, p5, p6, p7, p8, p9), ref_count_(0) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename P1, typename P2, typename P3, typename P4, typename P5,
|
|
|
|
|
typename P6, typename P7, typename P8, typename P9, typename P10>
|
|
|
|
|
RefCountedObject(
|
|
|
|
|
P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10)
|
|
|
|
|
: T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10), ref_count_(0) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename P1, typename P2, typename P3, typename P4, typename P5,
|
|
|
|
|
typename P6, typename P7, typename P8, typename P9, typename P10,
|
|
|
|
|
typename P11>
|
|
|
|
|
RefCountedObject(
|
|
|
|
|
P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10,
|
|
|
|
|
P11 p11)
|
|
|
|
|
: T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11), ref_count_(0) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual int AddRef() const {
|
|
|
|
|
return AtomicOps::Increment(&ref_count_);
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2015-10-12 16:10:43 +02:00
|
|
|
virtual int Release() const {
|
2015-12-07 14:29:14 -08:00
|
|
|
int count = AtomicOps::Decrement(&ref_count_);
|
2014-05-13 18:00:26 +00:00
|
|
|
if (!count) {
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-21 13:23:27 +00:00
|
|
|
// 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 {
|
2015-12-07 14:29:14 -08:00
|
|
|
return AtomicOps::AcquireLoad(&ref_count_) == 1;
|
2015-02-21 13:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
protected:
|
|
|
|
|
virtual ~RefCountedObject() {
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-07 14:29:14 -08:00
|
|
|
mutable volatile int ref_count_;
|
2014-05-13 18:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace rtc
|
|
|
|
|
|
2015-04-30 14:16:07 +02:00
|
|
|
#endif // WEBRTC_BASE_REFCOUNT_H_
|