diff --git a/CMakeLists.txt b/CMakeLists.txt index a35b971bd1b..7bf8c70411f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1372,6 +1372,7 @@ if(gRPC_BUILD_TESTS) add_dependencies(buildtests_cxx streams_not_seen_test) add_dependencies(buildtests_cxx string_ref_test) add_dependencies(buildtests_cxx string_test) + add_dependencies(buildtests_cxx switch_test) add_dependencies(buildtests_cxx sync_test) add_dependencies(buildtests_cxx system_roots_test) add_dependencies(buildtests_cxx table_test) @@ -23770,6 +23771,41 @@ target_link_libraries(string_test ) +endif() +if(gRPC_BUILD_TESTS) + +add_executable(switch_test + test/core/promise/switch_test.cc +) +target_compile_features(switch_test PUBLIC cxx_std_14) +target_include_directories(switch_test + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/include + ${_gRPC_ADDRESS_SORTING_INCLUDE_DIR} + ${_gRPC_RE2_INCLUDE_DIR} + ${_gRPC_SSL_INCLUDE_DIR} + ${_gRPC_UPB_GENERATED_DIR} + ${_gRPC_UPB_GRPC_GENERATED_DIR} + ${_gRPC_UPB_INCLUDE_DIR} + ${_gRPC_XXHASH_INCLUDE_DIR} + ${_gRPC_ZLIB_INCLUDE_DIR} + third_party/googletest/googletest/include + third_party/googletest/googletest + third_party/googletest/googlemock/include + third_party/googletest/googlemock + ${_gRPC_PROTO_GENS_DIR} +) + +target_link_libraries(switch_test + ${_gRPC_ALLTARGETS_LIBRARIES} + gtest + absl::type_traits + absl::statusor + gpr +) + + endif() if(gRPC_BUILD_TESTS) diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index 2e7cd202f5c..cadf505d159 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -16279,6 +16279,24 @@ targets: - gtest - grpc_test_util uses_polling: false +- name: switch_test + gtest: true + build: test + language: c++ + headers: + - src/core/lib/promise/detail/promise_factory.h + - src/core/lib/promise/detail/promise_like.h + - src/core/lib/promise/if.h + - src/core/lib/promise/poll.h + - src/core/lib/promise/switch.h + src: + - test/core/promise/switch_test.cc + deps: + - gtest + - absl/meta:type_traits + - absl/status:statusor + - gpr + uses_polling: false - name: sync_test gtest: true build: test diff --git a/src/core/BUILD b/src/core/BUILD index 63b603d6bc6..70870a13eee 100644 --- a/src/core/BUILD +++ b/src/core/BUILD @@ -619,6 +619,17 @@ grpc_cc_library( ], ) +grpc_cc_library( + name = "switch", + language = "c++", + public_hdrs = ["lib/promise/switch.h"], + deps = [ + "if", + "promise_factory", + "//:gpr_platform", + ], +) + grpc_cc_library( name = "promise_status", external_deps = [ @@ -736,15 +747,6 @@ grpc_cc_library( ], ) -grpc_cc_library( - name = "switch", - language = "c++", - public_hdrs = [ - "lib/promise/detail/switch.h", - ], - deps = ["//:gpr_platform"], -) - grpc_cc_library( name = "basic_seq", language = "c++", diff --git a/src/core/lib/promise/detail/switch.h b/src/core/lib/promise/detail/switch.h deleted file mode 100644 index a823b260609..00000000000 --- a/src/core/lib/promise/detail/switch.h +++ /dev/null @@ -1,1455 +0,0 @@ -// -// Copyright 2021 gRPC authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -// -// Automatically generated by tools/codegen/core/gen_switch.py -// - -#ifndef GRPC_SRC_CORE_LIB_PROMISE_DETAIL_SWITCH_H -#define GRPC_SRC_CORE_LIB_PROMISE_DETAIL_SWITCH_H - -#include - -#include - -namespace grpc_core { - -template -R Switch(char idx, F0 f0) { - switch (idx) { - case 0: - return f0(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18, F19 f19) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - case 19: - return f19(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18, F19 f19, F20 f20) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - case 19: - return f19(); - case 20: - return f20(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18, F19 f19, F20 f20, F21 f21) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - case 19: - return f19(); - case 20: - return f20(); - case 21: - return f21(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18, F19 f19, F20 f20, F21 f21, F22 f22) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - case 19: - return f19(); - case 20: - return f20(); - case 21: - return f21(); - case 22: - return f22(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18, F19 f19, F20 f20, F21 f21, F22 f22, - F23 f23) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - case 19: - return f19(); - case 20: - return f20(); - case 21: - return f21(); - case 22: - return f22(); - case 23: - return f23(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18, F19 f19, F20 f20, F21 f21, F22 f22, F23 f23, - F24 f24) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - case 19: - return f19(); - case 20: - return f20(); - case 21: - return f21(); - case 22: - return f22(); - case 23: - return f23(); - case 24: - return f24(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18, F19 f19, F20 f20, F21 f21, F22 f22, F23 f23, - F24 f24, F25 f25) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - case 19: - return f19(); - case 20: - return f20(); - case 21: - return f21(); - case 22: - return f22(); - case 23: - return f23(); - case 24: - return f24(); - case 25: - return f25(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18, F19 f19, F20 f20, F21 f21, F22 f22, F23 f23, - F24 f24, F25 f25, F26 f26) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - case 19: - return f19(); - case 20: - return f20(); - case 21: - return f21(); - case 22: - return f22(); - case 23: - return f23(); - case 24: - return f24(); - case 25: - return f25(); - case 26: - return f26(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18, F19 f19, F20 f20, F21 f21, F22 f22, F23 f23, - F24 f24, F25 f25, F26 f26, F27 f27) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - case 19: - return f19(); - case 20: - return f20(); - case 21: - return f21(); - case 22: - return f22(); - case 23: - return f23(); - case 24: - return f24(); - case 25: - return f25(); - case 26: - return f26(); - case 27: - return f27(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18, F19 f19, F20 f20, F21 f21, F22 f22, F23 f23, - F24 f24, F25 f25, F26 f26, F27 f27, F28 f28) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - case 19: - return f19(); - case 20: - return f20(); - case 21: - return f21(); - case 22: - return f22(); - case 23: - return f23(); - case 24: - return f24(); - case 25: - return f25(); - case 26: - return f26(); - case 27: - return f27(); - case 28: - return f28(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18, F19 f19, F20 f20, F21 f21, F22 f22, F23 f23, - F24 f24, F25 f25, F26 f26, F27 f27, F28 f28, F29 f29) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - case 19: - return f19(); - case 20: - return f20(); - case 21: - return f21(); - case 22: - return f22(); - case 23: - return f23(); - case 24: - return f24(); - case 25: - return f25(); - case 26: - return f26(); - case 27: - return f27(); - case 28: - return f28(); - case 29: - return f29(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18, F19 f19, F20 f20, F21 f21, F22 f22, F23 f23, - F24 f24, F25 f25, F26 f26, F27 f27, F28 f28, F29 f29, F30 f30) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - case 19: - return f19(); - case 20: - return f20(); - case 21: - return f21(); - case 22: - return f22(); - case 23: - return f23(); - case 24: - return f24(); - case 25: - return f25(); - case 26: - return f26(); - case 27: - return f27(); - case 28: - return f28(); - case 29: - return f29(); - case 30: - return f30(); - } - abort(); -} - -template -R Switch(char idx, F0 f0, F1 f1, F2 f2, F3 f3, F4 f4, F5 f5, F6 f6, F7 f7, - F8 f8, F9 f9, F10 f10, F11 f11, F12 f12, F13 f13, F14 f14, F15 f15, - F16 f16, F17 f17, F18 f18, F19 f19, F20 f20, F21 f21, F22 f22, F23 f23, - F24 f24, F25 f25, F26 f26, F27 f27, F28 f28, F29 f29, F30 f30, - F31 f31) { - switch (idx) { - case 0: - return f0(); - case 1: - return f1(); - case 2: - return f2(); - case 3: - return f3(); - case 4: - return f4(); - case 5: - return f5(); - case 6: - return f6(); - case 7: - return f7(); - case 8: - return f8(); - case 9: - return f9(); - case 10: - return f10(); - case 11: - return f11(); - case 12: - return f12(); - case 13: - return f13(); - case 14: - return f14(); - case 15: - return f15(); - case 16: - return f16(); - case 17: - return f17(); - case 18: - return f18(); - case 19: - return f19(); - case 20: - return f20(); - case 21: - return f21(); - case 22: - return f22(); - case 23: - return f23(); - case 24: - return f24(); - case 25: - return f25(); - case 26: - return f26(); - case 27: - return f27(); - case 28: - return f28(); - case 29: - return f29(); - case 30: - return f30(); - case 31: - return f31(); - } - abort(); -} - -} // namespace grpc_core - -#endif // GRPC_SRC_CORE_LIB_PROMISE_DETAIL_SWITCH_H diff --git a/src/core/lib/promise/switch.h b/src/core/lib/promise/switch.h new file mode 100644 index 00000000000..1bb05507ef2 --- /dev/null +++ b/src/core/lib/promise/switch.h @@ -0,0 +1,72 @@ +// Copyright 2023 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef GRPC_SRC_CORE_LIB_PROMISE_SWITCH_H +#define GRPC_SRC_CORE_LIB_PROMISE_SWITCH_H + +#include + +#include +#include + +#include "src/core/lib/promise/detail/promise_factory.h" +#include "src/core/lib/promise/if.h" + +namespace grpc_core { + +namespace promise_detail { +template +struct Case { + D discriminator; + F factory; +}; + +template +struct Default { + F factory; +}; +} // namespace promise_detail + +template +auto Case(D discriminator, PromiseFactory f) { + return promise_detail::Case{discriminator, std::move(f)}; +} + +template +auto Default(PromiseFactory f) { + return promise_detail::Default{std::move(f)}; +} + +// Given a list of cases that result in promise factories, return a single +// promise chosen by the discriminator (the first argument of this function). +// e.g.: +// Switch(1, Case<1>([] { return 43; }), Case<2>([] { return 44; })) +// resolves to 43. +// TODO(ctiller): consider writing a code-generator like we do for seq/join +// so that this lowers into a C switch statement. +template +auto Switch(D, promise_detail::Default def) { + return promise_detail::OncePromiseFactory(std::move(def.factory)) + .Make(); +} + +template +auto Switch(D discriminator, promise_detail::Case c, Others... others) { + return If(discriminator == c.discriminator, std::move(c.factory), + Switch(discriminator, std::move(others)...)); +} + +} // namespace grpc_core + +#endif // GRPC_SRC_CORE_LIB_PROMISE_SWITCH_H diff --git a/test/core/promise/BUILD b/test/core/promise/BUILD index b4c4ce855c6..a035820d5a8 100644 --- a/test/core/promise/BUILD +++ b/test/core/promise/BUILD @@ -197,6 +197,17 @@ grpc_cc_test( deps = ["//src/core:if"], ) +grpc_cc_test( + name = "switch_test", + srcs = ["switch_test.cc"], + external_deps = ["gtest"], + language = "c++", + tags = ["promise_test"], + uses_event_engine = False, + uses_polling = False, + deps = ["//src/core:switch"], +) + grpc_cc_test( name = "loop_test", srcs = ["loop_test.cc"], diff --git a/test/core/promise/switch_test.cc b/test/core/promise/switch_test.cc new file mode 100644 index 00000000000..ed18c1113ed --- /dev/null +++ b/test/core/promise/switch_test.cc @@ -0,0 +1,69 @@ +// Copyright 2023 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/core/lib/promise/switch.h" + +#include "gtest/gtest.h" + +namespace grpc_core { + +TEST(SwitchTest, JustDefault) { + EXPECT_EQ(Switch(42, Default([] { return 1; }))(), Poll(1)); +} + +TEST(SwitchTest, ThreeCases) { + auto test_switch = [](int d) { + return Switch(d, Case(1, [] { return 25; }), Case(2, [] { return 95; }), + Case(3, [] { return 68; }), Default([] { return 52; })); + }; + EXPECT_EQ(test_switch(0)(), Poll(52)); + EXPECT_EQ(test_switch(1)(), Poll(25)); + EXPECT_EQ(test_switch(2)(), Poll(95)); + EXPECT_EQ(test_switch(3)(), Poll(68)); + EXPECT_EQ(test_switch(4)(), Poll(52)); +} + +TEST(SwitchTest, Pending) { + auto test_switch = [](int d) { + return Switch(d, Case(42, []() -> Poll { return Pending{}; }), + Case(1, [] { return 25; }), Case(2, [] { return 95; }), + Case(3, [] { return 68; }), Default([] { return 52; })); + }; + EXPECT_EQ(test_switch(0)(), Poll(52)); + EXPECT_EQ(test_switch(1)(), Poll(25)); + EXPECT_EQ(test_switch(2)(), Poll(95)); + EXPECT_EQ(test_switch(3)(), Poll(68)); + EXPECT_EQ(test_switch(4)(), Poll(52)); + EXPECT_EQ(test_switch(42)(), Poll(Pending{})); +} + +TEST(SwitchTest, ThreeCasesFromEnum) { + enum class X : uint8_t { A, B, C }; + + auto test_switch = [](X d) { + return Switch(d, Case(X::A, [] { return 25; }), + Case(X::B, [] { return 95; }), Case(X::C, [] { return 68; }), + Default([] { return 52; })); + }; + EXPECT_EQ(test_switch(X::A)(), Poll(25)); + EXPECT_EQ(test_switch(X::B)(), Poll(95)); + EXPECT_EQ(test_switch(X::C)(), Poll(68)); +} + +} // namespace grpc_core + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/tools/codegen/core/gen_switch.py b/tools/codegen/core/gen_switch.py deleted file mode 100755 index 80e53c6416d..00000000000 --- a/tools/codegen/core/gen_switch.py +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/env python3 - -# Copyright 2021 gRPC authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import sys - - -# utility: print a big comment block into a set of files -def put_banner(files, banner): - for f in files: - print("/*", file=f) - for line in banner: - print(" * %s" % line, file=f) - print(" */", file=f) - print("", file=f) - - -with open("src/core/lib/promise/detail/switch.h", "w") as H: - # copy-paste copyright notice from this file - with open(sys.argv[0]) as my_source: - copyright = [] - for line in my_source: - if line[0] != "#": - break - for line in my_source: - if line[0] == "#": - copyright.append(line) - break - for line in my_source: - if line[0] != "#": - break - copyright.append(line) - put_banner([H], [line[2:].rstrip() for line in copyright]) - - put_banner([H], ["Automatically generated by %s" % sys.argv[0]]) - - print("#ifndef GRPC_CORE_LIB_PROMISE_DETAIL_SWITCH_H", file=H) - print("#define GRPC_CORE_LIB_PROMISE_DETAIL_SWITCH_H", file=H) - print("", file=H) - print("#include ", file=H) - print("", file=H) - print("#include ", file=H) - print("", file=H) - print("namespace grpc_core {", file=H) - - for n in range(1, 33): - print("", file=H) - print( - "template R Switch(char idx, %s) {" - % ( - ", ".join("typename F%d" % i for i in range(0, n)), - ", ".join("F%d f%d" % (i, i) for i in range(0, n)), - ), - file=H, - ) - print(" switch (idx) {", file=H) - for i in range(0, n): - print(" case %d: return f%d();" % (i, i), file=H) - print(" }", file=H) - print(" abort();", file=H) - print("}", file=H) - - print("", file=H) - print("}", file=H) - print("", file=H) - print("#endif // GRPC_CORE_LIB_PROMISE_DETAIL_SWITCH_H", file=H) diff --git a/tools/run_tests/generated/tests.json b/tools/run_tests/generated/tests.json index 97e35cd758b..665d030a6fb 100644 --- a/tools/run_tests/generated/tests.json +++ b/tools/run_tests/generated/tests.json @@ -9925,6 +9925,30 @@ ], "uses_polling": false }, + { + "args": [], + "benchmark": false, + "ci_platforms": [ + "linux", + "mac", + "posix", + "windows" + ], + "cpu_cost": 1.0, + "exclude_configs": [], + "exclude_iomgrs": [], + "flaky": false, + "gtest": true, + "language": "c++", + "name": "switch_test", + "platforms": [ + "linux", + "mac", + "posix", + "windows" + ], + "uses_polling": false + }, { "args": [], "benchmark": false,