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