gen_switch.py 2.5 KB

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