grpc
src
core
lib
promise
core/lib/promise/context.h
Go to the documentation of this file.
1
// Copyright 2021 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_PROMISE_CONTEXT_H
16
#define GRPC_CORE_LIB_PROMISE_CONTEXT_H
17
18
#include <
grpc/support/port_platform.h
>
19
20
#include <type_traits>
21
#include <utility>
22
23
#include "
src/core/lib/gpr/tls.h
"
24
25
namespace
grpc_core
{
26
27
// To avoid accidentally creating context types, we require an explicit
28
// specialization of this template per context type. The specialization need
29
// not contain any members, only exist.
30
// The reason for avoiding this is that context types each use a thread local.
31
template
<
typename
T>
32
struct
ContextType
;
// IWYU pragma: keep
33
34
namespace
promise_detail {
35
36
template
<
typename
T>
37
class
Context
:
public
ContextType
<T> {
38
public
:
39
explicit
Context
(
T
* p) :
old_
(
current_
) {
current_
= p; }
40
~Context
() {
current_
=
old_
; }
41
Context
(
const
Context
&) =
delete
;
42
Context
&
operator=
(
const
Context
&) =
delete
;
43
44
static
T
*
get
() {
return
current_
; }
45
46
private
:
47
T
*
const
old_
;
48
static
GPR_THREAD_LOCAL
(
T
*)
current_
;
49
};
50
51
template
<
typename
T>
52
GPR_THREAD_LOCAL
(
T
*)
53
Context<T>::current_
;
54
55
template
<
typename
T,
typename
F>
56
class
WithContext
{
57
public
:
58
WithContext
(F f,
T
*
context
) :
context_
(
context
),
f_
(
std
::
move
(f)) {}
59
60
decltype(std::declval<F>()()) operator()() {
61
Context<T>
ctx
(
context_
);
62
return
f_
();
63
}
64
65
private
:
66
T
*
context_
;
67
F
f_
;
68
};
69
70
}
// namespace promise_detail
71
72
// Retrieve the current value of a context.
73
template
<
typename
T>
74
T
*
GetContext
() {
75
return
promise_detail::Context<T>::get
();
76
}
77
78
// Given a promise and a context, return a promise that has that context set.
79
template
<
typename
T,
typename
F>
80
promise_detail::WithContext<T, F>
WithContext
(F f,
T
*
context
) {
81
return
promise_detail::WithContext<T, F>
(f,
context
);
82
}
83
84
}
// namespace grpc_core
85
86
#endif // GRPC_CORE_LIB_PROMISE_CONTEXT_H
current_
Block * current_
Definition:
protobuf/src/google/protobuf/descriptor.cc:1035
grpc_core::promise_detail::Context::~Context
~Context()
Definition:
core/lib/promise/context.h:40
grpc_core::promise_detail::WithContext::f_
F f_
Definition:
core/lib/promise/context.h:67
grpc_core
Definition:
call_metric_recorder.h:31
ctx
static struct test_ctx ctx
Definition:
test-ipc-send-recv.c:65
grpc_core::GetContext
T * GetContext()
Definition:
core/lib/promise/context.h:74
T
#define T(upbtypeconst, upbtype, ctype, default_value)
grpc_core::promise_detail::Context
Definition:
core/lib/promise/context.h:37
grpc_core::ContextType
Definition:
core/lib/promise/context.h:32
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition:
abseil-cpp/absl/utility/utility.h:221
grpc_core::promise_detail::Context::Context
Context(T *p)
Definition:
core/lib/promise/context.h:39
grpc_core::promise_detail::Context::operator=
Context & operator=(const Context &)=delete
grpc_core::promise_detail::Context::GPR_THREAD_LOCAL
static GPR_THREAD_LOCAL(T *) current_
grpc_core::promise_detail::Context::get
static T * get()
Definition:
core/lib/promise/context.h:44
GPR_THREAD_LOCAL
#define GPR_THREAD_LOCAL(type)
Definition:
tls.h:151
F
#define F(b, c, d)
Definition:
md4.c:112
grpc_core::WithContext
promise_detail::WithContext< T, F > WithContext(F f, T *context)
Definition:
core/lib/promise/context.h:80
grpc_core::promise_detail::Context::old_
T *const old_
Definition:
core/lib/promise/context.h:47
grpc_core::promise_detail::WithContext
Definition:
core/lib/promise/context.h:56
grpc_core::promise_detail::WithContext::context_
T * context_
Definition:
core/lib/promise/context.h:66
std
Definition:
grpcpp/impl/codegen/async_unary_call.h:407
tls.h
grpc_core::promise_detail::WithContext::WithContext
WithContext(F f, T *context)
Definition:
core/lib/promise/context.h:58
context
grpc::ClientContext context
Definition:
istio_echo_server_lib.cc:61
port_platform.h
grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:02