ifw-daq  3.0.0-pre2
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 location;
38 
39  friend bool operator==(SourceFile const& lhs, SourceFile const& rhs) noexcept {
40  return lhs.source_name == rhs.source_name && lhs.location == rhs.location;
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.location < rhs.location);
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 location
57  *
58  * @param source Source file.
59  * @param path Local path to a file @a source_name and @a source will be resolved to.
60  */
61  void Add(SourceFile const& source,
62  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
72  -> std::filesystem::path;
73 
74  /**
75  * Get native representation of source mapping for serialization.
76  */
77  auto GetMapping() const noexcept -> Mapping const&;
78  void SetMapping(Mapping mapping ) noexcept;
79 private:
80  Mapping m_mapping;
81 };
82 
83 // NOLINTNEXTLINE
84 void to_json(nlohmann::json& j, SourceResolver::Mapping const& p);
85 // NOLINTNEXTLINE
86 void from_json(nlohmann::json const& j, SourceResolver::Mapping& p);
87 
88 } // namespace daq::dpm
89 
90 #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 std::ostream & operator<<(std::ostream &os, SourceFile const &source)
friend bool operator<(SourceFile const &lhs, SourceFile const &rhs) noexcept
std::string location
[user@]host:/path/
std::string source_name
Named by user.