1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/*
* 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_;
};
// This class is similar to AlwaysValidPointer, but it does not create
// a default object and crashes if none of the input pointers are non-null.
template <typename Interface>
class AlwaysValidPointerNoDefault {
public:
explicit AlwaysValidPointerNoDefault(Interface* pointer) : pointer_(pointer) {
RTC_CHECK(pointer_);
}
// Create a pointer by
// a) taking over ownership of |instance|
// b) or fallback to |pointer|, without taking ownership.
// At least one of the arguments must be non-null.
explicit AlwaysValidPointerNoDefault(std::unique_ptr<Interface> instance,
Interface* pointer = nullptr)
: owned_instance_(std::move(instance)),
pointer_(owned_instance_ ? owned_instance_.get() : pointer) {
RTC_CHECK(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_;
};
template <typename T, typename U, typename V, typename W>
bool operator==(const AlwaysValidPointer<T, U>& a,
const AlwaysValidPointer<V, W>& b) {
return a.get() == b.get();
}
template <typename T, typename U, typename V, typename W>
bool operator!=(const AlwaysValidPointer<T, U>& a,
const AlwaysValidPointer<V, W>& b) {
return !(a == b);
}
template <typename T, typename U>
bool operator==(const AlwaysValidPointer<T, U>& a, std::nullptr_t) {
return a.get() == nullptr;
}
template <typename T, typename U>
bool operator!=(const AlwaysValidPointer<T, U>& a, std::nullptr_t) {
return !(a == nullptr);
}
template <typename T, typename U>
bool operator==(std::nullptr_t, const AlwaysValidPointer<T, U>& a) {
return a.get() == nullptr;
}
template <typename T, typename U>
bool operator!=(std::nullptr_t, const AlwaysValidPointer<T, U>& a) {
return !(a == nullptr);
}
template <typename T, typename U>
bool operator==(const AlwaysValidPointerNoDefault<T>& a,
const AlwaysValidPointerNoDefault<U>& b) {
return a.get() == b.get();
}
template <typename T, typename U>
bool operator!=(const AlwaysValidPointerNoDefault<T>& a,
const AlwaysValidPointerNoDefault<U>& b) {
return !(a == b);
}
template <typename T>
bool operator==(const AlwaysValidPointerNoDefault<T>& a, std::nullptr_t) {
return a.get() == nullptr;
}
template <typename T>
bool operator!=(const AlwaysValidPointerNoDefault<T>& a, std::nullptr_t) {
return !(a == nullptr);
}
template <typename T>
bool operator==(std::nullptr_t, const AlwaysValidPointerNoDefault<T>& a) {
return a.get() == nullptr;
}
template <typename T>
bool operator!=(std::nullptr_t, const AlwaysValidPointerNoDefault<T>& a) {
return !(a == nullptr);
}
// Comparison with raw pointer.
template <typename T, typename U, typename V>
bool operator==(const AlwaysValidPointer<T, U>& a, const V* b) {
return a.get() == b;
}
template <typename T, typename U, typename V>
bool operator!=(const AlwaysValidPointer<T, U>& a, const V* b) {
return !(a == b);
}
template <typename T, typename U, typename V>
bool operator==(const T* a, const AlwaysValidPointer<U, V>& b) {
return a == b.get();
}
template <typename T, typename U, typename V>
bool operator!=(const T* a, const AlwaysValidPointer<U, V>& b) {
return !(a == b);
}
template <typename T, typename U>
bool operator==(const AlwaysValidPointerNoDefault<T>& a, const U* b) {
return a.get() == b;
}
template <typename T, typename U>
bool operator!=(const AlwaysValidPointerNoDefault<T>& a, const U* b) {
return !(a == b);
}
template <typename T, typename U>
bool operator==(const T* a, const AlwaysValidPointerNoDefault<U>& b) {
return a == b.get();
}
template <typename T, typename U>
bool operator!=(const T* a, const AlwaysValidPointerNoDefault<U>& b) {
return !(a == b);
}
} // namespace webrtc
#endif // RTC_BASE_MEMORY_ALWAYS_VALID_POINTER_H_
|