IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
peak_common_metadata.hpp
Go to the documentation of this file.
1
11#pragma once
12
17#include <peak_common_c/detail/peak_common_defines.h>
18
19#include <unordered_map>
20#include <algorithm>
21#include <sstream>
22#include <vector>
23
24namespace peak
25{
26namespace common
27{
28
45{
46public:
56 template <typename T>
57 void SetValueByName(const std::string& key, T&& value)
58 {
59 m_data[key] = Any(std::forward<T>(value));
60 }
61
75 template <MetadataKey metadataKey, typename T>
76 void SetValueByKey(T&& value)
77 {
78 static_assert(
79 std::is_convertible<T, typename detail::MetadataKeyTraits<metadataKey>::type>::value, "Incorrect value type for metadata key");
81 detail::MetadataKeyTraits<metadataKey>::Name(), static_cast<typename detail::MetadataKeyTraits<metadataKey>::type>(value));
82 }
83
92 PEAK_COMMON_NO_DISCARD bool HasEntryByName(const std::string& name) const
93 {
94 return m_data.find(name) != m_data.end(); // NOLINT
95 }
96
107 template <MetadataKey metadataKey>
108 PEAK_COMMON_NO_DISCARD bool HasEntryByKey() const
109 {
110 using trait = detail::MetadataKeyTraits<metadataKey>;
111 const auto hasKey = HasEntryByName(trait::Name());
112
113 if (hasKey)
114 {
115 const auto hasCorrectType = TryGetValueByName(trait::Name()).GetType() == typeid(typename trait::type);
116 return hasCorrectType;
117 }
118
119 return false;
120 }
121
130 PEAK_COMMON_NO_DISCARD Any TryGetValueByName(const std::string& key) const
131 {
132 if (!HasEntryByName(key))
133 {
134 return {};
135 }
136
137 return m_data.at(key);
138 }
139
153 template <typename T>
154 PEAK_COMMON_NO_DISCARD T GetValueByName(const std::string& key) const
155 {
156 if (!HasEntryByName(key))
157 {
158 throw OutOfRangeException("There is no value named " + key);
159 }
160
161 return TryGetValueByName(key).AnyCast<T>();
162 }
163
179 template <MetadataKey metadataKey>
180 PEAK_COMMON_NO_DISCARD typename detail::MetadataKeyTraits<metadataKey>::type GetValueByKey() const
181 {
182 using trait = detail::MetadataKeyTraits<metadataKey>;
183 return GetValueByName<typename trait::type>(trait::Name());
184 }
185
197 friend std::ostream& operator<<(std::ostream& os, const Metadata& metadata)
198 {
199 bool first = true;
200 os << "{";
201 for (const auto& pair : metadata.m_data)
202 {
203 if (!first)
204 {
205 os << ',';
206 }
207
208 // NOTE: Cannot output value, as we don't know the type...
209 os << " \"" << pair.first << "\"";
210
211 first = false;
212 }
213 os << " }";
214
215 return os;
216 }
217
227 PEAK_COMMON_NO_DISCARD std::string ToString() const
228 {
229 std::ostringstream ss;
230 ss << *this;
231 return ss.str();
232 }
233
241 PEAK_COMMON_NO_DISCARD std::vector<std::string> GetKeyNames() const
242 {
243 std::vector<std::string> keys;
244 keys.reserve(m_data.size());
245 std::transform(m_data.cbegin(), m_data.cend(), std::back_inserter(keys), [](const auto& pair) {
246 return pair.first;
247 });
248
249 return keys;
250 }
251
252private:
253 std::unordered_map<std::string, Any> m_data;
254};
255
256} // namespace common
257} // namespace peak
A type-erased container for any copyable value.
const std::type_info & GetType() const
Gets the type of the stored value.
T & AnyCast()
Casts the stored value to the specified type.
Type-safe container for image acquisition metadata.
T GetValueByName(const std::string &key) const
Retrieves and casts the value associated with a string key.
friend std::ostream & operator<<(std::ostream &os, const Metadata &metadata)
Outputs the metadata keys in human-readable form.
std::string ToString() const
Returns a string representation of the metadata.
Any TryGetValueByName(const std::string &key) const
Retrieves a value by string key as a type-erased object.
void SetValueByName(const std::string &key, T &&value)
Stores a value using a string-based key.
std::vector< std::string > GetKeyNames() const
Returns a vector of key names.
bool HasEntryByName(const std::string &name) const
Checks if a given string key is stored in the metadata.
void SetValueByKey(T &&value)
Stores a value using a strongly typed metadata key.
bool HasEntryByKey() const
Checks if a known key is stored in the metadata.
detail::MetadataKeyTraits< metadataKey >::type GetValueByKey() const
Retrieves the value of a strongly typed metadata key.
Thrown when a parameter is outside the valid range.