ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
keywordEx.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_EX_HPP
7 #define DAQ_DPM_KEYWORD_EX_HPP
8 #include <algorithm>
9 #include <regex>
10 #include <string>
11 #include <string_view>
12 #include <vector>
13 
14 #include <daq/fits/keyword.hpp>
15 
16 namespace daq::dpm {
17 namespace detail {
18 
19 /**
20  * Rule operator
21  */
22 enum class Operator : std::uint8_t { Include, Exclude };
23 
24 /**
25  * Rule scope.
26  */
27 enum class Scope : std::uint8_t { Any, Value, Eso, Commentary };
28 
29 /**
30  * Represents a keyword rule expression.
31  */
32 struct Rule {
35  std::string pattern;
36 };
37 
38 /**
39  * Parse expression of the form documented in KeywordEx
40  *
41  * @throw std::invalid_argument if expression @a ex is not valid.
42  */
43 Rule ParseEx(std::string_view ex);
44 
45 } // namespace detail
46 
47 /**
48  * Create keyword expression that memoize the provided string pattern.
49  *
50  * Each rule string is defined by:
51  *
52  * <rule> ::= <operator><scope> " " <pattern> | <operator> " " <pattern>
53  * <operator> ::= "+" | "-"
54  * <scope> ::= "v" | "e" | "c"
55  * <pattern> ::= <ascii-text> | <wildcard> | <pattern>
56  * <ascii-text> ::= [#x20 - #x7e]
57  * <wildcard> ::= "*" | "?" | "[" <ascii-text> "]"
58  */
59 class KeywordEx {
60 public:
61  KeywordEx() = default;
62 
63  /**
64  * Construct expresssion from single rule.
65  */
66  explicit KeywordEx(std::string_view rule);
67 
68  /**
69  * Construct from initilizer_list of c-strings.
70  *
71  * @param rules Initializer list of rules.
72  */
73  explicit KeywordEx(std::initializer_list<char const*> rules) {
74  std::transform(
75  std::begin(rules), std::end(rules), std::back_inserter(m_rules), [](auto&& value) {
76  return detail::ParseEx(std::string_view(value));
77  });
78  }
79 
80  /**
81  * Construct from a pair of iterators.
82  *
83  * @param begin beginning of sequence.
84  * @param end end of sequence.
85  */
86  template <class ForwardIt,
87  typename = typename std::enable_if_t<
88  std::is_constructible_v<std::string_view, typename ForwardIt::value_type>>>
89  KeywordEx(ForwardIt begin, ForwardIt end) {
90  std::transform(begin, end, std::back_inserter(m_rules), [](auto&& value) {
91  return detail::ParseEx(std::string_view(value));
92  });
93  }
94 
95 protected:
96  constexpr std::vector<detail::Rule> const& GetRules() const noexcept {
97  return m_rules;
98  }
99 
100  friend bool KeywordMatch(fits::KeywordVariant const& keyword, KeywordEx const& ex);
101 
102 private:
103  std::vector<detail::Rule> m_rules;
104 };
105 
106 /**
107  *
108  * @tparam Iterator An iterator type to a type that can be converted to a std::string_view, such as
109  * char const**, std::vector<std::string>::const_iterator,
110  * std::list<std::string_view>::const_iterator.
111  */
112 bool KeywordMatch(fits::KeywordVariant const& keyword, KeywordEx const& ex);
113 
114 /**
115  * Transforms keyword name using regex
116  *
117  */
119 KeywordTransform(fits::KeywordVariant const& keyword, std::regex const& re, char const* fmt);
120 
122 KeywordTransform(fits::ValueKeyword const& keyword, std::regex const& re, char const* fmt);
123 
125 KeywordTransform(fits::EsoKeyword const& keyword, std::regex const& re, char const* fmt);
126 
128 KeywordTransform(fits::LiteralKeyword const& keyword, std::regex const& re, char const* fmt);
129 
130 } // namespace daq::dpm
131 
132 #endif // #ifndef DAQ_DPM_KEYWORD_EX_HPP
daq::dpm::KeywordEx::KeywordEx
KeywordEx()=default
daq::dpm::KeywordEx::GetRules
constexpr std::vector< detail::Rule > const & GetRules() const noexcept
Definition: keywordEx.hpp:96
daq::fits::KeywordVariant
std::variant< ValueKeyword, EsoKeyword, LiteralKeyword > KeywordVariant
The different variants of keywords that are supported.
Definition: keyword.hpp:400
daq::dpm::KeywordEx::KeywordEx
KeywordEx(std::initializer_list< char const * > rules)
Construct from initilizer_list of c-strings.
Definition: keywordEx.hpp:73
keyword.hpp
Contains data structure for FITS keywords.
daq::dpm::KeywordTransform
fits::KeywordVariant KeywordTransform(fits::KeywordVariant const &keyword, std::regex const &re, char const *fmt)
Transforms keyword name using regex.
Definition: keywordEx.cpp:164
daq::fits::Eso
@ Eso
An ESO hiearchical keyword.
Definition: keyword.hpp:72
daq::dpm::KeywordEx::KeywordMatch
friend bool KeywordMatch(fits::KeywordVariant const &keyword, KeywordEx const &ex)
Definition: keywordEx.cpp:150
daq::dpm::detail::Scope
Scope
Rule scope.
Definition: keywordEx.hpp:27
daq::fits::BasicKeyword
A type safe version of LiteralKeyword that consist of the three basic components of a FITS keyword ke...
Definition: keyword.hpp:266
daq::dpm::detail::Scope::Any
@ Any
daq::dpm::KeywordEx
Create keyword expression that memoize the provided string pattern.
Definition: keywordEx.hpp:59
daq::dpm
Definition: testDpSpec.cpp:16
daq::dpm::detail::Rule::pattern
std::string pattern
Definition: keywordEx.hpp:35
daq::dpm::detail::Rule::scope
Scope scope
Definition: keywordEx.hpp:34
daq::dpm::detail::Operator::Include
@ Include
daq::fits::Value
@ Value
A value keyword.
Definition: keyword.hpp:68
daq::dpm::KeywordEx::KeywordEx
KeywordEx(ForwardIt begin, ForwardIt end)
Construct from a pair of iterators.
Definition: keywordEx.hpp:89
daq::dpm::detail::ParseEx
Rule ParseEx(std::string_view ex)
Parse expression of the form documented in KeywordEx.
Definition: keywordEx.cpp:17
daq::dpm::detail::Rule::op
Operator op
Definition: keywordEx.hpp:33
daq::dpm::KeywordMatch
bool KeywordMatch(fits::KeywordVariant const &keyword, KeywordEx const &ex)
Definition: keywordEx.cpp:150
daq::dpm::detail::Rule
Represents a keyword rule expression.
Definition: keywordEx.hpp:32
daq::fits::LiteralKeyword
Represents the literal 80-character FITS keyword record.
Definition: keyword.hpp:125
daq::dpm::detail::Operator
Operator
Rule operator.
Definition: keywordEx.hpp:22