ref_counted_ptr.h
Go to the documentation of this file.
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 
23 
24 #include <iosfwd>
25 #include <type_traits>
26 #include <utility>
27 
29 
30 namespace grpc_core {
31 
32 // A smart pointer class for objects that provide IncrementRefCount() and
33 // Unref() methods, such as those provided by the RefCounted base class.
34 template <typename T>
36  public:
38  // NOLINTNEXTLINE(google-explicit-constructor)
39  RefCountedPtr(std::nullptr_t) {}
40 
41  // If value is non-null, we take ownership of a ref to it.
42  template <typename Y>
43  explicit RefCountedPtr(Y* value) : value_(value) {}
44 
45  // Move ctors.
46  RefCountedPtr(RefCountedPtr&& other) noexcept {
47  value_ = other.value_;
48  other.value_ = nullptr;
49  }
50  template <typename Y>
51  // NOLINTNEXTLINE(google-explicit-constructor)
52  RefCountedPtr(RefCountedPtr<Y>&& other) noexcept {
53  value_ = static_cast<T*>(other.value_);
54  other.value_ = nullptr;
55  }
56 
57  // Move assignment.
58  RefCountedPtr& operator=(RefCountedPtr&& other) noexcept {
59  reset(other.value_);
60  other.value_ = nullptr;
61  return *this;
62  }
63  template <typename Y>
65  reset(other.value_);
66  other.value_ = nullptr;
67  return *this;
68  }
69 
70  // Copy ctors.
71  RefCountedPtr(const RefCountedPtr& other) {
72  if (other.value_ != nullptr) other.value_->IncrementRefCount();
73  value_ = other.value_;
74  }
75  template <typename Y>
76  // NOLINTNEXTLINE(google-explicit-constructor)
79  "T does not have a virtual dtor");
80  if (other.value_ != nullptr) other.value_->IncrementRefCount();
81  value_ = static_cast<T*>(other.value_);
82  }
83 
84  // Copy assignment.
85  // NOLINTNEXTLINE(bugprone-unhandled-self-assignment)
87  // Note: Order of reffing and unreffing is important here in case value_
88  // and other.value_ are the same object.
89  if (other.value_ != nullptr) other.value_->IncrementRefCount();
90  reset(other.value_);
91  return *this;
92  }
93  template <typename Y>
96  "T does not have a virtual dtor");
97  // Note: Order of reffing and unreffing is important here in case value_
98  // and other.value_ are the same object.
99  if (other.value_ != nullptr) other.value_->IncrementRefCount();
100  reset(other.value_);
101  return *this;
102  }
103 
105  if (value_ != nullptr) value_->Unref();
106  }
107 
108  void swap(RefCountedPtr& other) { std::swap(value_, other.value_); }
109 
110  // If value is non-null, we take ownership of a ref to it.
111  void reset(T* value = nullptr) {
112  if (value_ != nullptr) value_->Unref();
113  value_ = value;
114  }
115  void reset(const DebugLocation& location, const char* reason,
116  T* value = nullptr) {
117  if (value_ != nullptr) value_->Unref(location, reason);
118  value_ = value;
119  }
120  template <typename Y>
121  void reset(Y* value = nullptr) {
123  "T does not have a virtual dtor");
124  if (value_ != nullptr) value_->Unref();
125  value_ = static_cast<T*>(value);
126  }
127  template <typename Y>
128  void reset(const DebugLocation& location, const char* reason,
129  Y* value = nullptr) {
131  "T does not have a virtual dtor");
132  if (value_ != nullptr) value_->Unref(location, reason);
133  value_ = static_cast<T*>(value);
134  }
135 
136  // TODO(roth): This method exists solely as a transition mechanism to allow
137  // us to pass a ref to idiomatic C code that does not use RefCountedPtr<>.
138  // Once all of our code has been converted to idiomatic C++, this
139  // method should go away.
140  T* release() {
141  T* value = value_;
142  value_ = nullptr;
143  return value;
144  }
145 
146  T* get() const { return value_; }
147 
148  T& operator*() const { return *value_; }
149  T* operator->() const { return value_; }
150 
151  template <typename Y>
152  bool operator==(const RefCountedPtr<Y>& other) const {
153  return value_ == other.value_;
154  }
155 
156  template <typename Y>
157  bool operator==(const Y* other) const {
158  return value_ == other;
159  }
160 
161  bool operator==(std::nullptr_t) const { return value_ == nullptr; }
162 
163  template <typename Y>
164  bool operator!=(const RefCountedPtr<Y>& other) const {
165  return value_ != other.value_;
166  }
167 
168  template <typename Y>
169  bool operator!=(const Y* other) const {
170  return value_ != other;
171  }
172 
173  bool operator!=(std::nullptr_t) const { return value_ != nullptr; }
174 
175  private:
176  template <typename Y>
177  friend class RefCountedPtr;
178 
179  T* value_ = nullptr;
180 };
181 
182 // A smart pointer class for objects that provide IncrementWeakRefCount() and
183 // WeakUnref() methods, such as those provided by the DualRefCounted base class.
184 template <typename T>
186  public:
188  // NOLINTNEXTLINE(google-explicit-constructor)
189  WeakRefCountedPtr(std::nullptr_t) {}
190 
191  // If value is non-null, we take ownership of a ref to it.
192  template <typename Y>
193  explicit WeakRefCountedPtr(Y* value) {
194  value_ = value;
195  }
196 
197  // Move ctors.
199  value_ = other.value_;
200  other.value_ = nullptr;
201  }
202  template <typename Y>
203  // NOLINTNEXTLINE(google-explicit-constructor)
205  value_ = static_cast<T*>(other.value_);
206  other.value_ = nullptr;
207  }
208 
209  // Move assignment.
211  reset(other.value_);
212  other.value_ = nullptr;
213  return *this;
214  }
215  template <typename Y>
217  reset(other.value_);
218  other.value_ = nullptr;
219  return *this;
220  }
221 
222  // Copy ctors.
224  if (other.value_ != nullptr) other.value_->IncrementWeakRefCount();
225  value_ = other.value_;
226  }
227  template <typename Y>
228  // NOLINTNEXTLINE(google-explicit-constructor)
231  "T does not have a virtual dtor");
232  if (other.value_ != nullptr) other.value_->IncrementWeakRefCount();
233  value_ = static_cast<T*>(other.value_);
234  }
235 
236  // Copy assignment.
237  // NOLINTNEXTLINE(bugprone-unhandled-self-assignment)
239  // Note: Order of reffing and unreffing is important here in case value_
240  // and other.value_ are the same object.
241  if (other.value_ != nullptr) other.value_->IncrementWeakRefCount();
242  reset(other.value_);
243  return *this;
244  }
245  template <typename Y>
248  "T does not have a virtual dtor");
249  // Note: Order of reffing and unreffing is important here in case value_
250  // and other.value_ are the same object.
251  if (other.value_ != nullptr) other.value_->IncrementWeakRefCount();
252  reset(other.value_);
253  return *this;
254  }
255 
257  if (value_ != nullptr) value_->WeakUnref();
258  }
259 
260  void swap(WeakRefCountedPtr& other) { std::swap(value_, other.value_); }
261 
262  // If value is non-null, we take ownership of a ref to it.
263  void reset(T* value = nullptr) {
264  if (value_ != nullptr) value_->WeakUnref();
265  value_ = value;
266  }
267  void reset(const DebugLocation& location, const char* reason,
268  T* value = nullptr) {
269  if (value_ != nullptr) value_->WeakUnref(location, reason);
270  value_ = value;
271  }
272  template <typename Y>
273  void reset(Y* value = nullptr) {
275  "T does not have a virtual dtor");
276  if (value_ != nullptr) value_->WeakUnref();
277  value_ = static_cast<T*>(value);
278  }
279  template <typename Y>
280  void reset(const DebugLocation& location, const char* reason,
281  Y* value = nullptr) {
283  "T does not have a virtual dtor");
284  if (value_ != nullptr) value_->WeakUnref(location, reason);
285  value_ = static_cast<T*>(value);
286  }
287 
288  // TODO(roth): This method exists solely as a transition mechanism to allow
289  // us to pass a ref to idiomatic C code that does not use WeakRefCountedPtr<>.
290  // Once all of our code has been converted to idiomatic C++, this
291  // method should go away.
292  T* release() {
293  T* value = value_;
294  value_ = nullptr;
295  return value;
296  }
297 
298  T* get() const { return value_; }
299 
300  T& operator*() const { return *value_; }
301  T* operator->() const { return value_; }
302 
303  template <typename Y>
304  bool operator==(const WeakRefCountedPtr<Y>& other) const {
305  return value_ == other.value_;
306  }
307 
308  template <typename Y>
309  bool operator==(const Y* other) const {
310  return value_ == other;
311  }
312 
313  bool operator==(std::nullptr_t) const { return value_ == nullptr; }
314 
315  template <typename Y>
316  bool operator!=(const WeakRefCountedPtr<Y>& other) const {
317  return value_ != other.value_;
318  }
319 
320  template <typename Y>
321  bool operator!=(const Y* other) const {
322  return value_ != other;
323  }
324 
325  bool operator!=(std::nullptr_t) const { return value_ != nullptr; }
326 
327  private:
328  template <typename Y>
329  friend class WeakRefCountedPtr;
330 
331  T* value_ = nullptr;
332 };
333 
334 template <typename T, typename... Args>
336  return RefCountedPtr<T>(new T(std::forward<Args>(args)...));
337 }
338 
339 template <typename T>
340 bool operator<(const RefCountedPtr<T>& p1, const RefCountedPtr<T>& p2) {
341  return p1.get() < p2.get();
342 }
343 
344 template <typename T>
346  return p1.get() < p2.get();
347 }
348 
349 } // namespace grpc_core
350 
351 #endif /* GRPC_CORE_LIB_GPRPP_REF_COUNTED_PTR_H */
grpc_core::WeakRefCountedPtr::swap
void swap(WeakRefCountedPtr &other)
Definition: ref_counted_ptr.h:260
grpc_core::MakeRefCounted
RefCountedPtr< T > MakeRefCounted(Args &&... args)
Definition: ref_counted_ptr.h:335
grpc_core::RefCountedPtr::operator!=
bool operator!=(const Y *other) const
Definition: ref_counted_ptr.h:169
grpc_core::DebugLocation
Definition: debug_location.h:31
grpc_core::WeakRefCountedPtr::operator!=
bool operator!=(std::nullptr_t) const
Definition: ref_counted_ptr.h:325
grpc_core::RefCountedPtr::swap
void swap(RefCountedPtr &other)
Definition: ref_counted_ptr.h:108
grpc_core::RefCountedPtr::get
T * get() const
Definition: ref_counted_ptr.h:146
grpc_core::RefCountedPtr::~RefCountedPtr
~RefCountedPtr()
Definition: ref_counted_ptr.h:104
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::RefCountedPtr::reset
void reset(T *value=nullptr)
Definition: ref_counted_ptr.h:111
grpc_core::WeakRefCountedPtr::WeakRefCountedPtr
WeakRefCountedPtr(WeakRefCountedPtr< Y > &&other) noexcept
Definition: ref_counted_ptr.h:204
grpc_core::RefCountedPtr::operator==
bool operator==(const RefCountedPtr< Y > &other) const
Definition: ref_counted_ptr.h:152
grpc_core::WeakRefCountedPtr::operator=
WeakRefCountedPtr & operator=(WeakRefCountedPtr< Y > &&other) noexcept
Definition: ref_counted_ptr.h:216
grpc_core::WeakRefCountedPtr::operator!=
bool operator!=(const Y *other) const
Definition: ref_counted_ptr.h:321
grpc_core::RefCountedPtr::operator->
T * operator->() const
Definition: ref_counted_ptr.h:149
grpc_core::WeakRefCountedPtr::reset
void reset(Y *value=nullptr)
Definition: ref_counted_ptr.h:273
grpc_core::RefCountedPtr::release
T * release()
Definition: ref_counted_ptr.h:140
grpc_core::WeakRefCountedPtr::operator!=
bool operator!=(const WeakRefCountedPtr< Y > &other) const
Definition: ref_counted_ptr.h:316
grpc_core::RefCountedPtr::reset
void reset(Y *value=nullptr)
Definition: ref_counted_ptr.h:121
grpc_core::WeakRefCountedPtr::get
T * get() const
Definition: ref_counted_ptr.h:298
grpc_core::RefCountedPtr::operator=
RefCountedPtr & operator=(const RefCountedPtr< Y > &other)
Definition: ref_counted_ptr.h:94
T
#define T(upbtypeconst, upbtype, ctype, default_value)
grpc_core::RefCountedPtr::operator==
bool operator==(std::nullptr_t) const
Definition: ref_counted_ptr.h:161
grpc_core::RefCountedPtr::operator*
T & operator*() const
Definition: ref_counted_ptr.h:148
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_core::RefCountedPtr::RefCountedPtr
RefCountedPtr(std::nullptr_t)
Definition: ref_counted_ptr.h:39
grpc_core::RefCountedPtr::RefCountedPtr
RefCountedPtr(Y *value)
Definition: ref_counted_ptr.h:43
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
grpc_core::WeakRefCountedPtr::operator=
WeakRefCountedPtr & operator=(const WeakRefCountedPtr &other)
Definition: ref_counted_ptr.h:238
grpc_core::WeakRefCountedPtr::operator=
WeakRefCountedPtr & operator=(WeakRefCountedPtr &&other) noexcept
Definition: ref_counted_ptr.h:210
hpack_encoder_fixtures::Args
Args({0, 16384})
grpc_core::WeakRefCountedPtr::WeakRefCountedPtr
WeakRefCountedPtr(Y *value)
Definition: ref_counted_ptr.h:193
grpc_core::RefCountedPtr::operator!=
bool operator!=(std::nullptr_t) const
Definition: ref_counted_ptr.h:173
grpc_core::WeakRefCountedPtr::operator=
WeakRefCountedPtr & operator=(const WeakRefCountedPtr< Y > &other)
Definition: ref_counted_ptr.h:246
grpc_core::RefCountedPtr::operator!=
bool operator!=(const RefCountedPtr< Y > &other) const
Definition: ref_counted_ptr.h:164
grpc_core::WeakRefCountedPtr::release
T * release()
Definition: ref_counted_ptr.h:292
grpc_core::RefCountedPtr::operator=
RefCountedPtr & operator=(const RefCountedPtr &other)
Definition: ref_counted_ptr.h:86
grpc_core::WeakRefCountedPtr::~WeakRefCountedPtr
~WeakRefCountedPtr()
Definition: ref_counted_ptr.h:256
grpc_core::RefCountedPtr::RefCountedPtr
RefCountedPtr(RefCountedPtr< Y > &&other) noexcept
Definition: ref_counted_ptr.h:52
grpc_core::operator<
bool operator<(const RefCountedPtr< T > &p1, const RefCountedPtr< T > &p2)
Definition: ref_counted_ptr.h:340
std::swap
void swap(Json::Value &a, Json::Value &b)
Specialize std::swap() for Json::Value.
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:1226
grpc_core::WeakRefCountedPtr::WeakRefCountedPtr
WeakRefCountedPtr(const WeakRefCountedPtr &other)
Definition: ref_counted_ptr.h:223
grpc_core::RefCountedPtr::value_
T * value_
Definition: ref_counted_ptr.h:179
grpc_core::RefCountedPtr::operator=
RefCountedPtr & operator=(RefCountedPtr< Y > &&other) noexcept
Definition: ref_counted_ptr.h:64
grpc_core::WeakRefCountedPtr::WeakRefCountedPtr
WeakRefCountedPtr(WeakRefCountedPtr &&other) noexcept
Definition: ref_counted_ptr.h:198
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_core::WeakRefCountedPtr::WeakRefCountedPtr
WeakRefCountedPtr(std::nullptr_t)
Definition: ref_counted_ptr.h:189
grpc_core::RefCountedPtr::RefCountedPtr
RefCountedPtr(RefCountedPtr &&other) noexcept
Definition: ref_counted_ptr.h:46
grpc_core::RefCountedPtr::RefCountedPtr
RefCountedPtr()
Definition: ref_counted_ptr.h:37
grpc_core::RefCountedPtr::reset
void reset(const DebugLocation &location, const char *reason, T *value=nullptr)
Definition: ref_counted_ptr.h:115
grpc_core::WeakRefCountedPtr::reset
void reset(const DebugLocation &location, const char *reason, T *value=nullptr)
Definition: ref_counted_ptr.h:267
debug_location.h
grpc_core::RefCountedPtr::operator==
bool operator==(const Y *other) const
Definition: ref_counted_ptr.h:157
grpc_core::WeakRefCountedPtr::operator->
T * operator->() const
Definition: ref_counted_ptr.h:301
grpc_core::WeakRefCountedPtr
Definition: ref_counted_ptr.h:185
grpc_core::RefCountedPtr::RefCountedPtr
RefCountedPtr(const RefCountedPtr &other)
Definition: ref_counted_ptr.h:71
grpc_core::WeakRefCountedPtr::operator==
bool operator==(std::nullptr_t) const
Definition: ref_counted_ptr.h:313
grpc_core::RefCountedPtr::RefCountedPtr
RefCountedPtr(const RefCountedPtr< Y > &other)
Definition: ref_counted_ptr.h:77
grpc_core::RefCountedPtr::reset
void reset(const DebugLocation &location, const char *reason, Y *value=nullptr)
Definition: ref_counted_ptr.h:128
grpc_core::WeakRefCountedPtr::operator==
bool operator==(const WeakRefCountedPtr< Y > &other) const
Definition: ref_counted_ptr.h:304
grpc_core::WeakRefCountedPtr::operator*
T & operator*() const
Definition: ref_counted_ptr.h:300
grpc_core::RefCountedPtr::operator=
RefCountedPtr & operator=(RefCountedPtr &&other) noexcept
Definition: ref_counted_ptr.h:58
grpc_core::WeakRefCountedPtr::reset
void reset(const DebugLocation &location, const char *reason, Y *value=nullptr)
Definition: ref_counted_ptr.h:280
grpc_core::WeakRefCountedPtr::reset
void reset(T *value=nullptr)
Definition: ref_counted_ptr.h:263
grpc_core::WeakRefCountedPtr::value_
T * value_
Definition: ref_counted_ptr.h:331
grpc_core::WeakRefCountedPtr::WeakRefCountedPtr
WeakRefCountedPtr()
Definition: ref_counted_ptr.h:187
grpc_core::WeakRefCountedPtr::operator==
bool operator==(const Y *other) const
Definition: ref_counted_ptr.h:309
grpc_core::WeakRefCountedPtr::WeakRefCountedPtr
WeakRefCountedPtr(const WeakRefCountedPtr< Y > &other)
Definition: ref_counted_ptr.h:229
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:07