From e0e91a624aecba25909bed63878f8caf0ad010ae Mon Sep 17 00:00:00 2001 From: Jonas Oreland Date: Sat, 19 Mar 2022 00:29:48 +0100 Subject: [PATCH] AlwaysValidPointer - forwarding constructor and tests Bug: webrtc:10335 Change-Id: I1305ccf8d4f1b25f89fef7d26b090741d5f57f7e Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/256106 Reviewed-by: Harald Alvestrand Commit-Queue: Jonas Oreland Cr-Commit-Position: refs/heads/main@{#36264} --- rtc_base/memory/BUILD.gn | 3 ++ rtc_base/memory/always_valid_pointer.h | 13 ++++- .../memory/always_valid_pointer_unittest.cc | 49 +++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 rtc_base/memory/always_valid_pointer_unittest.cc diff --git a/rtc_base/memory/BUILD.gn b/rtc_base/memory/BUILD.gn index 583278ee6e..8965a2c702 100644 --- a/rtc_base/memory/BUILD.gn +++ b/rtc_base/memory/BUILD.gn @@ -45,10 +45,12 @@ rtc_library("unittests") { testonly = true sources = [ "aligned_malloc_unittest.cc", + "always_valid_pointer_unittest.cc", "fifo_buffer_unittest.cc", ] deps = [ ":aligned_malloc", + ":always_valid_pointer", ":fifo_buffer", "../../test:test_support", ] @@ -56,4 +58,5 @@ rtc_library("unittests") { rtc_source_set("always_valid_pointer") { sources = [ "always_valid_pointer.h" ] + deps = [ "..:checks" ] } diff --git a/rtc_base/memory/always_valid_pointer.h b/rtc_base/memory/always_valid_pointer.h index c73512d3c5..570ad938fd 100644 --- a/rtc_base/memory/always_valid_pointer.h +++ b/rtc_base/memory/always_valid_pointer.h @@ -11,13 +11,16 @@ #define RTC_BASE_MEMORY_ALWAYS_VALID_POINTER_H_ #include +#include + +#include "rtc_base/checks.h" namespace webrtc { // This template allows the instantiation of a pointer to Interface in such a // way that if it is passed a null pointer, an object of class Default will be // created, which will be deallocated when the pointer is deleted. -template +template class AlwaysValidPointer { public: explicit AlwaysValidPointer(Interface* pointer) @@ -26,6 +29,14 @@ class AlwaysValidPointer { RTC_DCHECK(pointer_); } + template + AlwaysValidPointer(Interface* pointer, Args... args) + : owned_instance_( + pointer ? nullptr : std::make_unique(std::move(args...))), + pointer_(pointer ? pointer : owned_instance_.get()) { + RTC_DCHECK(pointer_); + } + Interface* get() { return pointer_; } Interface* operator->() { return pointer_; } Interface& operator*() { return *pointer_; } diff --git a/rtc_base/memory/always_valid_pointer_unittest.cc b/rtc_base/memory/always_valid_pointer_unittest.cc new file mode 100644 index 0000000000..dbb0671e32 --- /dev/null +++ b/rtc_base/memory/always_valid_pointer_unittest.cc @@ -0,0 +1,49 @@ +/* + * Copyright 2004 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 "rtc_base/memory/always_valid_pointer.h" + +#include + +#include "test/gtest.h" + +namespace webrtc { + +TEST(AlwaysValidPointerTest, DefaultToEmptyValue) { + AlwaysValidPointer ptr(nullptr); + EXPECT_EQ(*ptr, ""); +} +TEST(AlwaysValidPointerTest, DefaultWithForwardedArgument) { + AlwaysValidPointer ptr(nullptr, "test"); + EXPECT_EQ(*ptr, "test"); +} +TEST(AlwaysValidPointerTest, DefaultToSubclass) { + struct A { + virtual ~A() {} + virtual int f() = 0; + }; + struct B : public A { + int b = 0; + explicit B(int val) : b(val) {} + virtual ~B() {} + int f() override { return b; } + }; + AlwaysValidPointer ptr(nullptr, 3); + EXPECT_EQ(ptr->f(), 3); + EXPECT_EQ((*ptr).f(), 3); + EXPECT_EQ(ptr.get()->f(), 3); +} +TEST(AlwaysValidPointerTest, NonDefaultValue) { + std::string str("keso"); + AlwaysValidPointer ptr(&str, "test"); + EXPECT_EQ(*ptr, "keso"); +} + +} // namespace webrtc