1 // Copyright 2024 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_SRC_CORE_LIB_PROMISE_DETAIL_PROMISE_VARIANT_H 16 #define GRPC_SRC_CORE_LIB_PROMISE_DETAIL_PROMISE_VARIANT_H 17 18 #include "absl/types/variant.h" 19 20 namespace grpc_core { 21 22 namespace promise_detail { 23 24 // Visitor function for PromiseVariant - calls the poll operator on the inner 25 // type 26 class PollVisitor { 27 public: 28 template <typename T> operator()29 auto operator()(T& x) { 30 return x(); 31 } 32 }; 33 34 // Helper type - given a variant V, provides the poll operator (which simply 35 // visits the inner type on the variant with PollVisitor) 36 template <typename V> 37 class PromiseVariant { 38 public: PromiseVariant(V variant)39 explicit PromiseVariant(V variant) : variant_(std::move(variant)) {} operator()40 auto operator()() { return absl::visit(PollVisitor(), variant_); } 41 42 private: 43 V variant_; 44 }; 45 46 } // namespace promise_detail 47 48 } // namespace grpc_core 49 50 #endif // GRPC_SRC_CORE_LIB_PROMISE_DETAIL_PROMISE_VARIANT_H 51