generated from CoolLibs/library-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
59 lines (52 loc) · 1.58 KB
/
generator.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
# ---HOW TO---
# Modify `ways_of_finding_to_string()`, then run this script.
# ------------
def ways_of_finding_to_string():
return [
["UseStd", "std::to_string(value)"],
["UseAdl", "to_string(value)"],
["UseMethod", "value.to_string()"],
["RangesImplementation", "internal::stringify__ranges<T>(value)"],
["OptionalLike",
'value ? "Some: " + Cool::stringify(*value) : "None"'],
# "UseOstream" must be after "OptionalLike" because we want pointers to use the optional one rather than ostream
["UseOstream", "(std::stringstream{} << value).str()"],
]
def all_ways_of_finding_to_string():
concepts = ""
implementations = ""
first = True
for [concept_name, implementation] in ways_of_finding_to_string():
concepts += f"""
template<typename T>
concept {concept_name} = requires(T value)
{{
{{{implementation}}} -> std::convertible_to<std::string>;
}};
"""
implementations += f"""
{"else " if not first else ""}if constexpr(internal::{concept_name}<T>)
{{
return {implementation};
}}"""
first = False
return f"""
namespace internal {{
{concepts}
}} // namespace internal
template<typename T>
auto stringify(const T& value) -> std::string
{{
{implementations}
else {{
return std::string{{"[Cool::stringify] ERROR: Couldn't find a to_string() function for this type: "}} + typeid(T).name();
}}
}}
"""
if __name__ == "__main__":
from tooling.generate_files import generate
generate(
files=[
all_ways_of_finding_to_string,
]
)