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 <hta@webrtc.org> Commit-Queue: Jonas Oreland <jonaso@webrtc.org> Cr-Commit-Position: refs/heads/main@{#36264}
This commit is contained in:
parent
0627cb3920
commit
e0e91a624a
@ -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" ]
|
||||
}
|
||||
|
||||
@ -11,13 +11,16 @@
|
||||
#define RTC_BASE_MEMORY_ALWAYS_VALID_POINTER_H_
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#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 <typename Interface, typename Default>
|
||||
template <typename Interface, typename Default = Interface>
|
||||
class AlwaysValidPointer {
|
||||
public:
|
||||
explicit AlwaysValidPointer(Interface* pointer)
|
||||
@ -26,6 +29,14 @@ class AlwaysValidPointer {
|
||||
RTC_DCHECK(pointer_);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
AlwaysValidPointer(Interface* pointer, Args... args)
|
||||
: owned_instance_(
|
||||
pointer ? nullptr : std::make_unique<Default>(std::move(args...))),
|
||||
pointer_(pointer ? pointer : owned_instance_.get()) {
|
||||
RTC_DCHECK(pointer_);
|
||||
}
|
||||
|
||||
Interface* get() { return pointer_; }
|
||||
Interface* operator->() { return pointer_; }
|
||||
Interface& operator*() { return *pointer_; }
|
||||
|
||||
49
rtc_base/memory/always_valid_pointer_unittest.cc
Normal file
49
rtc_base/memory/always_valid_pointer_unittest.cc
Normal file
@ -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 <string>
|
||||
|
||||
#include "test/gtest.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
TEST(AlwaysValidPointerTest, DefaultToEmptyValue) {
|
||||
AlwaysValidPointer<std::string> ptr(nullptr);
|
||||
EXPECT_EQ(*ptr, "");
|
||||
}
|
||||
TEST(AlwaysValidPointerTest, DefaultWithForwardedArgument) {
|
||||
AlwaysValidPointer<std::string> 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<A, B> 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<std::string> ptr(&str, "test");
|
||||
EXPECT_EQ(*ptr, "keso");
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
Loading…
x
Reference in New Issue
Block a user