MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TensorFormat.cpp
Go to the documentation of this file.
1#include "TensorFormat.hpp"
2
4
5namespace MayaFlux::IO {
6
8{
9 static TensorFormatRegistry registry;
10 return registry;
11}
12
14{
15 for (auto& existing : m_formats) {
16 if (existing.name == format.name) {
17 existing = std::move(format);
18 return;
19 }
20 }
21 m_formats.push_back(std::move(format));
22}
23
24const TensorFormat* TensorFormatRegistry::find(std::string_view name) const
25{
26 for (const auto& format : m_formats) {
27 if (format.name == name)
28 return &format;
29 }
30 return nullptr;
31}
32
33const TensorFormat* TensorFormatRegistry::by_extension(const std::string& filepath, bool need_writer) const
34{
35 const auto ext = Detail::lower_extension(filepath);
36 if (ext.empty())
37 return nullptr;
38
39 for (const auto& format : m_formats) {
40 if (need_writer && !format.make_writer)
41 continue;
42 if (std::ranges::find(format.extensions, ext) != format.extensions.end())
43 return &format;
44 }
45 return nullptr;
46}
47
48const TensorFormat* TensorFormatRegistry::detect(const std::string& filepath) const
49{
50 const auto path = FileReader::resolve_path(filepath);
51
52 if (const auto head = Detail::read_file_head(path, 4096)) {
53 for (const auto& format : m_formats) {
54 if (format.probe && format.make_reader && format.probe(head->bytes, head->file_size))
55 return &format;
56 }
57 }
58 return by_extension(path, false);
59}
60
61std::unique_ptr<TensorReader> TensorFormatRegistry::create_reader(
62 const std::string& filepath, std::string_view format) const
63{
64 const TensorFormat* chosen = format.empty() ? detect(filepath) : find(format);
65 return chosen && chosen->make_reader ? chosen->make_reader() : nullptr;
66}
67
68std::unique_ptr<TensorWriter> TensorFormatRegistry::create_writer(
69 const std::string& filepath, std::string_view format) const
70{
71 const TensorFormat* chosen = format.empty() ? by_extension(filepath, true) : find(format);
72 return chosen && chosen->make_writer ? chosen->make_writer() : nullptr;
73}
74
75std::vector<std::string> TensorFormatRegistry::format_names() const
76{
77 std::vector<std::string> names;
78 names.reserve(m_formats.size());
79 for (const auto& format : m_formats)
80 names.push_back(format.name);
81 return names;
82}
83
84}
Cycle Behavior: The for_cycles(N) configuration controls how many times the capture operation execute...
static std::string resolve_path(const std::string &filepath)
Resolve a filepath against the project source root if not found as-is.
const TensorFormat * detect(const std::string &filepath) const
Format recognised by content, then by extension, or nullptr.
std::unique_ptr< TensorWriter > create_writer(const std::string &filepath, std::string_view format={}) const
New writer for a destination path.
static TensorFormatRegistry & instance()
The process-wide registry.
const TensorFormat * find(std::string_view name) const
Format with this name, or nullptr.
std::unique_ptr< TensorReader > create_reader(const std::string &filepath, std::string_view format={}) const
New reader for a file.
std::vector< TensorFormat > m_formats
const TensorFormat * by_extension(const std::string &filepath, bool need_writer) const
std::vector< std::string > format_names() const
Names of all registered formats, in registration order.
void register_format(TensorFormat format)
Add a format, replacing any earlier one with the same name.
Registry that picks a tensor reader or writer by name, content or extension.
std::string lower_extension(const std::string &path)
Lowercase extension of a path without its dot.
std::optional< FileHead > read_file_head(const std::string &path, size_t limit)
Read up to limit leading bytes of a file, or std::nullopt if it cannot be opened.
std::function< std::unique_ptr< TensorReader >()> make_reader
std::function< std::unique_ptr< TensorWriter >()> make_writer
Everything the registry needs to know about one tensor file format.