14#include <unordered_map>
46 using allowedTypes = std::tuple<int64_t, std::vector<int64_t>, double, std::vector<double>, std::string, std::vector<std::string>, bool,
61 Dispatch(name, std::forward<T>(value), std::is_lvalue_reference<T>{});
91 template <MetadataKey metadataKey,
typename T>
95 std::is_convertible<T, typename detail::MetadataKeyTraits<metadataKey>::type>::value,
"Incorrect value type for metadata key");
96 constexpr auto key = detail::MetadataKeyTraits<metadataKey>::Name();
97 m_data[key] =
Any(
static_cast<typename detail::MetadataKeyTraits<metadataKey>::type
>(std::forward<T>(value)));
110 return m_data.find(name) != m_data.end();
123 template <MetadataKey metadataKey>
126 using trait = detail::MetadataKeyTraits<metadataKey>;
132 return hasCorrectType;
153 return m_data.at(name);
169 template <
typename T>
172 static_assert(detail::is_in_tuple<std::decay_t<T>,
allowedTypes>::value,
"T must be one of the allowedTypes!");
197 template <MetadataKey metadataKey>
200 using trait = detail::MetadataKeyTraits<metadataKey>;
203 const std::string keyName = trait::Name();
207 return TryGetValueByName(trait::Name()).template AnyCast<typename trait::type>();
225 for (
const auto& pair : metadata.m_data)
233 os <<
" \"" << pair.first <<
"\"";
253 std::ostringstream ss;
267 std::vector<std::string> names;
268 names.reserve(m_data.size());
269 std::transform(m_data.cbegin(), m_data.cend(), std::back_inserter(names), [](
const auto& pair) {
277 template <
typename T>
278 void Dispatch(
const std::string& key, T&& value, std::true_type)
280 using Decayed =
typename std::decay<T>::type;
282 static_assert(detail::is_in_tuple<Decayed, allowedTypes>::value,
"Lvalue type must be exactly one of the 'allowedTypes'!");
284 m_data[key] =
Any(value);
287 template <
typename T>
288 void Dispatch(
const std::string& key, T&& value, std::false_type)
290 using Normalized = detail::normalize_t<T>;
292 static_assert(detail::is_in_tuple<Normalized, allowedTypes>::value,
293 "Rvalue type must be one of the 'allowedTypes' or be able to be widened into one of the 'allowedTypes'!");
295 m_data[key] = Any(Cast(std::forward<T>(value)));
298 template <
typename T>
299 void SetValueByNameInternal(
const std::string& name, T&& value)
301 m_data[name] = Any(std::forward<T>(value));
304 template <
typename T>
305 static detail::normalize_t<T> Cast(T&& v)
307 return static_cast<detail::normalize_t<T>
>(v);
310 template <
typename T>
311 static std::vector<T> Cast(std::vector<T>&& v)
316 std::unordered_map<std::string, Any> m_data;
325 template <
typename T>
328 metadata.SetValueByNameInternal(key, std::forward<T>(value));
A type-erased container for any copyable value.
Definition peak_common_any.hpp:31
T & AnyCast()
Casts the stored value to the specified type.
Definition peak_common_any.hpp:174
PEAK_COMMON_NO_DISCARD const std::type_info & GetType() const
Gets the type of the stored value.
Definition peak_common_any.hpp:159
Thrown when a parameter is outside the valid range.
Definition peak_common_exceptions.hpp:114
Namespace for internal implementation details and low-level types.
Definition peak_common_math.hpp:19
Root namespace for common types and interfaces shared across multiple libraries.
Definition peak_common_math.hpp:17
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition peak_buffer.hpp:29
#define PEAK_COMMON_NO_DISCARD
Definition peak_common_defines.h:89