Add r-value constructor for RefCountedObject.
Previously RefCountedObject was passing all parameters by value.
This meant that it was hard to use it with movable types, such
as unique_ptr<>. Now there is a constructor that takes r-value,
which means that RefCountedObject<std::unique_ptr<foo>> can be
initialized by passing std::unique_ptr<foo> to the constructor.
Review-Url: https://codereview.webrtc.org/2036123002
Cr-Commit-Position: refs/heads/master@{#13079}
diff --git a/webrtc/base/refcount.h b/webrtc/base/refcount.h
index 55ce23a..9e881c8 100644
--- a/webrtc/base/refcount.h
+++ b/webrtc/base/refcount.h
@@ -12,6 +12,7 @@
#define WEBRTC_BASE_REFCOUNT_H_
#include <string.h>
+#include <utility>
#include "webrtc/base/atomicops.h"
@@ -22,6 +23,7 @@
public:
virtual int AddRef() const = 0;
virtual int Release() const = 0;
+
protected:
virtual ~RefCountInterface() {}
};
@@ -29,75 +31,123 @@
template <class T>
class RefCountedObject : public T {
public:
- RefCountedObject() : ref_count_(0) {
- }
+ RefCountedObject() {}
- template<typename P>
- explicit RefCountedObject(P p) : T(p), ref_count_(0) {
- }
+ template <typename P>
+ explicit RefCountedObject(const P& p) : T(p) {}
- template<typename P1, typename P2>
- RefCountedObject(P1 p1, P2 p2) : T(p1, p2), ref_count_(0) {
- }
+ template <typename P>
+ explicit RefCountedObject(P&& p) : T(std::move(p)) {}
- template<typename P1, typename P2, typename P3>
- RefCountedObject(P1 p1, P2 p2, P3 p3) : T(p1, p2, p3), ref_count_(0) {
- }
+ template <typename P1, typename P2>
+ RefCountedObject(P1 p1, P2 p2) : T(p1, p2) {}
- template<typename P1, typename P2, typename P3, typename P4>
- RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4)
- : T(p1, p2, p3, p4), ref_count_(0) {
- }
+ template <typename P1, typename P2, typename P3>
+ RefCountedObject(P1 p1, P2 p2, P3 p3) : T(p1, p2, p3) {}
- template<typename P1, typename P2, typename P3, typename P4, typename P5>
- RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
- : T(p1, p2, p3, p4, p5), ref_count_(0) {
- }
+ template <typename P1, typename P2, typename P3, typename P4>
+ RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4) : T(p1, p2, p3, p4) {}
- template<typename P1, typename P2, typename P3, typename P4, typename P5,
- typename P6>
+ template <typename P1, typename P2, typename P3, typename P4, typename P5>
+ RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) : T(p1, p2, p3, p4, p5) {}
+
+ template <typename P1,
+ typename P2,
+ typename P3,
+ typename P4,
+ typename P5,
+ typename P6>
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6)
- : T(p1, p2, p3, p4, p5, p6), ref_count_(0) {
- }
+ : T(p1, p2, p3, p4, p5, p6) {}
- template<typename P1, typename P2, typename P3, typename P4, typename P5,
- typename P6, typename P7>
+ template <typename P1,
+ typename P2,
+ typename P3,
+ typename P4,
+ typename P5,
+ typename P6,
+ typename P7>
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7)
- : T(p1, p2, p3, p4, p5, p6, p7), ref_count_(0) {
- }
+ : T(p1, p2, p3, p4, p5, p6, p7) {}
- template<typename P1, typename P2, typename P3, typename P4, typename P5,
- typename P6, typename P7, typename P8>
+ template <typename P1,
+ typename P2,
+ typename P3,
+ typename P4,
+ typename P5,
+ typename P6,
+ typename P7,
+ typename P8>
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8)
- : T(p1, p2, p3, p4, p5, p6, p7, p8), ref_count_(0) {
- }
+ : T(p1, p2, p3, p4, p5, p6, p7, p8) {}
- 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>
+ 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) {}
- 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>
+ 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) {}
- 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) {
- }
+ 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) {}
- virtual int AddRef() const {
- return AtomicOps::Increment(&ref_count_);
- }
+ virtual int AddRef() const { return AtomicOps::Increment(&ref_count_); }
virtual int Release() const {
int count = AtomicOps::Decrement(&ref_count_);
@@ -118,10 +168,9 @@
}
protected:
- virtual ~RefCountedObject() {
- }
+ virtual ~RefCountedObject() {}
- mutable volatile int ref_count_;
+ mutable volatile int ref_count_ = 0;
};
} // namespace rtc
diff --git a/webrtc/modules/desktop_capture/shared_desktop_frame.cc b/webrtc/modules/desktop_capture/shared_desktop_frame.cc
index e069a54..3114235 100644
--- a/webrtc/modules/desktop_capture/shared_desktop_frame.cc
+++ b/webrtc/modules/desktop_capture/shared_desktop_frame.cc
@@ -23,7 +23,7 @@
std::unique_ptr<SharedDesktopFrame> SharedDesktopFrame::Wrap(
std::unique_ptr<DesktopFrame> desktop_frame) {
return std::unique_ptr<SharedDesktopFrame>(
- new SharedDesktopFrame(new Core(desktop_frame.release())));
+ new SharedDesktopFrame(new Core(std::move(desktop_frame))));
}
SharedDesktopFrame* SharedDesktopFrame::Wrap(DesktopFrame* desktop_frame) {