GRPC C++  1.39.1
orphanable.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_ORPHANABLE_H
20 #define GRPC_CORE_LIB_GPRPP_ORPHANABLE_H
21 
23 
24 #include <grpc/support/log.h>
25 #include <grpc/support/sync.h>
26 
27 #include <cinttypes>
28 #include <memory>
29 
34 
35 namespace grpc_core {
36 
37 // A base class for orphanable objects, which have one external owner
38 // but are not necessarily destroyed immediately when the external owner
39 // gives up ownership. Instead, the owner calls the object's Orphan()
40 // method, and the object then takes responsibility for its own cleanup
41 // and destruction.
42 class Orphanable {
43  public:
44  // Gives up ownership of the object. The implementation must arrange
45  // to eventually destroy the object without further interaction from the
46  // caller.
47  virtual void Orphan() = 0;
48 
49  // Not copyable or movable.
50  Orphanable(const Orphanable&) = delete;
51  Orphanable& operator=(const Orphanable&) = delete;
52 
53  protected:
55  virtual ~Orphanable() {}
56 };
57 
59  public:
60  template <typename T>
61  void operator()(T* p) {
62  p->Orphan();
63  }
64 };
65 
66 template <typename T, typename Deleter = OrphanableDelete>
67 using OrphanablePtr = std::unique_ptr<T, Deleter>;
68 
69 template <typename T, typename... Args>
70 inline OrphanablePtr<T> MakeOrphanable(Args&&... args) {
71  return OrphanablePtr<T>(new T(std::forward<Args>(args)...));
72 }
73 
74 // A type of Orphanable with internal ref-counting.
75 template <typename Child>
77  public:
78  // Not copyable nor movable.
81 
82  protected:
83  // Allow RefCountedPtr<> to access Unref() and IncrementRefCount().
84  template <typename T>
85  friend class RefCountedPtr;
86 
87  // Note: Tracing is a no-op on non-debug builds.
88  explicit InternallyRefCounted(const char* trace = nullptr,
89  intptr_t initial_refcount = 1)
90  : refs_(initial_refcount, trace) {}
91  ~InternallyRefCounted() override = default;
92 
94  IncrementRefCount();
95  return RefCountedPtr<Child>(static_cast<Child*>(this));
96  }
98  const char* reason) GRPC_MUST_USE_RESULT {
99  IncrementRefCount(location, reason);
100  return RefCountedPtr<Child>(static_cast<Child*>(this));
101  }
102 
103  void Unref() {
104  if (GPR_UNLIKELY(refs_.Unref())) {
105  delete this;
106  }
107  }
108  void Unref(const DebugLocation& location, const char* reason) {
109  if (GPR_UNLIKELY(refs_.Unref(location, reason))) {
110  delete this;
111  }
112  }
113 
114  private:
115  void IncrementRefCount() { refs_.Ref(); }
116  void IncrementRefCount(const DebugLocation& location, const char* reason) {
117  refs_.Ref(location, reason);
118  }
119 
120  grpc_core::RefCount refs_;
121 };
122 
123 } // namespace grpc_core
124 
125 #endif /* GRPC_CORE_LIB_GPRPP_ORPHANABLE_H */
Definition: debug_location.h:31
Definition: orphanable.h:76
void Unref()
Definition: orphanable.h:103
RefCountedPtr< Child > Ref(const DebugLocation &location, const char *reason) GRPC_MUST_USE_RESULT
Definition: orphanable.h:97
void Unref(const DebugLocation &location, const char *reason)
Definition: orphanable.h:108
RefCountedPtr< Child > Ref() GRPC_MUST_USE_RESULT
Definition: orphanable.h:93
InternallyRefCounted(const InternallyRefCounted &)=delete
~InternallyRefCounted() override=default
InternallyRefCounted & operator=(const InternallyRefCounted &)=delete
InternallyRefCounted(const char *trace=nullptr, intptr_t initial_refcount=1)
Definition: orphanable.h:88
Definition: orphanable.h:58
void operator()(T *p)
Definition: orphanable.h:61
Definition: orphanable.h:42
Orphanable & operator=(const Orphanable &)=delete
Orphanable()
Definition: orphanable.h:54
virtual ~Orphanable()
Definition: orphanable.h:55
virtual void Orphan()=0
Orphanable(const Orphanable &)=delete
Definition: ref_counted.h:47
void Ref(Value n=1)
Definition: ref_counted.h:71
bool Unref()
Definition: ref_counted.h:155
Definition: ref_counted_ptr.h:35
#define GPR_UNLIKELY(x)
Definition: port_platform.h:660
#define GRPC_MUST_USE_RESULT
Definition: port_platform.h:524
Round Robin Policy.
Definition: backend_metric.cc:26
OrphanablePtr< T > MakeOrphanable(Args &&... args)
Definition: orphanable.h:70
std::unique_ptr< T, Deleter > OrphanablePtr
Definition: orphanable.h:67