Loading...
Searching...
No Matches
input_format_registry.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * The input formats this build can read, and how a path is matched to one.
5 *
6 * Core seeds the registry with YAML and Quantum ESPRESSO inputs on first use.
7 * A build-time add-on registers its formats from its registration function,
8 * which every entry point runs before it reads input (io/addon_registration.hpp),
9 * so detection, `--format` and the usage text all see the same list.
10 */
11
12#include <filesystem>
13#include <memory>
14
18#include "utils/registry.hpp"
19
20namespace q3 {
21
22/// The registry every entry point reads input formats from.
24 static Registry<InputFormat> registry = [] {
26 seeded.add(std::make_unique<YAMLInputFormat>());
27 seeded.add(std::make_unique<QEInputFormat>());
28 return seeded;
29 }();
30 return registry;
31}
32
33/**
34 * The format that claims `path`, or nullptr when none does.
35 *
36 * Every format that recognises the path is a candidate, and the one with the
37 * highest detection_priority() wins. Ties go to the format registered first,
38 * so detection does not depend on anything but the registry's contents.
39 */
40inline const InputFormat* detect_input_format(const std::filesystem::path& path) {
41 const InputFormat* detected = nullptr;
42 for (const auto& format : input_format_registry().entries()) {
43 if (!format->recognizes(path))
44 continue;
45 if (detected == nullptr || format->detection_priority() > detected->detection_priority())
46 detected = format.get();
47 }
48 return detected;
49}
50
51/**
52 * The format whose input a run given no input path should read from
53 * `directory`, or nullptr when no format claims it.
54 */
55inline const InputFormat* format_claiming_directory(const std::filesystem::path& directory) {
56 for (const auto& format : input_format_registry().entries()) {
57 if (format->claims_working_directory(directory))
58 return format.get();
59 }
60 return nullptr;
61}
62
63} // namespace q3
One input format a run can be read from; see the file comment.
virtual int detection_priority() const =0
An ordered set of interchangeable implementations of Entry, found by name.
Definition registry.hpp:33
void add(std::unique_ptr< Entry > entry)
Add entry; throws std::logic_error when one of its names is already registered.
Definition registry.hpp:36
const InputFormat * detect_input_format(const std::filesystem::path &path)
const InputFormat * format_claiming_directory(const std::filesystem::path &directory)
Registry< InputFormat > & input_format_registry()
The registry every entry point reads input formats from.