19#include <unordered_map>
30struct MetadataAccessor;
51 using allowedTypes = std::tuple<int64_t, std::vector<int64_t>, double, std::vector<double>, std::string, std::vector<std::string>, bool,
66 Dispatch(name, std::forward<T>(value), std::is_lvalue_reference<T>{});
96 template <MetadataKey metadataKey,
typename T>
100 std::is_convertible<T, typename detail::MetadataKeyTraits<metadataKey>::type>::value,
"Incorrect value type for metadata key");
101 constexpr auto key = detail::MetadataKeyTraits<metadataKey>::Name();
102 m_data[key] =
Any(
static_cast<typename detail::MetadataKeyTraits<metadataKey>::type
>(std::forward<T>(value)));
115 return m_data.find(name) != m_data.end();
128 template <MetadataKey metadataKey>
131 using trait = detail::MetadataKeyTraits<metadataKey>;
137 return hasCorrectType;
158 return m_data.at(name);
174 template <
typename T>
177 static_assert(detail::is_in_tuple<std::decay_t<T>, allowedTypes>::value,
"T must be one of the allowedTypes!");
202 template <MetadataKey metadataKey>
203 PEAK_COMMON_NO_DISCARD
typename detail::MetadataKeyTraits<metadataKey>::type
GetValueByKey()
const
205 using trait = detail::MetadataKeyTraits<metadataKey>;
208 const std::string keyName = trait::Name();
212 return TryGetValueByName(trait::Name()).template AnyCast<typename trait::type>();
230 for (
const auto& pair : metadata.m_data)
238 os <<
" \"" << pair.first <<
"\"";
256 PEAK_COMMON_NO_DISCARD std::string
ToString()
const
258 std::ostringstream ss;
270 PEAK_COMMON_NO_DISCARD std::vector<std::string>
GetKeyNames()
const
272 std::vector<std::string> names;
273 names.reserve(m_data.size());
274 std::transform(m_data.cbegin(), m_data.cend(), std::back_inserter(names), [](
const auto& pair) {
282 template <
typename T>
283 void Dispatch(
const std::string& key, T&& value, std::true_type)
285 using Decayed =
typename std::decay<T>::type;
287 static_assert(detail::is_in_tuple<Decayed, allowedTypes>::value,
"Lvalue type must be exactly one of the 'allowedTypes'!");
289 m_data[key] =
Any(value);
292 template <
typename T>
293 void Dispatch(
const std::string& key, T&& value, std::false_type)
295 using Normalized = detail::normalize_t<T>;
297 static_assert(detail::is_in_tuple<Normalized, allowedTypes>::value,
298 "Rvalue type must be one of the 'allowedTypes' or be able to be widened into one of the 'allowedTypes'!");
300 m_data[key] = Any(Cast(std::forward<T>(value)));
303 template <
typename T>
304 void SetValueByNameInternal(
const std::string& name, T&& value)
306 m_data[name] = Any(std::forward<T>(value));
309 template <
typename T>
310 static detail::normalize_t<T> Cast(T&& v)
312 return static_cast<detail::normalize_t<T>
>(v);
315 template <
typename T>
316 static std::vector<T> Cast(std::vector<T>&& v)
321 std::unordered_map<std::string, Any> m_data;
323 friend struct detail::MetadataAccessor;
328struct MetadataAccessor
330 template <
typename T>
331 static void SetValueByName(Metadata& metadata,
const std::string& key, T&& value)
333 metadata.SetValueByNameInternal(key, std::forward<T>(value));
A type-erased container for any copyable value.
Definition peak_common_any.hpp:36
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
Thrown when a parameter is outside the valid range.
Definition peak_common_exceptions.hpp:119
Namespace for internal implementation details and low-level types.
Definition peak_common_math.hpp:24
Root namespace for common types and interfaces shared across multiple libraries.
Definition peak_common_math.hpp:22