ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
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 <nlohmann/json.hpp>
17 
18 namespace daq::dpm {
19 
20 class SourceNotFound : public std::invalid_argument {
21  using std::invalid_argument::invalid_argument;
22 };
23 
24 /**
25  * Provides location of fits source file.
26  */
28 public:
29  struct SourceFile {
30  /**
31  * Named by user
32  */
33  std::string source_name;
34  /**
35  * [user@]host:/path/
36  */
37  std::string origin;
38 
39  friend bool operator==(SourceFile const& lhs, SourceFile const& rhs) noexcept {
40  return lhs.source_name == rhs.source_name && lhs.origin == rhs.origin;
41  }
42  friend bool operator<(SourceFile const& lhs, SourceFile const& rhs) noexcept {
43  return lhs.source_name < rhs.source_name ||
44  (lhs.source_name == rhs.source_name && lhs.origin < rhs.origin);
45  }
46  friend std::ostream& operator<<(std::ostream& os, SourceFile const& source);
47  };
48  using Mapping = std::map<SourceFile, std::string>;
49  /**
50  * Initialize resolver with no content.
51  */
52  SourceResolver() = default;
53  explicit SourceResolver(Mapping mapping);
54 
55  /**
56  * Adds @c path so it is resolved using @c source_name and @c @origin
57  *
58  * @param source_name Source name.
59  * @param origin Origin file location.
60  * @param path Local path to a file @a source_name and @a origin will be resolved to.
61  */
62  void Add(SourceFile const& source,
63  std::filesystem::path const& path);
64 
65  /**
66  * Resolves local file that was previously added with @a Add().
67  *
68  * @param source_name Source name.
69  * @param origin Origin file location.
70  * @return Local path to a file @a source_name and @a origin will be resolved to.
71  * @throw SourceNotFound if source cannot be resolved.
72  */
73  auto Resolve(SourceFile const& source) const
74  -> std::filesystem::path;
75 
76  /**
77  * Get native representation of source mapping for serialization.
78  */
79  auto GetMapping() const noexcept -> Mapping const&;
80  void SetMapping(Mapping mapping ) noexcept;
81 private:
82  Mapping m_mapping;
83 };
84 
85 // NOLINTNEXTLINE
86 void to_json(nlohmann::json& j, SourceResolver::Mapping const& p);
87 // NOLINTNEXTLINE
88 void from_json(nlohmann::json const& j, SourceResolver::Mapping& p);
89 
90 } // namespace daq::dpm
91 
92 #endif // DAQ_DPM_SOURCERESOLVER_HPP
daq::dpm::from_json
void from_json(nlohmann::json const &j, SourceResolver::Mapping &p)
Definition: sourceResolver.cpp:63
daq::dpm::SourceResolver::SourceFile::operator==
friend bool operator==(SourceFile const &lhs, SourceFile const &rhs) noexcept
Definition: sourceResolver.hpp:39
daq::dpm::SourceResolver::GetMapping
auto GetMapping() const noexcept -> Mapping const &
Get native representation of source mapping for serialization.
Definition: sourceResolver.cpp:42
daq::dpm::SourceResolver::Mapping
std::map< SourceFile, std::string > Mapping
Definition: sourceResolver.hpp:48
daq::dpm::SourceResolver::SourceFile::source_name
std::string source_name
Named by user.
Definition: sourceResolver.hpp:33
daq::dpm::to_json
void to_json(nlohmann::json &j, SourceResolver::Mapping const &p)
Definition: sourceResolver.cpp:53
daq::dpm::SourceResolver::Resolve
auto Resolve(SourceFile const &source) const -> std::filesystem::path
Resolves local file that was previously added with Add().
Definition: sourceResolver.cpp:30
daq::dpm::SourceNotFound
Definition: sourceResolver.hpp:20
daq::dpm
Definition: testDpSpec.cpp:16
daq::dpm::SourceResolver::SourceFile::operator<
friend bool operator<(SourceFile const &lhs, SourceFile const &rhs) noexcept
Definition: sourceResolver.hpp:42
daq::dpm::SourceResolver::Add
void Add(SourceFile const &source, std::filesystem::path const &path)
Adds path so it is resolved using source_name and @origin .
Definition: sourceResolver.cpp:25
daq::dpm::SourceResolver::SourceFile::origin
std::string origin
[user@]host:/path/
Definition: sourceResolver.hpp:37
daq::dpm::SourceResolver::SourceFile
Definition: sourceResolver.hpp:29
daq::dpm::SourceResolver
Provides location of fits source file.
Definition: sourceResolver.hpp:27
daq::dpm::SourceResolver::SourceFile::operator<<
friend std::ostream & operator<<(std::ostream &os, SourceFile const &source)
Definition: sourceResolver.cpp:17
daq::dpm::SourceResolver::SourceResolver
SourceResolver()=default
Initialize resolver with no content.
daq::dpm::SourceResolver::SetMapping
void SetMapping(Mapping mapping) noexcept
Definition: sourceResolver.cpp:46
daq::json
nlohmann::json json
Definition: json.cpp:20