MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Sidecar.cpp
Go to the documentation of this file.
1#include "Sidecar.hpp"
2
4
5namespace {
6
7 constexpr const char* stub_text = "#pragma once\n";
8
9 std::vector<unsigned> include_lines(const std::string& text, const std::string& quoted)
10 {
11 std::vector<unsigned> includes;
12 std::istringstream lines(text);
13 std::string line;
14 unsigned number = 0;
15 while (std::getline(lines, line)) {
16 ++number;
17 const auto first = line.find_first_not_of(" \t");
18 if (first == std::string::npos || line[first] != '#') {
19 continue;
20 }
21 if (line.find("include", first) != std::string::npos && line.find(quoted) != std::string::npos) {
22 includes.push_back(number);
23 }
24 }
25 return includes;
26 }
27
28}
29
30std::optional<std::string> read_text(const std::filesystem::path& path)
31{
32 std::ifstream stream(path, std::ios::binary);
33 if (!stream) {
34 return std::nullopt;
35 }
36 std::ostringstream content;
37 content << stream.rdbuf();
38 return content.str();
39}
40
41bool insert_include(const HeaderRequests& requests, bool& inserted, std::string& error)
42{
43 unsigned last_marker = 0;
44 for (const RecordRequest& record : requests.records) {
45 last_marker = std::max(last_marker, record.line);
46 }
47 return insert_include(requests.header, last_marker, ".describe.hpp", inserted, error);
48}
49
50bool insert_include(const std::filesystem::path& header, unsigned last_marker,
51 std::string_view suffix, bool& inserted, std::string& error)
52{
53 inserted = false;
54 const std::string file = sidecar_path(header, suffix).filename().string();
55 auto text = read_text(header);
56 if (!text) {
57 error = "cannot read " + header.string();
58 return false;
59 }
60 if (!include_lines(*text, file + "\"").empty()) {
61 return true;
62 }
63
64 const std::string newline = text->find("\r\n") != std::string::npos ? "\r\n" : "\n";
65 const std::string line = "#include \"" + file + "\"" + newline;
66
67 std::size_t offset = 0;
68 for (unsigned i = 0; i < last_marker && offset != std::string::npos; ++i) {
69 const auto next = text->find('\n', offset);
70 offset = next == std::string::npos ? std::string::npos : next + 1;
71 }
72 if (offset == std::string::npos) {
73 *text += newline + line;
74 } else {
75 text->insert(offset, line);
76 }
77
78 if (!write_if_changed(header, *text, error)) {
79 return false;
80 }
81 inserted = true;
82 return true;
83}
84
85std::filesystem::path sidecar_path(const std::filesystem::path& header)
86{
87 return sidecar_path(header, ".describe.hpp");
88}
89
90std::filesystem::path sidecar_path(const std::filesystem::path& header, std::string_view suffix)
91{
92 return header.parent_path() / (header.stem().string() + std::string(suffix));
93}
94
95std::string render_sidecar(const HeaderRequests& requests)
96{
97 std::vector<const RecordRequest*> records;
98 records.reserve(requests.records.size());
99 for (const auto& record : requests.records) {
100 records.push_back(&record);
101 }
102 std::ranges::sort(records, {}, &RecordRequest::name);
103
104 std::string out;
105 out += "#pragma once\n\n";
106 out += "#include \"MayaFlux/Transitive/Reflect/Describe.hpp\"\n\n";
107 out += "#ifndef MAYAFLUX_REFLECT_SCAN\n\n";
108 out += "namespace MayaFlux::Reflect::Generated {\n\n";
109 for (const RecordRequest* record : records) {
110 out += "template <>\n";
111 out += "struct Description<::" + record->name + "> {\n";
112 out += " static constexpr std::string_view name = \"" + record->name + "\";\n\n";
113 out += " static constexpr auto members()\n {\n return std::tuple {\n";
114 for (const std::string& member : record->members) {
115 out += " Member<&::";
116 out += record->name;
117 out += "::";
118 out += member;
119 out += "> { \"";
120 out += member;
121 out += "\" },\n";
122 }
123 out += " };\n }\n};\n\n";
124 }
125 out += "}\n\n#endif\n";
126 return out;
127}
128
129bool ensure_stub(const std::filesystem::path& path, std::string& error)
130{
131 if (std::filesystem::exists(path)) {
132 return true;
133 }
134 return write_if_changed(path, stub_text, error);
135}
136
137bool write_if_changed(const std::filesystem::path& path, const std::string& content, std::string& error)
138{
139 if (read_text(path) == std::optional<std::string>(content)) {
140 return true;
141 }
142 const std::filesystem::path staged = path.string() + ".tmp";
143 {
144 std::ofstream stream(staged, std::ios::binary | std::ios::trunc);
145 stream << content;
146 if (!stream) {
147 error = "cannot write " + staged.string();
148 return false;
149 }
150 }
151 std::error_code code;
152 std::filesystem::rename(staged, path, code);
153 if (code) {
154 std::filesystem::remove(staged, code);
155 error = "cannot replace " + path.string();
156 return false;
157 }
158 return true;
159}
160
161std::optional<std::string> check_include(const HeaderRequests& requests)
162{
163 unsigned last_marker = 0;
164 for (const RecordRequest& record : requests.records) {
165 last_marker = std::max(last_marker, record.line);
166 }
167 return check_include(requests.header, last_marker, ".describe.hpp");
168}
169
170std::optional<std::string> check_include(
171 const std::filesystem::path& header, unsigned last_marker, std::string_view suffix)
172{
173 const std::string file = sidecar_path(header, suffix).filename().string();
174 const std::string quoted = file + "\"";
175
176 const auto text = read_text(header);
177 if (!text) {
178 return header.string() + ": cannot read the header to check its sidecar include";
179 }
180
181 const std::vector<unsigned> includes = include_lines(*text, quoted);
182
183 const std::string where = header.string() + ": ";
184 if (includes.empty()) {
185 return where + "add #include \"" + file + "\" after line " + std::to_string(last_marker);
186 }
187 if (includes.size() > 1) {
188 return where + "#include \"" + file + "\" appears more than once";
189 }
190 if (includes.front() <= last_marker) {
191 return where + "#include \"" + file + "\" at line " + std::to_string(includes.front())
192 + " must come after line " + std::to_string(last_marker)
193 + ", the end of the namespace holding the last MF_DESCRIBE";
194 }
195 return std::nullopt;
196}
197
198}
Vruta::SoundRoutine line(float start_value, float end_value, float duration_seconds, uint32_t step_duration, bool restartable)
Creates a continuous interpolation generator between two values over time.
Definition Tasks.cpp:60
std::string render_sidecar(const HeaderRequests &requests)
Text of the sidecar for one header, with its records sorted by name.
Definition Sidecar.cpp:95
bool ensure_stub(const std::filesystem::path &path, std::string &error)
Writes an empty sidecar when none exists, so the include never fails a first scan.
Definition Sidecar.cpp:129
bool insert_include(const HeaderRequests &requests, bool &inserted, std::string &error)
Inserts the sidecar include on the line after the last marker when the header has none.
Definition Sidecar.cpp:41
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
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
bool write_if_changed(const std::filesystem::path &path, const std::string &content, std::string &error)
Replaces path with content only when the content differs.
Definition Sidecar.cpp:137
std::pair< std::shared_ptr< Kriya::BufferPipeline >, std::shared_ptr< Buffers::AudioBuffer > > record(const std::shared_ptr< Kakshya::DynamicSoundStream > &stream, uint32_t input_channel, double seconds, std::optional< uint32_t > monitor, uint32_t channel)
Record a live input channel into a stream for a number of seconds.
Definition Rigs.cpp:262
std::vector< RecordRequest > records
Definition Request.hpp:27
Every record described by the MF_DESCRIBE markers of one header.
Definition Request.hpp:25
One described record: its qualified name and the names of its public data members.
Definition Request.hpp:15