diff --git a/webrtc/BUILD.gn b/webrtc/BUILD.gn index 9f1fd0b172..1cdc94b89d 100644 --- a/webrtc/BUILD.gn +++ b/webrtc/BUILD.gn @@ -383,6 +383,7 @@ if (rtc_include_tests) { "base/rate_statistics_unittest.cc", "base/ratelimiter_unittest.cc", "base/ratetracker_unittest.cc", + "base/refcountedobject_unittest.cc", "base/rollingaccumulator_unittest.cc", "base/rtccertificate_unittest.cc", "base/rtccertificategenerator_unittest.cc", diff --git a/webrtc/base/BUILD.gn b/webrtc/base/BUILD.gn index aa62054ed6..908c7b77ca 100644 --- a/webrtc/base/BUILD.gn +++ b/webrtc/base/BUILD.gn @@ -148,6 +148,7 @@ rtc_static_library("rtc_base_approved") { "ratetracker.cc", "ratetracker.h", "refcount.h", + "refcountedobject.h", "safe_conversions.h", "safe_conversions_impl.h", "sanitizer.h", diff --git a/webrtc/base/refcount.h b/webrtc/base/refcount.h index 9e881c85bd..565ae495b2 100644 --- a/webrtc/base/refcount.h +++ b/webrtc/base/refcount.h @@ -7,14 +7,10 @@ * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ - #ifndef WEBRTC_BASE_REFCOUNT_H_ #define WEBRTC_BASE_REFCOUNT_H_ -#include -#include - -#include "webrtc/base/atomicops.h" +#include "webrtc/base/refcountedobject.h" namespace rtc { @@ -28,151 +24,6 @@ class RefCountInterface { virtual ~RefCountInterface() {} }; -template -class RefCountedObject : public T { - public: - RefCountedObject() {} - - template - explicit RefCountedObject(const P& p) : T(p) {} - - template - explicit RefCountedObject(P&& p) : T(std::move(p)) {} - - template - RefCountedObject(P1 p1, P2 p2) : T(p1, p2) {} - - template - RefCountedObject(P1 p1, P2 p2, P3 p3) : T(p1, p2, p3) {} - - template - RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4) : T(p1, p2, p3, p4) {} - - template - RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) : T(p1, p2, p3, p4, p5) {} - - template - RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) - : T(p1, p2, p3, p4, p5, p6) {} - - template - RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) - : T(p1, p2, p3, p4, p5, p6, p7) {} - - template - 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) {} - - template - 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 - 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 - 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 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_REFCOUNT_H_ diff --git a/webrtc/base/refcountedobject.h b/webrtc/base/refcountedobject.h new file mode 100644 index 0000000000..ee2ec10867 --- /dev/null +++ b/webrtc/base/refcountedobject.h @@ -0,0 +1,225 @@ +// This file was GENERATED by command: +// pump.py refcountedobject.h.pump +// DO NOT EDIT BY HAND!!! + +/* + * 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 + +#include "webrtc/base/atomicops.h" + +namespace rtc { + +template +class RefCountedObject : public T { + public: + RefCountedObject() {} + + template + explicit RefCountedObject(P0&& p0) : T(std::forward(p0)) {} + template + RefCountedObject(P0&& p0, P1&& p1) + : T(std::forward(p0), std::forward(p1)) {} + template + RefCountedObject(P0&& p0, P1&& p1, P2&& p2) + : T(std::forward(p0), std::forward(p1), std::forward(p2)) {} + template + RefCountedObject(P0&& p0, P1&& p1, P2&& p2, P3&& p3) + : T(std::forward(p0), + std::forward(p1), + std::forward(p2), + std::forward(p3)) {} + template + RefCountedObject(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4) + : T(std::forward(p0), + std::forward(p1), + std::forward(p2), + std::forward(p3), + std::forward(p4)) {} + template + RefCountedObject(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5) + : T(std::forward(p0), + std::forward(p1), + std::forward(p2), + std::forward(p3), + std::forward(p4), + std::forward(p5)) {} + template + RefCountedObject(P0&& p0, + P1&& p1, + P2&& p2, + P3&& p3, + P4&& p4, + P5&& p5, + P6&& p6) + : T(std::forward(p0), + std::forward(p1), + std::forward(p2), + std::forward(p3), + std::forward(p4), + std::forward(p5), + std::forward(p6)) {} + template + RefCountedObject(P0&& p0, + P1&& p1, + P2&& p2, + P3&& p3, + P4&& p4, + P5&& p5, + P6&& p6, + P7&& p7) + : T(std::forward(p0), + std::forward(p1), + std::forward(p2), + std::forward(p3), + std::forward(p4), + std::forward(p5), + std::forward(p6), + std::forward(p7)) {} + template + RefCountedObject(P0&& p0, + P1&& p1, + P2&& p2, + P3&& p3, + P4&& p4, + P5&& p5, + P6&& p6, + P7&& p7, + P8&& p8) + : T(std::forward(p0), + std::forward(p1), + std::forward(p2), + std::forward(p3), + std::forward(p4), + std::forward(p5), + std::forward(p6), + std::forward(p7), + std::forward(p8)) {} + template + RefCountedObject(P0&& p0, + P1&& p1, + P2&& p2, + P3&& p3, + P4&& p4, + P5&& p5, + P6&& p6, + P7&& p7, + P8&& p8, + P9&& p9) + : T(std::forward(p0), + std::forward(p1), + std::forward(p2), + std::forward(p3), + std::forward(p4), + std::forward(p5), + std::forward(p6), + std::forward(p7), + std::forward(p8), + std::forward(p9)) {} + template + RefCountedObject(P0&& p0, + P1&& p1, + P2&& p2, + P3&& p3, + P4&& p4, + P5&& p5, + P6&& p6, + P7&& p7, + P8&& p8, + P9&& p9, + P10&& p10) + : T(std::forward(p0), + std::forward(p1), + std::forward(p2), + std::forward(p3), + std::forward(p4), + std::forward(p5), + std::forward(p6), + std::forward(p7), + std::forward(p8), + std::forward(p9), + std::forward(p10)) {} + + 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_ diff --git a/webrtc/base/refcountedobject.h.pump b/webrtc/base/refcountedobject.h.pump new file mode 100644 index 0000000000..cbf54e5b58 --- /dev/null +++ b/webrtc/base/refcountedobject.h.pump @@ -0,0 +1,64 @@ +/* + * 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 + +#include "webrtc/base/atomicops.h" + +namespace rtc { + +template +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)]]) {} + ]] + + 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_ diff --git a/webrtc/base/refcountedobject_unittest.cc b/webrtc/base/refcountedobject_unittest.cc new file mode 100644 index 0000000000..3d8163f73e --- /dev/null +++ b/webrtc/base/refcountedobject_unittest.cc @@ -0,0 +1,91 @@ +/* + * 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. + */ + +#include + +#include "webrtc/base/gunit.h" +#include "webrtc/base/refcount.h" + +namespace rtc { + +namespace { + +class A { + public: + A() {} + + private: + RTC_DISALLOW_COPY_AND_ASSIGN(A); +}; + +class RefClass : public RefCountInterface { + public: + RefClass() {} + + protected: + virtual ~RefClass() {} +}; + +class RefClassWithRvalue : public RefCountInterface { + public: + explicit RefClassWithRvalue(std::unique_ptr a) : a_(std::move(a)) {} + + protected: + virtual ~RefClassWithRvalue() {} + + public: + std::unique_ptr a_; +}; + +class RefClassWithMixedValues : public RefCountInterface { + public: + RefClassWithMixedValues(std::unique_ptr a, int b, const std::string& c) + : a_(std::move(a)), b_(b), c_(c) {} + + protected: + virtual ~RefClassWithMixedValues() {} + + public: + std::unique_ptr a_; + int b_; + std::string c_; +}; + +} // namespace + +TEST(RefCountedObject, Basic) { + scoped_refptr> aref( + new RefCountedObject()); + EXPECT_TRUE(aref->HasOneRef()); + EXPECT_EQ(2, aref->AddRef()); + EXPECT_EQ(1, aref->Release()); +} + +TEST(RefCountedObject, SupportRValuesInCtor) { + std::unique_ptr a(new A()); + scoped_refptr ref( + new RefCountedObject(std::move(a))); + EXPECT_TRUE(ref->a_.get() != nullptr); + EXPECT_TRUE(a.get() == nullptr); +} + +TEST(RefCountedObject, SupportMixedTypesInCtor) { + std::unique_ptr a(new A()); + int b = 9; + std::string c = "hello"; + scoped_refptr ref( + new RefCountedObject(std::move(a), b, c)); + EXPECT_TRUE(ref->a_.get() != nullptr); + EXPECT_TRUE(a.get() == nullptr); + EXPECT_EQ(b, ref->b_); + EXPECT_EQ(c, ref->c_); +} + +} // namespace rtc diff --git a/webrtc/common_video/video_frame_buffer.cc b/webrtc/common_video/video_frame_buffer.cc index dbb2743db1..1d209370a6 100644 --- a/webrtc/common_video/video_frame_buffer.cc +++ b/webrtc/common_video/video_frame_buffer.cc @@ -7,11 +7,12 @@ * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ +#include "webrtc/common_video/include/video_frame_buffer.h" + +#include #include -#include "webrtc/common_video/include/video_frame_buffer.h" - #include "webrtc/base/checks.h" #include "webrtc/base/keep_ref_until_done.h" #include "libyuv/convert.h" diff --git a/webrtc/modules/desktop_capture/screen_capturer_differ_wrapper.cc b/webrtc/modules/desktop_capture/screen_capturer_differ_wrapper.cc index b0d2282e0d..6306d0290c 100644 --- a/webrtc/modules/desktop_capture/screen_capturer_differ_wrapper.cc +++ b/webrtc/modules/desktop_capture/screen_capturer_differ_wrapper.cc @@ -10,6 +10,8 @@ #include "webrtc/modules/desktop_capture/screen_capturer_differ_wrapper.h" +#include + #include #include