MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
FunctionSidecar.cpp
Go to the documentation of this file.
1#include "FunctionSidecar.hpp"
2
3#include "Sidecar.hpp"
4
6
7std::string render_function_sidecar(const HeaderFunctions& functions)
8{
9 std::vector<const FunctionRequest*> sorted;
10 sorted.reserve(functions.functions.size());
11 for (const FunctionRequest& function : functions.functions) {
12 sorted.push_back(&function);
13 }
14 std::ranges::sort(sorted, [](const FunctionRequest* a, const FunctionRequest* b) {
15 return std::tie(a->name, a->expression) < std::tie(b->name, b->expression);
16 });
17
18 std::string out;
19 out += "#pragma once\n\n";
20 out += "#include \"MayaFlux/Transitive/Reflect/Describe.hpp\"\n\n";
21 out += "#ifndef MAYAFLUX_REFLECT_SCAN\n\n";
22 out += "namespace MayaFlux::Reflect::Generated {\n\n";
23 for (const FunctionRequest* function : sorted) {
24 out += "template <>\n";
25 out += "struct Signature<" + function->expression + "> {\n";
26 out += " static constexpr std::string_view name = \"" + function->name + "\";\n";
27 out += " static constexpr std::size_t required = " + std::to_string(function->required) + ";\n";
28 out += " static constexpr std::array<std::string_view, " + std::to_string(function->parameters.size())
29 + "> parameters {";
30 for (std::size_t i = 0; i < function->parameters.size(); ++i) {
31 out += i == 0 ? " " : ", ";
32 out += "\"" + function->parameters[i] + "\"";
33 }
34 out += function->parameters.empty() ? "};\n" : " };\n";
35 out += "\n template <std::size_t K, typename Values>\n";
36 out += " static decltype(auto) call(Values& values)\n {\n";
37 for (std::size_t count = function->required; count <= function->parameters.size(); ++count) {
38 out += count == function->required ? " if constexpr (K == " : " } else if constexpr (K == ";
39 out += std::to_string(count) + ") {\n";
40 out += " return " + function->callee + "(";
41 for (std::size_t i = 0; i < count; ++i) {
42 out += i == 0 ? "" : ", ";
43 out += "std::get<" + std::to_string(i) + ">(values)";
44 }
45 out += ");\n";
46 }
47 out += " } else {\n";
48 out += " static_assert(K != K, \"the sidecar has no call with that many arguments\");\n";
49 out += " }\n";
50 out += " }\n";
51 out += "};\n\n";
52 }
53 out += "}\n\n#endif\n";
54 return out;
55}
56
57unsigned last_function_line(const HeaderFunctions& functions)
58{
59 unsigned last = 0;
60 for (const FunctionRequest& function : functions.functions) {
61 last = std::max(last, function.line);
62 }
63 return last;
64}
65
66std::vector<std::string> check_function_sidecars(
67 const FunctionMap& requests,
68 const std::vector<std::filesystem::path>& headers,
69 const std::vector<std::filesystem::path>& existing)
70{
71 std::vector<std::string> problems;
72
73 for (const auto& header : headers) {
74 const auto found = requests.find(header);
75 if (found == requests.end()) {
76 problems.push_back(header.string() + ": contains MF_DESCRIBE_FUNCTION text but produced no request");
77 continue;
78 }
79
80 const std::filesystem::path sidecar = sidecar_path(header, function_sidecar_suffix);
81 const auto current = read_text(sidecar);
82 if (!current) {
83 problems.push_back(sidecar.string() + ": missing sidecar");
84 } else if (*current != render_function_sidecar(found->second)) {
85 problems.push_back(sidecar.string() + ": differs from what the generator would write");
86 }
87
88 if (const auto problem = check_include(header, last_function_line(found->second), function_sidecar_suffix)) {
89 problems.push_back(*problem);
90 }
91 }
92
93 for (const auto& sidecar : existing) {
94 const std::string name = sidecar.filename().string();
95 const std::string stem = name.substr(0, name.size() - function_sidecar_suffix.size());
96 const std::filesystem::path header = sidecar.parent_path() / (stem + ".hpp");
97 if (!requests.contains(header)) {
98 problems.push_back(sidecar.string() + ": orphaned, its header has no MF_DESCRIBE_FUNCTION request");
99 }
100 }
101 return problems;
102}
103
104}
Cycle Behavior: The for_cycles(N) configuration controls how many times the capture operation execute...
std::vector< std::string > check_function_sidecars(const FunctionMap &requests, const std::vector< std::filesystem::path > &headers, const std::vector< std::filesystem::path > &existing)
Compares the function sidecars on disk with what the generator would write, touching nothing.
std::optional< std::string > read_text(const std::filesystem::path &path)
The whole content of path, or nullopt when it cannot be read.
Definition Sidecar.cpp:30
std::filesystem::path sidecar_path(const std::filesystem::path &header)
Path of the sidecar for header: Name.hpp becomes Name.describe.hpp beside it.
Definition Sidecar.cpp:85
constexpr std::string_view function_sidecar_suffix
The suffix of a function sidecar: Name.hpp becomes Name.signatures.hpp beside it.
std::string render_function_sidecar(const HeaderFunctions &functions)
Text of the sidecar for one header, one Signature specialization per function, sorted by name.
std::map< std::filesystem::path, HeaderFunctions > FunctionMap
unsigned last_function_line(const HeaderFunctions &functions)
The line after which the sidecar include may appear: the last marker of the header.
std::optional< std::string > check_include(const HeaderRequests &requests)
Checks that the header includes its sidecar exactly once, after its last marker.
Definition Sidecar.cpp:161
One described function: how to name it as a template argument, and its parameter names.
std::vector< FunctionRequest > functions
Every function described by the MF_DESCRIBE_FUNCTION markers of one header.