| 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 <utility> |
| 21 |
|
| 22 |
#include <grpc/support/log.h> |
| 23 |
|
| 24 |
namespace grpc_core { |
| 25 |
|
| 26 |
// To avoid accidentally creating context types, we require an explicit |
| 27 |
// specialization of this template per context type. The specialization need |
| 28 |
// not contain any members, only exist. |
| 29 |
// The reason for avoiding this is that context types each use a thread local. |
| 30 |
template <typename T> |
| 31 |
struct ContextType; // IWYU pragma: keep |
| 32 |
|
| 33 |
namespace promise_detail { |
| 34 |
|
| 35 |
template <typename T> |
| 36 |
class Context : public ContextType<T> { |
| 37 |
public: |
| 38 |
explicit Context(T* p) : old_(current_) { current_ = p; } |
| 39 |
~Context() { current_ = old_; } |
| 40 |
Context(const Context&) = delete; |
| 41 |
Context& operator=(const Context&) = delete; |
| 42 |
|
| 43 |
static T* get() { return current_; } |
| 44 |
|
| 45 |
private: |
| 46 |
T* const old_; |
| 47 |
static thread_local T* current_; |
| 48 |
}; |
| 49 |
|
| 50 |
template <typename T> |
| 51 |
thread_local T* Context<T>::current_; |
| 52 |
|
| 53 |
template <typename T, typename F> |
| 54 |
class WithContext { |
| 55 |
public: |
| 56 |
WithContext(F f, T* context) : context_(context), f_(std::move(f)) {} |
| 57 |
|
| 58 |
decltype(std::declval<F>()()) operator()() { |
| 59 |
Context<T> ctx(context_); |
| 60 |
return f_(); |
| 61 |
} |
| 62 |
|
| 63 |
private: |
| 64 |
T* context_; |
| 65 |
F f_; |
| 66 |
}; |
| 67 |
|
| 68 |
} // namespace promise_detail |
| 69 |
|
| 70 |
// Return true if a context of type T is currently active. |
| 71 |
template <typename T> |
| 72 |
bool HasContext() { |
| 73 |
return promise_detail::Context<T>::get() != nullptr; |
| 74 |
} |
| 75 |
|
| 76 |
// Retrieve the current value of a context, or abort if the value is unset. |
| 77 |
template <typename T> |
| 78 |
T* GetContext() { |
| 79 |
auto* p = promise_detail::Context<T>::get(); |
| 80 |
GPR_ASSERT(p != nullptr); |
| 81 |
return p; |
| 82 |
} |
| 83 |
|
| 84 |
// Given a promise and a context, return a promise that has that context set. |
| 85 |
template <typename T, typename F> |
| 86 |
promise_detail::WithContext<T, F> WithContext(F f, T* context) { |
| 87 |
return promise_detail::WithContext<T, F>(std::move(f), context); |
| 88 |
} |
| 89 |
|
| 90 |
} // namespace grpc_core |
| 91 |
|
| 92 |
#endif // GRPC_CORE_LIB_PROMISE_CONTEXT_H |