ifw-daq  3.0.0-pre2
IFW Data Acquisition modules
sourceResolver.cpp
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 Defines daq::dpm::SourceResolver.
9  */
11 
12 #include <fmt/format.h>
13 #include <fmt/ostream.h>
14 
15 namespace daq::dpm {
16 
17 std::ostream& operator<<(std::ostream& os, SourceResolver::SourceFile const& source) {
18  os << "source name: \"" << source.source_name << "\", location: \"" << source.location << '"';
19  return os;
20 }
21 
22 SourceResolver::SourceResolver(Mapping mapping) : m_mapping(std::move(mapping)) {
23 }
24 
26  std::filesystem::path const& path) {
27  m_mapping[source] = path.native();
28 }
29 
31  -> std::filesystem::path {
32  auto it = m_mapping.find(source);
33  if (it == m_mapping.end()) {
34  throw SourceNotFound(
35  fmt::format("Could not resolve local filename for source: '{}' and location: '{}'",
36  source.source_name,
37  source.location));
38  }
39  return std::filesystem::path(it->second);
40 }
41 
42 auto SourceResolver::GetMapping() const noexcept -> Mapping const& {
43  return m_mapping;
44 }
45 
46 void SourceResolver::SetMapping(Mapping mapping) noexcept {
47  m_mapping.swap(mapping);
48 }
49 
50 // NOLINTNEXTLINE
51 
52 // NOLINTNEXTLINE
53 void to_json(nlohmann::json& j, SourceResolver::Mapping const& p) {
54  j = nlohmann::json::array();
55  for (auto const& s : p) {
56  j.emplace_back(nlohmann::json{
57  {"sourceName", s.first.source_name},
58  {"location", s.first.location},
59  {"localPath", s.second}});
60  }
61 }
62 // NOLINTNEXTLINE
63 void from_json(nlohmann::json const& j, SourceResolver::Mapping& p) {
64  p.clear();
65  for (auto const& s : j) {
66  p.emplace(SourceResolver::SourceFile{s["sourceName"], s["location"]}, s["localPath"]);
67  }
68 }
69 
70 } // namespace daq::dpm
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)
std::ostream & operator<<(std::ostream &os, DaqController const &daq)
Definition: scheduler.cpp:59
void to_json(nlohmann::json &j, SourceResolver::Mapping const &p)
@ SourceNotFound
Definition: main.cpp:43
Declares daq::dpm::SourceResolver.
std::string location
[user@]host:/path/
std::string source_name
Named by user.