MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SignatureEntries.cpp
Go to the documentation of this file.
2
3#include "Sidecar.hpp"
4
6
7namespace {
8
9 std::multimap<std::string, std::filesystem::path> headers_by_name(const std::filesystem::path& directory)
10 {
11 std::multimap<std::string, std::filesystem::path> found;
12 std::error_code code;
13 for (std::filesystem::recursive_directory_iterator it(directory, code), end; it != end; it.increment(code)) {
14 if (code) {
15 break;
16 }
17 if (it->is_regular_file(code) && it->path().extension() == ".hpp") {
18 found.emplace(it->path().filename().string(), it->path());
19 }
20 }
21 return found;
22 }
23
24 bool declares_class(const std::filesystem::path& header, const std::string& class_name)
25 {
26 const auto text = read_text(header);
27 if (!text) {
28 return false;
29 }
30 const std::regex declaration(
31 R"((?:^|\n)\s*(?:class|struct)\s+(?:\w+\s+)*)" + class_name + R"(\s*(?:final\s*)?(?::|\{))");
32 return std::regex_search(*text, declaration);
33 }
34
35}
36
37std::optional<std::vector<SignatureEntry>> read_signature_entries(
38 const std::filesystem::path& registry, const std::filesystem::path& source_root, std::string& error)
39{
40 std::ifstream stream(registry);
41 if (!stream) {
42 error = "cannot read " + registry.string();
43 return std::nullopt;
44 }
45
46 const std::regex entry(R"(^\s*([NWB])\‍((\w+),\s*(MayaFlux::[\w:]+)\)\s*\\?\s*$)");
47 std::vector<SignatureEntry> entries;
48 std::set<std::string> names;
49 std::string line;
50 while (std::getline(stream, line)) {
51 if (!line.empty() && line.back() == '\r') {
52 line.pop_back();
53 }
54 std::smatch match;
55 if (!std::regex_match(line, match, entry)) {
56 continue;
57 }
58 if (!names.insert(match[2].str()).second) {
59 error = "registry is empty or contains duplicate factory names: " + match[2].str();
60 return std::nullopt;
61 }
62 entries.push_back(SignatureEntry { .name = match[2].str(), .type_name = match[3].str(), .header = {} });
63 }
64 if (entries.empty()) {
65 error = "registry is empty or contains duplicate factory names";
66 return std::nullopt;
67 }
68
69 const auto headers = headers_by_name(source_root / "MayaFlux");
70 for (SignatureEntry& item : entries) {
71 const std::string class_name = item.type_name.substr(item.type_name.rfind("::") + 2);
72 std::vector<std::filesystem::path> matches;
73 const auto range = headers.equal_range(class_name + ".hpp");
74 for (auto it = range.first; it != range.second; ++it) {
75 if (declares_class(it->second, class_name)) {
76 matches.push_back(it->second);
77 }
78 }
79 if (matches.size() != 1) {
80 error = item.name + ": expected one defining class header, found " + std::to_string(matches.size());
81 return std::nullopt;
82 }
83 item.header = matches.front();
84 }
85 return entries;
86}
87
88}
std::optional< std::vector< SignatureEntry > > read_signature_entries(const std::filesystem::path &registry, const std::filesystem::path &source_root, std::string &error)
Reads the N, W and B registrations of the registry header and finds each class's header.
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
One class of the Creator registry: the method name, its qualified type and its header.