ifw-daq 3.1.0
IFW Data Acquisition modules
Loading...
Searching...
No Matches
sourceResolver.hpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup daq_dpm_libmerge
4 * @copyright (c) Copyright ESO 2022
5 * All Rights Reserved
6 * ESO (eso.org) is an Intergovernmental Organisation, and therefore special legal conditions apply.
7 *
8 * @brief Declares daq::dpm::SourceResolver.
9 */
10#ifndef DAQ_DPM_SOURCERESOLVER_HPP
11#define DAQ_DPM_SOURCERESOLVER_HPP
12
13#include <filesystem>
14#include <map>
15
16#include <fmt/ostream.h>
17#include <nlohmann/json.hpp>
18
19namespace daq::dpm {
20
21class SourceNotFound : public std::invalid_argument {
22 using std::invalid_argument::invalid_argument;
23};
24
25/**
26 * Provides location of fits source file.
27 */
29public:
30 struct SourceFile {
31 /**
32 * Named by user
33 */
34 std::string source_name;
35 /**
36 * [user@]host:/path/
37 */
38 std::string location;
39
40 friend bool operator==(SourceFile const& lhs, SourceFile const& rhs) noexcept {
41 return lhs.source_name == rhs.source_name && lhs.location == rhs.location;
42 }
43 friend bool operator<(SourceFile const& lhs, SourceFile const& rhs) noexcept {
44 return lhs.source_name < rhs.source_name ||
45 (lhs.source_name == rhs.source_name && lhs.location < rhs.location);
46 }
47 friend std::ostream& operator<<(std::ostream& os, SourceFile const& source);
48 };
49 using Mapping = std::map<SourceFile, std::string>;
50 /**
51 * Initialize resolver with no content.
52 */
53 SourceResolver() = default;
54 explicit SourceResolver(Mapping mapping);
55
56 /**
57 * Adds @c path so it is resolved using @c source_name and @c location
58 *
59 * @param source Source file.
60 * @param path Local path to a file @a source_name and @a source will be resolved to.
61 */
62 void Add(SourceFile const& source, std::filesystem::path const& path);
63
64 /**
65 * Resolves local file that was previously added with @a Add().
66 *
67 * @param source Source file.
68 * @return Local path to a file @a source_name and @a source resolved to.
69 * @throw SourceNotFound if source cannot be resolved.
70 */
71 auto Resolve(SourceFile const& source) const -> std::filesystem::path;
72
73 /**
74 * Get native representation of source mapping for serialization.
75 */
76 auto GetMapping() const noexcept -> Mapping const&;
77 void SetMapping(Mapping mapping) noexcept;
78
79private:
80 Mapping m_mapping;
81};
82
83// NOLINTNEXTLINE
84void to_json(nlohmann::json& j, SourceResolver::Mapping const& p);
85// NOLINTNEXTLINE
86void from_json(nlohmann::json const& j, SourceResolver::Mapping& p);
87
88} // namespace daq::dpm
89
90template <>
91struct fmt::formatter<daq::dpm::SourceResolver::SourceFile> : ostream_formatter {};
92
93#endif // DAQ_DPM_SOURCERESOLVER_HPP
Provides location of fits source file.
void SetMapping(Mapping mapping) noexcept
void Add(SourceFile const &source, std::filesystem::path const &path)
Adds path so it is resolved using source_name and location.
auto GetMapping() const noexcept -> Mapping const &
Get native representation of source mapping for serialization.
SourceResolver()=default
Initialize resolver with no content.
std::map< SourceFile, std::string > Mapping
auto Resolve(SourceFile const &source) const -> std::filesystem::path
Resolves local file that was previously added with Add().
void from_json(nlohmann::json const &j, SourceResolver::Mapping &p)
void to_json(nlohmann::json &j, SourceResolver::Mapping const &p)
friend bool operator==(SourceFile const &lhs, SourceFile const &rhs) noexcept
friend bool operator<(SourceFile const &lhs, SourceFile const &rhs) noexcept
friend std::ostream & operator<<(std::ostream &os, SourceFile const &source)
std::string location
[user@]host:/path/
std::string source_name
Named by user.