-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.py
executable file
·142 lines (113 loc) · 4.14 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
import argparse
import typing
def generate_concept(n: int, output_file: typing.TextIO):
output_file.write('template <typename T>\n')
output_file.write('concept Has' + str(n) + 'attr = \n')
if n != 0:
output_file.write('\tHas' + str(n - 1) + 'attr<T> &&\n')
output_file.write('\tAggregate<T> &&\n')
output_file.write('\trequires(T& t)\n')
output_file.write('\t{\n')
output_file.write('\t\t{\n')
output_file.write('\t\t\tT{ ')
for i in range(0, n):
if i != 0:
output_file.write(', ')
output_file.write('{}')
output_file.write(' }\n')
output_file.write('\t\t};\n')
output_file.write('\t};\n')
output_file.write('\n')
def generate_to_tuple(n: int, output_file: typing.TextIO):
output_file.write('template <Has' + str(n) + 'attr T>\n')
output_file.write('constexpr auto to_tuple_impl_cref(const T& t)\n')
output_file.write('{\n')
if n != 0:
output_file.write('\tconst auto& [')
for i in range(0, n):
if i != 0:
output_file.write(', ')
output_file.write('a' + str(i))
output_file.write('] = t;\n')
output_file.write('\treturn std::forward_as_tuple(')
for i in range(0, n):
if i != 0:
output_file.write(', ')
output_file.write('a' + str(i))
output_file.write(');\n')
output_file.write('}\n\n')
def generate_to_tuple_forwarding(n: int, output_file: typing.TextIO):
output_file.write('template <Has' + str(n) + 'attr T>\n')
output_file.write('constexpr auto to_tuple_impl_fref(T&& t)\n')
output_file.write('{\n')
if n != 0:
output_file.write('\tauto&& [')
for i in range(0, n):
if i != 0:
output_file.write(', ')
output_file.write('a' + str(i))
output_file.write('] = t;\n')
output_file.write('\treturn std::forward_as_tuple(')
for i in range(0, n):
if i != 0:
output_file.write(', ')
output_file.write('std::forward<decltype(a' + str(i) + ')>(a' + str(i) + ')')
output_file.write(');\n')
output_file.write('}\n\n')
def generate_to_tuple_ref(n: int, output_file: typing.TextIO):
output_file.write('template <Has' + str(n) + 'attr T>\n')
output_file.write('constexpr auto to_tuple_impl_ref(T& t)\n')
output_file.write('{\n')
if n != 0:
output_file.write('\tauto& [')
for i in range(0, n):
if i != 0:
output_file.write(', ')
output_file.write('a' + str(i))
output_file.write('] = t;\n')
output_file.write('\treturn std::forward_as_tuple(')
for i in range(0, n):
if i != 0:
output_file.write(', ')
output_file.write('a' + str(i))
output_file.write(');\n')
output_file.write('}\n\n')
def generate_get_tuple_size(n: int, output_file: typing.TextIO):
output_file.write('template <Has' + str(n) + 'attr T>\n')
output_file.write('constexpr std::size_t get_tuple_size_impl()\n')
output_file.write('{\n')
output_file.write('\treturn ' + str(n) + ';\n')
output_file.write('}\n\n')
def main():
parser = argparse.ArgumentParser(description='Generate stuff for aggreget_generated.hpp')
parser.add_argument('-n', '--nb', type=int, default=25, help='The max number of attributes handled')
parser.add_argument('-o', '--output', default='./src/aggreget/aggreget_generated.hpp', help='Path of the output file')
args = parser.parse_args()
output_file = open(args.output, "w")
output_file.write('#pragma once\n\n')
output_file.write('#include <tuple>\n\n')
output_file.write('#include "aggregate_concept.hpp"\n\n')
output_file.write('namespace aggreget\n')
output_file.write('{\n\n')
output_file.write('namespace details\n')
output_file.write('{\n\n')
output_file.write('/*\n** Concepts\n*/\n\n')
for i in range (0, args.nb):
generate_concept(i, output_file)
output_file.write('/*\n** ToTupleDetails constant ref\n*/\n\n')
for i in range (0, args.nb):
generate_to_tuple(i, output_file)
output_file.write('/*\n** ToTupleDetails forwarding ref\n*/\n\n')
for i in range (0, args.nb):
generate_to_tuple_forwarding(i, output_file)
output_file.write('/*\n** ToTupleDetails ref\n*/\n\n')
for i in range (0, args.nb):
generate_to_tuple_ref(i, output_file)
output_file.write('/*\n** GetTupleSizeDetails\n*/\n\n')
for i in range (0, args.nb):
generate_get_tuple_size(i, output_file)
output_file.write('\n}\n')
output_file.write('\n}\n')
if __name__ == "__main__":
main()