Files
platform-external-webrtc/rtc_base/memory/always_valid_pointer.h
Christoffer Jansson 25361a89dd Revert "Reland "Don't create PacketSocketFactory inside BasicPortAllocatorSession""
This reverts commit a8be79ce27f10a698bdb46e490c2bbcbb2300e52.

Reason for revert: Downstream projects were not fixed and I was to eager to reland this, sorry about this.

Original change's description:
> Reland "Don't create PacketSocketFactory inside BasicPortAllocatorSession"
>
> This is a reland of commit 7d4634cef76a1ac244d4b83faaf4c617bf236b71
>
> Original change's description:
> > Don't create PacketSocketFactory inside BasicPortAllocatorSession
> >
> > This extends AlwaysValidPointer to avoid creating a unique_ptr inside it.
> >
> > Bug: webrtc:13145
> > Change-Id: I73a4f18d0a7037b57f575b04b134e4f7eadceb79
> > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/263240
> > Reviewed-by: Jonas Oreland <jonaso@webrtc.org>
> > Commit-Queue: Daniel.L (Byoungchan) Lee <daniel.l@hpcnt.com>
> > Reviewed-by: Harald Alvestrand <hta@webrtc.org>
> > Cr-Commit-Position: refs/heads/main@{#37048}
>
> Bug: webrtc:13145
> Change-Id: I7d64c25b2942b392a1c35ff2fe1edc83d7b03746
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/264503
> Reviewed-by: Harald Alvestrand <hta@webrtc.org>
> Reviewed-by: Christoffer Jansson <jansson@webrtc.org>
> Commit-Queue: Christoffer Jansson <jansson@webrtc.org>
> Reviewed-by: Daniel.L (Byoungchan) Lee <daniel.l@hpcnt.com>
> Cr-Commit-Position: refs/heads/main@{#37088}

Bug: webrtc:13145
Change-Id: Ie7990bae9a7c864ffaa4eb5b637618caad509633
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/264823
Bot-Commit: rubber-stamper@appspot.gserviceaccount.com <rubber-stamper@appspot.gserviceaccount.com>
Commit-Queue: Christoffer Jansson <jansson@webrtc.org>
Owners-Override: Christoffer Jansson <jansson@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37089}
2022-06-02 09:45:16 +00:00

111 lines
4.0 KiB
C++

/*
* Copyright 2022 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.
*/
#ifndef RTC_BASE_MEMORY_ALWAYS_VALID_POINTER_H_
#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 = Interface>
class AlwaysValidPointer {
public:
explicit AlwaysValidPointer(Interface* pointer)
: owned_instance_(pointer ? nullptr : std::make_unique<Default>()),
pointer_(pointer ? pointer : owned_instance_.get()) {
RTC_DCHECK(pointer_);
}
template <typename Arg,
typename std::enable_if<!(std::is_invocable<Arg>::value),
bool>::type = true>
AlwaysValidPointer(Interface* pointer, Arg arg)
: owned_instance_(pointer ? nullptr
: std::make_unique<Default>(std::move(arg))),
pointer_(pointer ? pointer : owned_instance_.get()) {
RTC_DCHECK(pointer_);
}
// Multiple arguments
template <typename Arg1, typename... Args>
AlwaysValidPointer(Interface* pointer, Arg1 arg1, Args... args)
: owned_instance_(pointer
? nullptr
: std::make_unique<Default>(std::move(arg1),
std::move(args...))),
pointer_(pointer ? pointer : owned_instance_.get()) {
RTC_DCHECK(pointer_);
}
// Create a pointer by
// a) using |pointer|, without taking ownership
// b) calling |function| and taking ownership of the result
template <typename Func,
typename std::enable_if<std::is_invocable<Func>::value,
bool>::type = true>
AlwaysValidPointer(Interface* pointer, Func function)
: owned_instance_(pointer ? nullptr : function()),
pointer_(owned_instance_ ? owned_instance_.get() : pointer) {
RTC_DCHECK(pointer_);
}
// Create a pointer by
// a) taking over ownership of |instance|
// b) or fallback to |pointer|, without taking ownership.
// c) or Default.
AlwaysValidPointer(std::unique_ptr<Interface>&& instance, Interface* pointer)
: owned_instance_(
instance
? std::move(instance)
: (pointer == nullptr ? std::make_unique<Default>() : nullptr)),
pointer_(owned_instance_ ? owned_instance_.get() : pointer) {
RTC_DCHECK(pointer_);
}
// Create a pointer by
// a) taking over ownership of |instance|
// b) or fallback to |pointer|, without taking ownership.
// c) or Default (with forwarded args).
template <typename... Args>
AlwaysValidPointer(std::unique_ptr<Interface>&& instance,
Interface* pointer,
Args... args)
: owned_instance_(
instance ? std::move(instance)
: (pointer == nullptr
? std::make_unique<Default>(std::move(args...))
: nullptr)),
pointer_(owned_instance_ ? owned_instance_.get() : pointer) {
RTC_DCHECK(pointer_);
}
Interface* get() { return pointer_; }
Interface* operator->() { return pointer_; }
Interface& operator*() { return *pointer_; }
Interface* get() const { return pointer_; }
Interface* operator->() const { return pointer_; }
Interface& operator*() const { return *pointer_; }
private:
const std::unique_ptr<Interface> owned_instance_;
Interface* const pointer_;
};
} // namespace webrtc
#endif // RTC_BASE_MEMORY_ALWAYS_VALID_POINTER_H_