1#!/usr/bin/env python3 2 3# Copyright 2021 gRPC authors. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import sys 18 19 20# utility: print a big comment block into a set of files 21def put_banner(files, banner): 22 for f in files: 23 print('/*', file=f) 24 for line in banner: 25 print(' * %s' % line, file=f) 26 print(' */', file=f) 27 print('', file=f) 28 29 30with open('src/core/lib/promise/detail/switch.h', 'w') as H: 31 # copy-paste copyright notice from this file 32 with open(sys.argv[0]) as my_source: 33 copyright = [] 34 for line in my_source: 35 if line[0] != '#': 36 break 37 for line in my_source: 38 if line[0] == '#': 39 copyright.append(line) 40 break 41 for line in my_source: 42 if line[0] != '#': 43 break 44 copyright.append(line) 45 put_banner([H], [line[2:].rstrip() for line in copyright]) 46 47 put_banner([H], ["Automatically generated by %s" % sys.argv[0]]) 48 49 print("#ifndef GRPC_CORE_LIB_PROMISE_DETAIL_SWITCH_H", file=H) 50 print("#define GRPC_CORE_LIB_PROMISE_DETAIL_SWITCH_H", file=H) 51 print('', file=H) 52 print('#include <grpc/support/port_platform.h>', file=H) 53 print('', file=H) 54 print("#include <stdlib.h>", file=H) 55 print('', file=H) 56 print("namespace grpc_core {", file=H) 57 58 for n in range(1, 33): 59 print('', file=H) 60 print("template <typename R, %s> R Switch(char idx, %s) {" % ( 61 ", ".join("typename F%d" % i for i in range(0, n)), 62 ", ".join("F%d f%d" % (i, i) for i in range(0, n)), 63 ), 64 file=H) 65 print(" switch (idx) {", file=H) 66 for i in range(0, n): 67 print(" case %d: return f%d();" % (i, i), file=H) 68 print(" }", file=H) 69 print(" abort();", file=H) 70 print("}", file=H) 71 72 print('', file=H) 73 print("}", file=H) 74 print('', file=H) 75 print("#endif // GRPC_CORE_LIB_PROMISE_DETAIL_SWITCH_H", file=H) 76