ifw-daq  1.0.0
IFW Data Acquisition modules
keyword.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_ocm_libfits
4  * @copyright 2021 ESO - European Southern Observatory
5  *
6  * @brief Contains data structure for FITS keywords
7  */
8 #ifndef DAQ_OCM_DAQ_FITS_KEYWORD_HPP_
9 #define DAQ_OCM_DAQ_FITS_KEYWORD_HPP_
10 
11 
12 #include <array>
13 
14 #include <optional>
15 #include <string>
16 #include <variant>
17 #include <vector>
18 #include <iosfwd>
19 
20 
21 namespace daq::fits {
22 
23 /**
24  * Non template base class that is purely used to avoid type-deduction issues of ValueType.
25  */
27  // Possible value types that can be carried in a keyword
28  using ValueType = std::variant<std::string, int64_t, uint64_t, double, bool>;
29 };
30 
31 /**
32  * A type safe version of FormattedKeyword that consist of the three basic components of a FITS
33  * keyword keyword record: name, value and optional comment.
34  *
35  * @note name should be used for the logical keyword name, not the keyword name according to FITS
36  * standard. E.g. the ESO hiearchical keywords all have the keyword name HIERARCH according to the
37  * standard, but this structure should instead use the logical fields where e.g. "HIERARCH ESO DPR
38  * CATG" is the logical keyword name.
39  *
40  * It does not model any specific unit, only the value type. Therefore it is not strictly necessary
41  * to have all possible types represented in ValueType (e.g. both singel and double precision
42  * floats).
43  *
44  * The idea is that the unit, formatting and ranges is provided provided by dictionaries.
45  *
46  * It also typically requires a dictionary to be able to understand how to parse @c value.
47  *
48  * @ingroup daq_ocm_libfits
49  */
50 template <class Trait>
52  BasicKeyword() = default;
53  BasicKeyword(BasicKeyword const&) = default;
54  BasicKeyword(BasicKeyword&&) noexcept = default;
55  BasicKeyword(std::string name,
56  char const* string_value,
57  std::optional<std::string> comment = std::nullopt);
58  BasicKeyword(std::string name,
60  std::optional<std::string> comment = std::nullopt) noexcept;
61 
62  BasicKeyword& operator=(BasicKeyword&& rhs) noexcept = default;
63  BasicKeyword& operator=(BasicKeyword const& rhs) = default;
64 
65  /**
66  * Compares all members for equality.
67  */
68  bool operator==(BasicKeyword const& rhs) const noexcept;
69  /**
70  * Compares all members for inequality.
71  */
72  bool operator!=(BasicKeyword const& rhs) const noexcept;
73  /**
74  * Uses @a name property as the sorting index.
75  */
76  bool operator<(BasicKeyword const& rhs) const noexcept;
77 
78 
79  std::string name;
81  std::optional<std::string> comment;
82 };
83 
85 struct EsoKeywordTraits {};
86 
87 /**
88  * Standard FITS value keyword.
89  *
90  * @ingroup daq_ocm_libfits
91  */
93 
94 /**
95  * ESO hiearchical keyword.
96  *
97  * @ingroup daq_ocm_libfits
98  */
100 
101 /**
102  * EsoKeyword is sorted after ValueKeyword.
103  *
104  * @ingroup daq_ocm_libfits
105  */
106 constexpr bool operator<(ValueKeyword const&, EsoKeyword const&) noexcept {
107  return true;
108 }
109 
110 /**
111  * EsoKeyword is sorted after ValueKeyword.
112  *
113  * @ingroup daq_ocm_libfits
114  */
115 constexpr bool operator<(EsoKeyword const&, ValueKeyword const&) noexcept {
116  return false;
117 }
118 
119 template <class Trait>
120 std::ostream& operator<<(std::ostream& os, BasicKeyword<Trait> const& kw);
121 
122 template <class Trait>
123 std::ostream& operator<<(std::ostream& os, BasicKeywordBase::ValueType const& kw);
124 
125 
126 /**
127  * The different variants of keywords that are supported.
128  *
129  * @ingroup daq_ocm_libfits
130  */
131 using KeywordVariant = std::variant<ValueKeyword, EsoKeyword>;
132 
133 /**
134  * Vector of keywords.
135  *
136  * @ingroup daq_ocm_libfits
137  */
138 using KeywordVector = std::vector<KeywordVariant>;
139 
140 /**
141  * Ostream formatting of keywords.
142  *
143  * @ingroup daq_ocm_libfits
144  */
145 std::ostream& operator<<(std::ostream& os, KeywordVariant const& kw);
146 
147 /**
148  * Compare keyword names of keyword of the same type.
149  *
150  * Comparing names only is useful to e.g. look up keyword by name, to modify it.
151  *
152  * @param lhs value to compare
153  * @param rhs value to compare
154  * @returns true iff lhs and rhs are the same type and their name property compares equal.
155  *
156  * @ingroup daq_ocm_libfits
157  */
158 bool NameEquals(KeywordVariant const& lhs, KeywordVariant const& rhs) noexcept;
159 
160 /**
161  * Updates @a a with keywords from @a b.
162  *
163  * New keywords are inserted at the end. Existing keywords will be updated with new value if
164  * keyword type and name is the same.
165  *
166  * @param to Keywords to update to.
167  * @param from Keywords to update from.
168  *
169  * @ingroup daq_ocm_libfits
170  */
171 void UpdateKeywords(KeywordVector& to, KeywordVector const& from);
172 
173 } // namespace daq::fits
174 #endif // #ifndef DAQ_OCM_DAQ_KEYWORD_HPP_
daq::fits::KeywordVariant
std::variant< ValueKeyword, EsoKeyword > KeywordVariant
The different variants of keywords that are supported.
Definition: keyword.hpp:131
daq::fits::BasicKeyword::BasicKeyword
BasicKeyword(BasicKeyword &&) noexcept=default
daq::fits::BasicKeyword::BasicKeyword
BasicKeyword()=default
daq::fits::BasicKeyword::BasicKeyword
BasicKeyword(BasicKeyword const &)=default
daq::fits::NameEquals
bool NameEquals(KeywordVariant const &lhs, KeywordVariant const &rhs) noexcept
Compare keyword names of keyword of the same type.
Definition: keyword.cpp:73
daq::fits::EsoKeywordTraits
Definition: keyword.hpp:85
daq::fits::UpdateKeywords
void UpdateKeywords(KeywordVector &to, KeywordVector const &from)
Updates a with keywords from b.
Definition: keyword.cpp:120
daq::fits::BasicKeywordBase::ValueType
std::variant< std::string, int64_t, uint64_t, double, bool > ValueType
Definition: keyword.hpp:28
daq::fits
Definition: cfitsio.cpp:14
daq::fits::BasicKeyword::operator<
bool operator<(BasicKeyword const &rhs) const noexcept
Uses name property as the sorting index.
Definition: keyword.cpp:68
daq::fits::BasicKeyword
A type safe version of FormattedKeyword that consist of the three basic components of a FITS keyword ...
Definition: keyword.hpp:51
daq::fits::BasicKeyword::comment
std::optional< std::string > comment
Definition: keyword.hpp:81
daq::fits::operator<<
std::ostream & operator<<(std::ostream &os, BasicKeyword< Trait > const &kw)
Definition: keyword.cpp:101
daq::fits::BasicKeywordBase
Non template base class that is purely used to avoid type-deduction issues of ValueType.
Definition: keyword.hpp:26
daq::fits::BasicKeyword::value
ValueType value
Definition: keyword.hpp:80
daq::fits::KeywordVector
std::vector< KeywordVariant > KeywordVector
Vector of keywords.
Definition: keyword.hpp:138
daq::fits::ValueKeywordTraits
Definition: keyword.hpp:84
daq::fits::BasicKeyword::name
std::string name
Definition: keyword.hpp:79