| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2017 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef GRPC_CORE_LIB_GPRPP_REF_COUNTED_PTR_H |
| 20 |
#define GRPC_CORE_LIB_GPRPP_REF_COUNTED_PTR_H |
| 21 |
|
| 22 |
#include <grpc/support/port_platform.h> |
| 23 |
|
| 24 |
#include <iosfwd> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
#include "src/core/lib/gprpp/debug_location.h" |
| 28 |
|
| 29 |
namespace grpc_core { |
| 30 |
|
| 31 |
// A smart pointer class for objects that provide IncrementRefCount() and |
| 32 |
// Unref() methods, such as those provided by the RefCounted base class. |
| 33 |
template <typename T> |
| 34 |
class RefCountedPtr { |
| 35 |
public: |
| 36 |
RefCountedPtr() {} |
| 37 |
// NOLINTNEXTLINE(google-explicit-constructor) |
| 38 |
RefCountedPtr(std::nullptr_t) {} |
| 39 |
|
| 40 |
// If value is non-null, we take ownership of a ref to it. |
| 41 |
template <typename Y> |
| 42 |
explicit RefCountedPtr(Y* value) : value_(value) {} |
| 43 |
|
| 44 |
// Move ctors. |
| 45 |
RefCountedPtr(RefCountedPtr&& other) noexcept { |
| 46 |
value_ = other.value_; |
| 47 |
other.value_ = nullptr; |
| 48 |
} |
| 49 |
template <typename Y> |
| 50 |
// NOLINTNEXTLINE(google-explicit-constructor) |
| 51 |
RefCountedPtr(RefCountedPtr<Y>&& other) noexcept { |
| 52 |
value_ = static_cast<T*>(other.value_); |
| 53 |
other.value_ = nullptr; |
| 54 |
} |
| 55 |
|
| 56 |
// Move assignment. |
| 57 |
RefCountedPtr& operator=(RefCountedPtr&& other) noexcept { |
| 58 |
reset(std::exchange(other.value_, nullptr)); |
| 59 |
return *this; |
| 60 |
} |
| 61 |
template <typename Y> |
| 62 |
RefCountedPtr& operator=(RefCountedPtr<Y>&& other) noexcept { |
| 63 |
reset(std::exchange(other.value_, nullptr)); |
| 64 |
return *this; |
| 65 |
} |
| 66 |
|
| 67 |
// Copy ctors. |
| 68 |
RefCountedPtr(const RefCountedPtr& other) { |
| 69 |
if (other.value_ != nullptr) other.value_->IncrementRefCount(); |
| 70 |
value_ = other.value_; |
| 71 |
} |
| 72 |
template <typename Y> |
| 73 |
// NOLINTNEXTLINE(google-explicit-constructor) |
| 74 |
RefCountedPtr(const RefCountedPtr<Y>& other) { |
| 75 |
static_assert(std::has_virtual_destructor<T>::value, |
| 76 |
"T does not have a virtual dtor"); |
| 77 |
if (other.value_ != nullptr) other.value_->IncrementRefCount(); |
| 78 |
value_ = static_cast<T*>(other.value_); |
| 79 |
} |
| 80 |
|
| 81 |
// Copy assignment. |
| 82 |
// NOLINTNEXTLINE(bugprone-unhandled-self-assignment) |
| 83 |
RefCountedPtr& operator=(const RefCountedPtr& other) { |
| 84 |
// Note: Order of reffing and unreffing is important here in case value_ |
| 85 |
// and other.value_ are the same object. |
| 86 |
if (other.value_ != nullptr) other.value_->IncrementRefCount(); |
| 87 |
reset(other.value_); |
| 88 |
return *this; |
| 89 |
} |
| 90 |
template <typename Y> |
| 91 |
RefCountedPtr& operator=(const RefCountedPtr<Y>& other) { |
| 92 |
static_assert(std::has_virtual_destructor<T>::value, |
| 93 |
"T does not have a virtual dtor"); |
| 94 |
// Note: Order of reffing and unreffing is important here in case value_ |
| 95 |
// and other.value_ are the same object. |
| 96 |
if (other.value_ != nullptr) other.value_->IncrementRefCount(); |
| 97 |
reset(other.value_); |
| 98 |
return *this; |
| 99 |
} |
| 100 |
|
| 101 |
~RefCountedPtr() { |
| 102 |
if (value_ != nullptr) value_->Unref(); |
| 103 |
} |
| 104 |
|
| 105 |
void swap(RefCountedPtr& other) { std::swap(value_, other.value_); } |
| 106 |
|
| 107 |
// If value is non-null, we take ownership of a ref to it. |
| 108 |
void reset(T* value = nullptr) { |
| 109 |
T* old_value = std::exchange(value_, value); |
| 110 |
if (old_value != nullptr) old_value->Unref(); |
| 111 |
} |
| 112 |
void reset(const DebugLocation& location, const char* reason, |
| 113 |
T* value = nullptr) { |
| 114 |
T* old_value = std::exchange(value_, value); |
| 115 |
if (old_value != nullptr) old_value->Unref(location, reason); |
| 116 |
} |
| 117 |
template <typename Y> |
| 118 |
void reset(Y* value = nullptr) { |
| 119 |
static_assert(std::has_virtual_destructor<T>::value, |
| 120 |
"T does not have a virtual dtor"); |
| 121 |
reset(static_cast<T*>(value)); |
| 122 |
} |
| 123 |
template <typename Y> |
| 124 |
void reset(const DebugLocation& location, const char* reason, |
| 125 |
Y* value = nullptr) { |
| 126 |
static_assert(std::has_virtual_destructor<T>::value, |
| 127 |
"T does not have a virtual dtor"); |
| 128 |
reset(location, reason, static_cast<T*>(value)); |
| 129 |
} |
| 130 |
|
| 131 |
// This method is mostly useful for interoperating with C code. |
| 132 |
// Eventually use within core should be banned, except at the surface API |
| 133 |
// boundaries. |
| 134 |
T* release() { return std::exchange(value_, nullptr); } |
| 135 |
|
| 136 |
T* get() const { return value_; } |
| 137 |
|
| 138 |
T& operator*() const { return *value_; } |
| 139 |
T* operator->() const { return value_; } |
| 140 |
|
| 141 |
template <typename Y> |
| 142 |
bool operator==(const RefCountedPtr<Y>& other) const { |
| 143 |
return value_ == other.value_; |
| 144 |
} |
| 145 |
|
| 146 |
template <typename Y> |
| 147 |
bool operator==(const Y* other) const { |
| 148 |
return value_ == other; |
| 149 |
} |
| 150 |
|
| 151 |
bool operator==(std::nullptr_t) const { return value_ == nullptr; } |
| 152 |
|
| 153 |
template <typename Y> |
| 154 |
bool operator!=(const RefCountedPtr<Y>& other) const { |
| 155 |
return value_ != other.value_; |
| 156 |
} |
| 157 |
|
| 158 |
template <typename Y> |
| 159 |
bool operator!=(const Y* other) const { |
| 160 |
return value_ != other; |
| 161 |
} |
| 162 |
|
| 163 |
bool operator!=(std::nullptr_t) const { return value_ != nullptr; } |
| 164 |
|
| 165 |
private: |
| 166 |
template <typename Y> |
| 167 |
friend class RefCountedPtr; |
| 168 |
|
| 169 |
T* value_ = nullptr; |
| 170 |
}; |
| 171 |
|
| 172 |
// A smart pointer class for objects that provide IncrementWeakRefCount() and |
| 173 |
// WeakUnref() methods, such as those provided by the DualRefCounted base class. |
| 174 |
template <typename T> |
| 175 |
class WeakRefCountedPtr { |
| 176 |
public: |
| 177 |
WeakRefCountedPtr() {} |
| 178 |
// NOLINTNEXTLINE(google-explicit-constructor) |
| 179 |
WeakRefCountedPtr(std::nullptr_t) {} |
| 180 |
|
| 181 |
// If value is non-null, we take ownership of a ref to it. |
| 182 |
template <typename Y> |
| 183 |
explicit WeakRefCountedPtr(Y* value) { |
| 184 |
value_ = value; |
| 185 |
} |
| 186 |
|
| 187 |
// Move ctors. |
| 188 |
WeakRefCountedPtr(WeakRefCountedPtr&& other) noexcept { |
| 189 |
value_ = other.value_; |
| 190 |
other.value_ = nullptr; |
| 191 |
} |
| 192 |
template <typename Y> |
| 193 |
// NOLINTNEXTLINE(google-explicit-constructor) |
| 194 |
WeakRefCountedPtr(WeakRefCountedPtr<Y>&& other) noexcept { |
| 195 |
value_ = static_cast<T*>(other.value_); |
| 196 |
other.value_ = nullptr; |
| 197 |
} |
| 198 |
|
| 199 |
// Move assignment. |
| 200 |
WeakRefCountedPtr& operator=(WeakRefCountedPtr&& other) noexcept { |
| 201 |
reset(std::exchange(other.value_, nullptr)); |
| 202 |
return *this; |
| 203 |
} |
| 204 |
template <typename Y> |
| 205 |
WeakRefCountedPtr& operator=(WeakRefCountedPtr<Y>&& other) noexcept { |
| 206 |
reset(std::exchange(other.value_, nullptr)); |
| 207 |
return *this; |
| 208 |
} |
| 209 |
|
| 210 |
// Copy ctors. |
| 211 |
WeakRefCountedPtr(const WeakRefCountedPtr& other) { |
| 212 |
if (other.value_ != nullptr) other.value_->IncrementWeakRefCount(); |
| 213 |
value_ = other.value_; |
| 214 |
} |
| 215 |
template <typename Y> |
| 216 |
// NOLINTNEXTLINE(google-explicit-constructor) |
| 217 |
WeakRefCountedPtr(const WeakRefCountedPtr<Y>& other) { |
| 218 |
static_assert(std::has_virtual_destructor<T>::value, |
| 219 |
"T does not have a virtual dtor"); |
| 220 |
if (other.value_ != nullptr) other.value_->IncrementWeakRefCount(); |
| 221 |
value_ = static_cast<T*>(other.value_); |
| 222 |
} |
| 223 |
|
| 224 |
// Copy assignment. |
| 225 |
// NOLINTNEXTLINE(bugprone-unhandled-self-assignment) |
| 226 |
WeakRefCountedPtr& operator=(const WeakRefCountedPtr& other) { |
| 227 |
// Note: Order of reffing and unreffing is important here in case value_ |
| 228 |
// and other.value_ are the same object. |
| 229 |
if (other.value_ != nullptr) other.value_->IncrementWeakRefCount(); |
| 230 |
reset(other.value_); |
| 231 |
return *this; |
| 232 |
} |
| 233 |
template <typename Y> |
| 234 |
WeakRefCountedPtr& operator=(const WeakRefCountedPtr<Y>& other) { |
| 235 |
static_assert(std::has_virtual_destructor<T>::value, |
| 236 |
"T does not have a virtual dtor"); |
| 237 |
// Note: Order of reffing and unreffing is important here in case value_ |
| 238 |
// and other.value_ are the same object. |
| 239 |
if (other.value_ != nullptr) other.value_->IncrementWeakRefCount(); |
| 240 |
reset(other.value_); |
| 241 |
return *this; |
| 242 |
} |
| 243 |
|
| 244 |
~WeakRefCountedPtr() { |
| 245 |
if (value_ != nullptr) value_->WeakUnref(); |
| 246 |
} |
| 247 |
|
| 248 |
void swap(WeakRefCountedPtr& other) { std::swap(value_, other.value_); } |
| 249 |
|
| 250 |
// If value is non-null, we take ownership of a ref to it. |
| 251 |
void reset(T* value = nullptr) { |
| 252 |
T* old_value = std::exchange(value_, value); |
| 253 |
if (old_value != nullptr) old_value->WeakUnref(); |
| 254 |
} |
| 255 |
void reset(const DebugLocation& location, const char* reason, |
| 256 |
T* value = nullptr) { |
| 257 |
T* old_value = std::exchange(value_, value); |
| 258 |
if (old_value != nullptr) old_value->WeakUnref(location, reason); |
| 259 |
} |
| 260 |
template <typename Y> |
| 261 |
void reset(Y* value = nullptr) { |
| 262 |
static_assert(std::has_virtual_destructor<T>::value, |
| 263 |
"T does not have a virtual dtor"); |
| 264 |
reset(static_cast<T*>(value)); |
| 265 |
} |
| 266 |
template <typename Y> |
| 267 |
void reset(const DebugLocation& location, const char* reason, |
| 268 |
Y* value = nullptr) { |
| 269 |
static_assert(std::has_virtual_destructor<T>::value, |
| 270 |
"T does not have a virtual dtor"); |
| 271 |
reset(location, reason, static_cast<T*>(value)); |
| 272 |
} |
| 273 |
|
| 274 |
// TODO(roth): This method exists solely as a transition mechanism to allow |
| 275 |
// us to pass a ref to idiomatic C code that does not use WeakRefCountedPtr<>. |
| 276 |
// Once all of our code has been converted to idiomatic C++, this |
| 277 |
// method should go away. |
| 278 |
T* release() { |
| 279 |
T* value = value_; |
| 280 |
value_ = nullptr; |
| 281 |
return value; |
| 282 |
} |
| 283 |
|
| 284 |
T* get() const { return value_; } |
| 285 |
|
| 286 |
T& operator*() const { return *value_; } |
| 287 |
T* operator->() const { return value_; } |
| 288 |
|
| 289 |
template <typename Y> |
| 290 |
bool operator==(const WeakRefCountedPtr<Y>& other) const { |
| 291 |
return value_ == other.value_; |
| 292 |
} |
| 293 |
|
| 294 |
template <typename Y> |
| 295 |
bool operator==(const Y* other) const { |
| 296 |
return value_ == other; |
| 297 |
} |
| 298 |
|
| 299 |
bool operator==(std::nullptr_t) const { return value_ == nullptr; } |
| 300 |
|
| 301 |
template <typename Y> |
| 302 |
bool operator!=(const WeakRefCountedPtr<Y>& other) const { |
| 303 |
return value_ != other.value_; |
| 304 |
} |
| 305 |
|
| 306 |
template <typename Y> |
| 307 |
bool operator!=(const Y* other) const { |
| 308 |
return value_ != other; |
| 309 |
} |
| 310 |
|
| 311 |
bool operator!=(std::nullptr_t) const { return value_ != nullptr; } |
| 312 |
|
| 313 |
private: |
| 314 |
template <typename Y> |
| 315 |
friend class WeakRefCountedPtr; |
| 316 |
|
| 317 |
T* value_ = nullptr; |
| 318 |
}; |
| 319 |
|
| 320 |
template <typename T, typename... Args> |
| 321 |
inline RefCountedPtr<T> MakeRefCounted(Args&&... args) { |
| 322 |
return RefCountedPtr<T>(new T(std::forward<Args>(args)...)); |
| 323 |
} |
| 324 |
|
| 325 |
template <typename T> |
| 326 |
bool operator<(const RefCountedPtr<T>& p1, const RefCountedPtr<T>& p2) { |
| 327 |
return p1.get() < p2.get(); |
| 328 |
} |
| 329 |
|
| 330 |
template <typename T> |
| 331 |
bool operator<(const WeakRefCountedPtr<T>& p1, const WeakRefCountedPtr<T>& p2) { |
| 332 |
return p1.get() < p2.get(); |
| 333 |
} |
| 334 |
|
| 335 |
} // namespace grpc_core |
| 336 |
|
| 337 |
#endif /* GRPC_CORE_LIB_GPRPP_REF_COUNTED_PTR_H */ |