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


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:46