GRPC C++  1.81.0
call_context_registry.h
Go to the documentation of this file.
1 // Copyright 2026 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 GRPCPP_IMPL_CALL_CONTEXT_REGISTRY_H
16 #define GRPCPP_IMPL_CALL_CONTEXT_REGISTRY_H
17 
18 #include <cstdint>
19 #include <utility>
20 
21 namespace grpc_core {
22 class Arena;
23 } // namespace grpc_core
24 
25 namespace grpc {
26 namespace impl {
27 
28 // Must define Propagate and extend CallContextTypeBase
29 template <typename T>
31 
32 template <typename T>
34  public:
35  static uint16_t id();
36 
37  private:
38  static void Destroy(void* p) { delete static_cast<T*>(p); }
39 
40  static void Propagate(void* p, grpc_core::Arena* a) {
41  CallContextType<T>::Propagate(static_cast<T*>(p), a);
42  }
43 };
44 
46  public:
47  // An opaque type to be used in ClientContext.
48  using ElementList = void**;
49 
50  // Adds an element to elements.
51  template <typename T>
52  static void SetContext(T element, ElementList& elements) {
53  uint16_t id = CallContextType<T>::id();
54  if (elements == nullptr) {
55  uint16_t num_elements = Count();
56  elements = new void*[num_elements]();
57  }
58  DestroyElement(id, elements[id]);
59  elements[id] = new T(std::move(element));
60  }
61 
62  // Called when starting the C-core call.
63  // Passes ownership of all elements into the C-core arena.
64  // Deletes elements and resets it to nullptr.
65  static void Propagate(ElementList& elements, grpc_core::Arena* arena);
66 
67  // Called on ClientContext destruction. No-op if elements is already null.
68  // Otherwise, deletes the context elements and deletes elements.
69  static void Destroy(ElementList& elements);
70 
71  private:
72  static uint16_t Count();
73 
74  static void DestroyElement(uint16_t id, void* element);
75 
76  static uint16_t Register(void (*destroy)(void*),
77  void (*propagate)(void*, grpc_core::Arena*));
78 
79  template <typename U>
80  friend struct CallContextTypeBase;
81 };
82 
83 template <typename T>
85  static uint16_t id = CallContextRegistry::Register(
87  return id;
88 }
89 
90 } // namespace impl
91 } // namespace grpc
92 
93 #endif // GRPCPP_IMPL_CALL_CONTEXT_REGISTRY_H
grpc::impl::CallContextRegistry::Propagate
static void Propagate(ElementList &elements, grpc_core::Arena *arena)
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc_core
Definition: context_types.h:20
grpc::impl::CallContextRegistry::SetContext
static void SetContext(T element, ElementList &elements)
Definition: call_context_registry.h:52
grpc::impl::CallContextRegistry::Destroy
static void Destroy(ElementList &elements)
grpc::impl::CallContextRegistry::ElementList
void ** ElementList
Definition: call_context_registry.h:48
grpc::impl::CallContextTypeBase
Definition: call_context_registry.h:33
grpc::impl::CallContextTypeBase::id
static uint16_t id()
Definition: call_context_registry.h:84
grpc::impl::CallContextRegistry
Definition: call_context_registry.h:45
grpc::impl::CallContextType
Definition: call_context_registry.h:30