single_set_ptr.h
Go to the documentation of this file.
1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPC_CORE_LIB_GPRPP_SINGLE_SET_PTR_H
16 #define GRPC_CORE_LIB_GPRPP_SINGLE_SET_PTR_H
17 
19 
20 #include <atomic>
21 #include <memory>
22 
23 #include <grpc/support/log.h>
24 
25 namespace grpc_core {
26 
27 template <class T, class Deleter = std::default_delete<T>>
28 class SingleSetPtr {
29  public:
30  SingleSetPtr() = default;
31  explicit SingleSetPtr(T* p) : p_{p} {}
32  explicit SingleSetPtr(std::unique_ptr<T, Deleter> p) : p_{p.release()} {}
33  ~SingleSetPtr() { Delete(p_.load(std::memory_order_relaxed)); }
34 
35  SingleSetPtr(const SingleSetPtr&) = delete;
36  SingleSetPtr& operator=(const SingleSetPtr&) = delete;
37  SingleSetPtr(SingleSetPtr&& other) noexcept
38  : p_(other.p_.exchange(nullptr)) {}
39  SingleSetPtr& operator=(SingleSetPtr&& other) noexcept {
40  Set(other.p_.exchange(nullptr, std::memory_order_acq_rel));
41  return *this;
42  }
43 
44  // Set the pointer;
45  // if already set, return the pre-set value and delete ptr;
46  // if deleted, return nullptr and delete ptr.
47  T* Set(T* ptr) {
48  T* expected = nullptr;
49  if (!p_.compare_exchange_strong(expected, ptr, std::memory_order_acq_rel,
50  std::memory_order_acquire)) {
51  Delete(ptr);
52  return expected;
53  }
54  return ptr;
55  }
56 
57  // Set the pointer from a compatible unique_ptr - with the same caveats as
58  // above.
59  T* Set(std::unique_ptr<T, Deleter> ptr) { return Set(ptr.release()); }
60 
61  // Clear the pointer.
62  void Reset() { Delete(p_.exchange(nullptr, std::memory_order_acq_rel)); }
63 
64  bool is_set() const {
65  T* p = p_.load(std::memory_order_acquire);
66  return p != nullptr;
67  }
68 
69  T* operator->() const {
70  T* p = p_.load(std::memory_order_acquire);
71  GPR_DEBUG_ASSERT(p != nullptr);
72  return p;
73  }
74 
75  T& operator*() const { return *operator->(); }
76 
77  private:
78  static void Delete(T* p) {
79  if (p == nullptr) return;
80  Deleter()(p);
81  }
82  std::atomic<T*> p_{nullptr};
83 };
84 
85 } // namespace grpc_core
86 
87 #endif // GRPC_CORE_LIB_GPRPP_SINGLE_SET_PTR_H
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
grpc_core::SingleSetPtr::Set
T * Set(T *ptr)
Definition: single_set_ptr.h:47
log.h
grpc_core::SingleSetPtr::Reset
void Reset()
Definition: single_set_ptr.h:62
GPR_DEBUG_ASSERT
#define GPR_DEBUG_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:103
grpc_core::SingleSetPtr::Set
T * Set(std::unique_ptr< T, Deleter > ptr)
Definition: single_set_ptr.h:59
grpc_core
Definition: call_metric_recorder.h:31
T
#define T(upbtypeconst, upbtype, ctype, default_value)
grpc_core::SingleSetPtr::~SingleSetPtr
~SingleSetPtr()
Definition: single_set_ptr.h:33
grpc_core::SingleSetPtr::SingleSetPtr
SingleSetPtr(std::unique_ptr< T, Deleter > p)
Definition: single_set_ptr.h:32
grpc_core::SingleSetPtr
Definition: single_set_ptr.h:28
grpc_core::SingleSetPtr::SingleSetPtr
SingleSetPtr(T *p)
Definition: single_set_ptr.h:31
grpc_core::SingleSetPtr::operator->
T * operator->() const
Definition: single_set_ptr.h:69
grpc_core::SingleSetPtr::SingleSetPtr
SingleSetPtr(SingleSetPtr &&other) noexcept
Definition: single_set_ptr.h:37
grpc_core::SingleSetPtr::SingleSetPtr
SingleSetPtr()=default
grpc_core::SingleSetPtr::operator*
T & operator*() const
Definition: single_set_ptr.h:75
grpc_core::SingleSetPtr::operator=
SingleSetPtr & operator=(SingleSetPtr &&other) noexcept
Definition: single_set_ptr.h:39
grpc_core::SingleSetPtr::is_set
bool is_set() const
Definition: single_set_ptr.h:64
grpc_core::SingleSetPtr::p_
std::atomic< T * > p_
Definition: single_set_ptr.h:82
grpc_core::SingleSetPtr::operator=
SingleSetPtr & operator=(const SingleSetPtr &)=delete
port_platform.h
grpc_core::SingleSetPtr::Delete
static void Delete(T *p)
Definition: single_set_ptr.h:78


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