31 #ifndef DW_FRAMEWORK_PARAMETERPROVIDER_HPP_ 32 #define DW_FRAMEWORK_PARAMETERPROVIDER_HPP_ 38 #include <dw/core/container/StringView.hpp> 76 void getRequired(dw::core::StringView
const& key, T* out)
const 80 throw Exception(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
91 void getRequired(dw::core::StringView
const& key,
size_t const index, T* out)
const 95 throw Exception(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
105 template <
typename T>
110 return get(key, out);
124 template <
typename T>
125 bool getOptional(dw::core::StringView
const& key,
size_t const index, T* out)
const 129 return get(key, index, out);
144 typename std::enable_if_t<
145 !std::is_array<T>::value &&
146 !std::is_enum<T>::value>* =
nullptr>
147 bool get(dw::core::StringView
const& key, T* out)
const 149 return get(
this, key,
typeid(T),
typeid(T), out);
159 typename std::enable_if_t<
160 !std::is_array<T>::value &&
161 !std::is_enum<T>::value>* =
nullptr>
162 bool get(dw::core::StringView
const& key,
size_t const index, T* out)
const 164 return get(
this, key, index,
typeid(T),
typeid(T), out);
174 typename std::enable_if_t<
175 !std::is_array<T>::value &&
176 !std::is_enum<T>::value>* =
nullptr>
177 bool get(dw::core::StringView
const& key, std::vector<T>* out)
const 179 return get(
this, key,
typeid(std::vector<T>),
typeid(std::vector<T>), out);
189 typename std::enable_if_t<
190 std::is_array<T>::value &&
191 std::rank<T>::value == 1 &&
192 !std::is_enum<std::remove_extent_t<T>>::value>* =
nullptr>
193 bool get(dw::core::StringView
const& key, T* out)
const 195 static_assert(std::extent<T>::value > 0,
"Array must have size greater zero");
196 using TElement =
typename std::remove_extent_t<T>;
197 std::vector<TElement> value;
198 if (!
get(key, &value))
203 constexpr
size_t size =
sizeof(T) /
sizeof(TElement);
204 if (value.size() != size)
206 throw Exception(DW_FAILURE,
"Array sizes don't match");
209 TElement* element = &(*out[0]);
210 for (
size_t i = 0; i < size; ++i)
212 *(element + i) = value[i];
224 typename std::enable_if_t<
225 std::is_array<T>::value &&
226 std::rank<T>::value == 1 &&
227 std::is_same<T, char8_t>::value>* =
nullptr>
228 bool get(dw::core::StringView
const& key, T* out)
const 230 static_assert(std::extent<T>::value > 0,
"Array must have size greater zero");
232 if (!
get(key, &value))
237 if (value.size() >= std::extent<T, 0>::value)
239 throw Exception(DW_FAILURE,
"Array sizes don't match");
243 strncat(out, value.c_str(), value.size());
254 typename std::enable_if_t<
255 std::is_array<T>::value && std::rank<T>::value == 2 &&
256 !std::is_enum<std::remove_all_extents_t<T>>::value>* =
nullptr>
257 bool get(dw::core::StringView
const& key, T* out)
const 259 static_assert(std::extent<T, 0>::value > 0,
"Array must have 1st dimension size greater zero");
260 static_assert(std::extent<T, 1>::value > 0,
"Array must have 2nd dimension size greater zero");
261 using TElement =
typename std::remove_all_extents_t<T>;
262 std::vector<TElement> value;
263 if (!
get(key, &value))
268 constexpr
size_t size =
sizeof(T) /
sizeof(TElement);
269 if (value.size() != size)
271 throw Exception(DW_FAILURE,
"Array sizes don't match");
274 TElement* element = &(out[0][0]);
275 for (
size_t i = 0; i < size; ++i)
277 *(element + i) = value[i];
288 template <
typename S,
typename T>
291 if (!getOptional<S, T>(key, out))
293 throw Exception(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
303 template <
typename S,
typename T>
304 void getRequired(dw::core::StringView
const& key,
size_t const index, T* out)
const 306 if (!getOptional<S, T>(key, index, out))
308 throw Exception(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
318 template <
typename S,
typename T>
323 return get<S, T>(key, out);
329 throw Exception(e.
status(),
"Failed to get unnamed parameter with mangled semantic type: ",
typeid(S).name(),
" - ", e.
messageStr());
344 template <
typename S,
typename T>
345 bool getOptional(dw::core::StringView
const& key,
size_t const index, T* out)
const 349 return get<S, T>(key, index, out);
355 throw Exception(e.
status(),
"Failed to get unnamed parameter with mangled semantic type and index: ",
typeid(S).name(),
" ", index,
" - ", e.
messageStr());
359 throw Exception(e.
status(),
"Failed to get parameter with semantic by name and index: ", key,
" ", index,
" - ", e.
messageStr());
370 typename S,
typename T,
371 std::enable_if_t<!std::is_enum<T>::value>* =
nullptr>
372 bool get(dw::core::StringView
const& key, T* out)
const 374 static_assert(!std::is_same<T, dw::core::StringView>::value,
"T shouldn't be a dw::core::StringView, use FixedString<N> instead");
384 return get(
this, key,
typeid(S),
typeid(T), out);
393 typename S,
typename T,
size_t N,
394 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* =
nullptr>
395 bool get(dw::core::StringView
const& key, dw::core::FixedString<N>* out)
const 397 dw::core::StringView str;
398 const auto& semanticTypeInfo = std::is_same<S, dw::core::FixedString<N>>::value ?
typeid(dw::core::StringView) :
typeid(S);
399 bool success =
get(
this, key, semanticTypeInfo,
typeid(dw::core::StringView), &str);
404 throw Exception(DW_BUFFER_FULL,
"The FixedString parameter '", key,
"' has a maximum capacity of N=", N,
" but the value has a length of ", str.size() + 1,
"(including trailing \\0)");
406 out->copyFrom(str.data(), str.size());
419 typename S,
typename T,
420 std::enable_if_t<std::is_enum<T>::value>* =
nullptr>
421 bool get(dw::core::StringView
const& key, T* out)
const 426 return get(
this, key,
typeid(S),
typeid(T), out);
429 dw::core::StringView str;
430 if (!
get(
this, key,
typeid(dw::core::StringView),
typeid(dw::core::StringView), &str))
436 *out = mapEnumNameToValue<T>(str);
441 throw Exception(e.
status(),
"Failed to map enum name '", str,
"' for parameter '", key,
"' to numeric value: ", e.
messageStr());
451 typename S,
typename T,
452 std::enable_if_t<!std::is_enum<T>::value>* =
nullptr>
453 bool get(dw::core::StringView
const& key,
size_t const index, T* out)
const 455 return get(
this, key, index,
typeid(S),
typeid(T), out);
464 typename S,
typename T,
size_t N,
465 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* =
nullptr>
466 bool get(dw::core::StringView
const& key,
size_t const index, dw::core::FixedString<N>* out)
const 468 dw::core::StringView str;
469 const auto& semanticTypeInfo = std::is_same<S, dw::core::FixedString<N>>::value ?
typeid(dw::core::StringView) :
typeid(S);
470 bool success =
get(
this, key, index, semanticTypeInfo,
typeid(dw::core::StringView), &str);
475 throw Exception(DW_BUFFER_FULL,
"The FixedString parameter '", key,
"' and index ", index,
" has a maximum capacity of N=", N,
" but the value has a length of ", str.size() + 1,
"(including trailing \\0)");
477 out->copyFrom(str.data(), str.size());
490 typename S,
typename T,
491 std::enable_if_t<std::is_enum<T>::value>* =
nullptr>
492 bool get(dw::core::StringView
const& key,
size_t const index, T* out)
const 497 return get(
this, key, index,
typeid(S),
typeid(T), out);
500 dw::core::StringView str;
501 if (!
get(
this, key, index,
typeid(dw::core::StringView),
typeid(dw::core::StringView), &str))
507 *out = mapEnumNameToValue<T>(str);
512 throw Exception(e.
status(),
"Failed to map enum name '", str,
"' for parameter '", key,
"' and index ", index,
" to numeric value: ", e.
messageStr());
530 dw::core::StringView
const& key,
531 const std::type_info& semanticTypeInfo,
532 const std::type_info& dataTypeInfo,
533 void* out)
const = 0;
550 dw::core::StringView
const& key,
size_t const index,
551 const std::type_info& semanticTypeInfo,
552 const std::type_info& dataTypeInfo,
553 void* out)
const = 0;
559 #endif // DW_FRAMEWORK_PARAMETERPROVIDER_HPP_ char8_t const * messageStr() const noexcept
bool getOptional(dw::core::StringView const &key, T *out) const
Convenience API throwing an exception instead of returning a failed status.
ParameterProvider()=default
Default constructor.
void getRequired(dw::core::StringView const &key, T *out) const
Convenience API throwing an exception instead of returning false.
bool getOptional(dw::core::StringView const &key, size_t const index, T *out) const
Convenience API throwing an exception instead of returning a failed status.
void getRequired(dw::core::StringView const &key, size_t const index, T *out) const
Convenience API throwing an exception instead of returning false.
void getRequired(dw::core::StringView const &key, T *out) const
Convenience API throwing an exception instead of returning false.
void getRequired(dw::core::StringView const &key, size_t const index, T *out) const
Convenience API throwing an exception instead of returning false.
ParameterProvider & operator=(ParameterProvider const &)=default
Copy assignment operator.
virtual ~ParameterProvider()=default
Destructor.
The interface to access parameter values identified by name and/or (semantic) type.
bool getOptional(dw::core::StringView const &key, size_t const index, T *out) const
Convenience API throwing an exception instead of returning a failed status.
bool getOptional(dw::core::StringView const &key, T *out) const
Convenience API throwing an exception instead of returning a failed status.