MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SignatureOutput.cpp
Go to the documentation of this file.
1#include "SignatureOutput.hpp"
2
4
5namespace {
6
7 std::string joined(const std::vector<std::string>& lines)
8 {
9 std::string out;
10 for (std::size_t i = 0; i < lines.size(); ++i) {
11 if (i != 0) {
12 out += '\n';
13 }
14 out += lines[i];
15 }
16 return out;
17 }
18
19 std::string include_line(const std::filesystem::path& header)
20 {
21 return "#include \"" + header.generic_string() + "\"";
22 }
23
24 std::string join_parameters(const ConstructorSignature& constructor, bool with_defaults)
25 {
26 std::string out;
27 for (std::size_t i = 0; i < constructor.parameters.size(); ++i) {
28 const SignatureParameter& parameter = constructor.parameters[i];
29 if (i != 0) {
30 out += ", ";
31 }
32 out += parameter.type + " " + parameter.name;
33 if (with_defaults && parameter.default_value) {
34 out += " = " + *parameter.default_value;
35 }
36 }
37 return out;
38 }
39
40 std::string join_arguments(const ConstructorSignature& constructor)
41 {
42 std::string out;
43 for (std::size_t i = 0; i < constructor.parameters.size(); ++i) {
44 const std::string& name = constructor.parameters[i].name;
45 if (i != 0) {
46 out += ", ";
47 }
48 out += "std::forward<decltype(" + name + ")>(" + name + ")";
49 }
50 return out;
51 }
52
53}
54
56 const std::vector<SignatureClass>& classes, const std::filesystem::path& source_root)
57{
58 std::set<std::filesystem::path> signature_headers;
59 std::vector<std::string> class_includes { "#pragma once", "" };
60 std::vector<std::string> declarations;
61 std::vector<std::string> definitions;
62
63 for (const SignatureClass& item : classes) {
64 if (item.abstract) {
65 continue;
66 }
67 signature_headers.insert(item.includes.begin(), item.includes.end());
68 class_includes.push_back(include_line(item.entry.header.lexically_relative(source_root)));
69
70 constexpr std::string_view root = "MayaFlux::";
71 const std::string relative = item.entry.type_name.starts_with(root)
72 ? item.entry.type_name.substr(root.size())
73 : item.entry.type_name;
74 const std::string result = "std::shared_ptr<" + relative + ">";
75 for (const ConstructorSignature& constructor : item.constructors) {
76 declarations.push_back(" " + result + " " + item.entry.name + "("
77 + join_parameters(constructor, true) + ");");
78 definitions.push_back(result + " Creator::" + item.entry.name + "(" + join_parameters(constructor, false) + ")");
79 definitions.emplace_back("{");
80 definitions.push_back(" auto obj = std::make_shared<" + item.entry.type_name + ">("
81 + join_arguments(constructor) + ");");
82 definitions.push_back(" MF_LIVE_EXPOSE_NAMED(\"" + item.entry.name + "\", obj);");
83 definitions.emplace_back(" return obj;");
84 definitions.emplace_back("}");
85 definitions.emplace_back("");
86 }
87 }
88
89 std::vector<std::string> includes { "#pragma once", "" };
90 for (const std::filesystem::path& header : signature_headers) {
91 includes.push_back(include_line(header));
92 }
93
94 return SignatureFiles {
95 .includes = joined(includes) + "\n",
96 .classes = joined(class_includes) + "\n",
97 .declarations = joined(declarations) + "\n",
98 .definitions = joined(definitions),
99 };
100}
101
102}
SignatureFiles render_signature_files(const std::vector< SignatureClass > &classes, const std::filesystem::path &source_root)
Renders the generated files for the non abstract classes of classes.
One public constructor a Creator method is generated from.
A registered class as the scan found it.
The four generated files of the Creator proxy, as text.
One constructor parameter: its fully qualified type, its name and its default argument text.