ifw-daq  3.0.0-pre2
IFW Data Acquisition modules
keywordRule.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_dpm_libmerge
4  * @copyright ESO - European Southern Observatory
5  */
6 #ifndef DAQ_DPM_KEYWORD_RULE_HPP
7 #define DAQ_DPM_KEYWORD_RULE_HPP
8 #include <vector>
9 
10 #include <daq/dpm/keywordEx.hpp>
11 #include <daq/fits/keyword.hpp>
12 
13 namespace daq::dpm {
14 
15 /**
16  * Interface for keyword rule processors
17  */
19 public:
20  enum class DefaultRule {
21  /**
22  * Default rule is to keep all keywords (useful for in-place merge)
23  */
24  All,
25  /**
26  * Default is to keep only user-keywords.
27  */
28  User,
29 
30  /**
31  * None (to disable keyword copying)
32  */
33  None,
34  };
35  virtual fits::KeywordVector
36  Process(fits::KeywordVector const&, DefaultRule default_rule) const = 0;
37  virtual ~KeywordRuleProcessor() noexcept = default;
38 };
39 
41 public:
42  using Rule = std::function<fits::KeywordVector(fits::KeywordVector const&)>;
43 
45  virtual fits::KeywordVector
46  Process(fits::KeywordVector const& keywords, DefaultRule default_rule) const override;
47 
48  void AddRule(Rule rule) {
49  m_rules.emplace_back(std::move(rule));
50  }
51 
52 private:
53  std::vector<Rule> m_rules;
54 };
55 
56 template <class InputIt, class OutputIt>
57 constexpr OutputIt
58 Filter(InputIt first, InputIt last, OutputIt d_first, KeywordEx const& filter_ex) {
59  return std::copy_if(
60  first, last, d_first, [&filter_ex](auto const kw) { return KeywordMatch(kw, filter_ex); });
61 }
62 
63 template <class InputIt, class OutputIt>
64 OutputIt Transform(InputIt first,
65  InputIt last,
66  OutputIt d_first,
67  KeywordEx const& filter_ex,
68  std::regex const& regex,
69  char const* fmt) {
70  while (first != last) {
71  if (KeywordMatch(*first, filter_ex)) {
72  *d_first++ = KeywordTransform(*first, regex, fmt);
73  } else {
74  *d_first++ = *first;
75  }
76  ++first;
77  }
78  return d_first;
79 }
80 
81 } // namespace daq::dpm
82 #endif // #ifndef DAQ_DPM_KEYWORD_RULE_HPP
Create keyword expression that memoize the provided string pattern.
Definition: keywordEx.hpp:59
Interface for keyword rule processors.
Definition: keywordRule.hpp:18
@ None
None (to disable keyword copying)
@ User
Default is to keep only user-keywords.
@ All
Default rule is to keep all keywords (useful for in-place merge)
virtual ~KeywordRuleProcessor() noexcept=default
virtual fits::KeywordVector Process(fits::KeywordVector const &, DefaultRule default_rule) const =0
std::function< fits::KeywordVector(fits::KeywordVector const &)> Rule
Definition: keywordRule.hpp:42
Contains data structure for FITS keywords.
fits::KeywordVariant KeywordTransform(fits::KeywordVariant const &keyword, std::regex const &re, char const *fmt)
Transforms keyword name using regex.
Definition: keywordEx.cpp:164
bool KeywordMatch(fits::KeywordVariant const &keyword, KeywordEx const &ex)
Definition: keywordEx.cpp:150
constexpr OutputIt Filter(InputIt first, InputIt last, OutputIt d_first, KeywordEx const &filter_ex)
Definition: keywordRule.hpp:58
OutputIt Transform(InputIt first, InputIt last, OutputIt d_first, KeywordEx const &filter_ex, std::regex const &regex, char const *fmt)
Definition: keywordRule.hpp:64
std::vector< KeywordVariant > KeywordVector
Vector of keywords.
Definition: keyword.hpp:414