IDS peak AFL v1.8.0
Loading...
Searching...
No Matches
peak_common_metadata.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
18
19#include <unordered_map>
20#include <sstream>
21
22namespace peak
23{
24namespace common
25{
26
43{
44public:
54 template <typename T>
55 void SetValueByName(const std::string& key, T&& value)
56 {
57 m_data[key] = Any(std::forward<T>(value));
58 }
59
73 template <MetadataKey metadataKey, typename T>
74 void SetValueByKey(T&& value)
75 {
76 static_assert(
77 std::is_convertible<T, typename detail::MetadataKeyTraits<metadataKey>::type>::value, "Incorrect value type for metadata key");
79 detail::MetadataKeyTraits<metadataKey>::name, static_cast<typename detail::MetadataKeyTraits<metadataKey>::type>(value));
80 }
81
90 PEAK_COMMON_NO_DISCARD bool HasEntryByName(const std::string& name) const
91 {
92 return m_data.find(name) != m_data.end();
93 }
94
105 template <MetadataKey metadataKey>
107 {
108 using trait = detail::MetadataKeyTraits<metadataKey>;
109 const auto hasKey = HasEntryByName(trait::name);
110
111 if (hasKey)
112 {
113 const auto hasCorrectType = TryGetValueByName(trait::name).GetType() == typeid(typename trait::type);
114 return hasCorrectType;
115 }
116
117 return false;
118 }
119
128 PEAK_COMMON_NO_DISCARD Any TryGetValueByName(const std::string& key) const
129 {
130 if (!HasEntryByName(key))
131 {
132 return {};
133 }
134
135 return m_data.at(key);
136 }
137
151 template <typename T>
152 PEAK_COMMON_NO_DISCARD T GetValueByName(const std::string& key) const
153 {
154 if (!HasEntryByName(key))
155 {
156 throw OutOfRangeException("There is no value named " + key);
157 }
158
159 return TryGetValueByName(key).AnyCast<T>();
160 }
161
177 template <MetadataKey metadataKey>
178 PEAK_COMMON_NO_DISCARD typename detail::MetadataKeyTraits<metadataKey>::type GetValueByKey() const
179 {
180 using trait = detail::MetadataKeyTraits<metadataKey>;
181 return GetValueByName<typename trait::type>(trait::name);
182 }
183
195 friend std::ostream& operator<<(std::ostream& os, const Metadata& metadata)
196 {
197 bool first = true;
198 os << "{";
199 for (const auto& pair : metadata.m_data)
200 {
201 if (!first)
202 {
203 os << ',';
204 }
205
206 // NOTE: Cannot output value, as we don't know the type...
207 os << " \"" << pair.first << "\"";
208
209 first = false;
210 }
211 os << " }";
212
213 return os;
214 }
215
226 {
227 std::ostringstream ss;
228 ss << *this;
229 return ss.str();
230 }
231
232private:
233 std::unordered_map<std::string, Any> m_data;
234};
235
236} // namespace common
237} // namespace peak
A type-erased container for any copyable value.
Definition peak_common_any.hpp:36
PEAK_COMMON_NO_DISCARD const std::type_info & GetType() const
Gets the type of the stored value.
Definition peak_common_any.hpp:164
T & AnyCast()
Casts the stored value to the specified type.
Definition peak_common_any.hpp:179
Type-safe container for image acquisition metadata.
Definition peak_common_metadata.hpp:43
PEAK_COMMON_NO_DISCARD T GetValueByName(const std::string &key) const
Retrieves and casts the value associated with a string key.
Definition peak_common_metadata.hpp:152
friend std::ostream & operator<<(std::ostream &os, const Metadata &metadata)
Outputs the metadata keys in human-readable form.
Definition peak_common_metadata.hpp:195
void SetValueByName(const std::string &key, T &&value)
Stores a value using a string-based key.
Definition peak_common_metadata.hpp:55
PEAK_COMMON_NO_DISCARD bool HasEntryByName(const std::string &name) const
Checks if a given string key is stored in the metadata.
Definition peak_common_metadata.hpp:90
PEAK_COMMON_NO_DISCARD bool HasEntryByKey() const
Checks if a known key is stored in the metadata.
Definition peak_common_metadata.hpp:106
void SetValueByKey(T &&value)
Stores a value using a strongly typed metadata key.
Definition peak_common_metadata.hpp:74
PEAK_COMMON_NO_DISCARD detail::MetadataKeyTraits< metadataKey >::type GetValueByKey() const
Retrieves the value of a strongly typed metadata key.
Definition peak_common_metadata.hpp:178
PEAK_COMMON_NO_DISCARD std::string ToString() const
Returns a string representation of the metadata.
Definition peak_common_metadata.hpp:225
PEAK_COMMON_NO_DISCARD Any TryGetValueByName(const std::string &key) const
Retrieves a value by string key as a type-erased object.
Definition peak_common_metadata.hpp:128
Thrown when a parameter is outside the valid range.
Definition peak_common_exceptions.hpp:111
#define PEAK_COMMON_NO_DISCARD
Compatability macro for the nodiscard attribute.
Definition peak_common_defines.hpp:91
Root namespace for common types and interfaces shared across multiple libraries.
Definition peak_common_math.hpp:22