xref: /aosp_15_r20/external/grpc-grpc/tools/codegen/core/gen_if_list.py (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1#!/usr/bin/env python3
2
3# Copyright 2023 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        for line in banner:
24            print("// %s" % line, file=f)
25        print("", file=f)
26
27
28with open("src/core/lib/gprpp/if_list.h", "w") as H:
29    # copy-paste copyright notice from this file
30    with open(sys.argv[0]) as my_source:
31        copyright = []
32        for line in my_source:
33            if line[0] != "#":
34                break
35        for line in my_source:
36            if line[0] == "#":
37                copyright.append(line)
38                break
39        for line in my_source:
40            if line[0] != "#":
41                break
42            copyright.append(line)
43        put_banner([H], [line[2:].rstrip() for line in copyright])
44
45    put_banner([H], ["", "Automatically generated by %s" % sys.argv[0], ""])
46
47    print("#ifndef GRPC_CORE_LIB_GPRPP_IF_LIST_H", file=H)
48    print("#define GRPC_CORE_LIB_GPRPP_IF_LIST_H", file=H)
49    print("", file=H)
50    print("#include <grpc/support/port_platform.h>", file=H)
51    print("", file=H)
52    print("#include <stdlib.h>", file=H)
53    print("", file=H)
54    print("namespace grpc_core {", file=H)
55
56    for n in range(1, 64):
57        print("", file=H)
58        print(
59            "template <typename CheckArg, typename ActionArg, typename"
60            " ActionFail, %s, %s> auto IfList(CheckArg input, ActionArg"
61            " action_arg, ActionFail action_fail, %s, %s) {"
62            % (
63                ", ".join("typename Check%d" % (i) for i in range(0, n)),
64                ", ".join("typename Action%d" % (i) for i in range(0, n)),
65                ", ".join("Check%d check%d" % (i, i) for i in range(0, n)),
66                ", ".join("Action%d action%d" % (i, i) for i in range(0, n)),
67            ),
68            file=H,
69        )
70        for i in range(0, n):
71            print(
72                "  if (check%d(input)) return action%d(action_arg);" % (i, i),
73                file=H,
74            )
75        print("  return action_fail(action_arg);", file=H)
76        print("}", file=H)
77
78    print("", file=H)
79    print("}", file=H)
80    print("", file=H)
81    print("#endif // GRPC_CORE_LIB_GPRPP_IF_LIST_H", file=H)
82