31#ifndef DW_FRAMEWORK_PARAMETERPROVIDER_HPP_
32#define DW_FRAMEWORK_PARAMETERPROVIDER_HPP_
38#include <dwshared/dwfoundation/dw/core/container/StringView.hpp>
76 void getRequired(dw::core::StringView
const& key, T* out)
const
80 throw ExceptionWithStatus(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 ExceptionWithStatus(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
105 template <
typename T>
110 return get(key, out);
112 catch (ExceptionWithStatus& e)
114 throw ExceptionWithStatus(e.statusCode(),
"Failed to get parameter by name: ", key,
" - ", e.message());
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);
131 catch (ExceptionWithStatus& e)
133 throw ExceptionWithStatus(e.statusCode(),
"Failed to get parameter by name and index: ", key,
"[", index,
"] - ", e.message());
144 typename std::enable_if_t<
145 !std::is_array<T>::value &&
146 !std::is_enum<T>::value>* =
nullptr>
148 bool get(dw::core::StringView
const& key, T* out)
const
150 return get(
this, key,
typeid(T),
typeid(T), out);
160 typename std::enable_if_t<
161 !std::is_array<T>::value &&
162 !std::is_enum<T>::value>* =
nullptr>
164 bool get(dw::core::StringView
const& key,
size_t const index, T* out)
const
166 return get(
this, key, index,
typeid(T),
typeid(T), out);
176 typename std::enable_if_t<
177 !std::is_array<T>::value &&
178 !std::is_enum<T>::value>* =
nullptr>
180 bool get(dw::core::StringView
const& key, std::vector<T>* out)
const
182 return get(
this, key,
typeid(std::vector<T>),
typeid(std::vector<T>), out);
192 typename std::enable_if_t<
193 std::is_array<T>::value &&
194 std::rank<T>::value == 1 &&
195 !std::is_enum<std::remove_extent_t<T>>::value>* =
nullptr>
197 bool get(dw::core::StringView
const& key, T* out)
const
199 static_assert(std::extent<T>::value > 0,
"Array must have size greater zero");
200 using TElement =
typename std::remove_extent_t<T>;
201 std::vector<TElement> value{};
202 if (!
get(key, &value))
207 constexpr size_t size{
sizeof(T) /
sizeof(TElement)};
208 if (value.size() != size)
211 throw ExceptionWithStatus(DW_FAILURE,
"Array sizes don't match");
214 TElement* element{&(*out[0])};
215 for (
size_t i{0U}; i < size; ++i)
219 *(element + i) = value[i];
231 typename std::enable_if_t<
232 std::is_array<T>::value &&
233 std::rank<T>::value == 1 &&
234 std::is_same<T, char8_t>::value>* =
nullptr>
236 bool get(dw::core::StringView
const& key, T* out)
const
238 static_assert(std::extent<T>::value > 0,
"Array must have size greater zero");
240 if (!
get(key, &value))
245 if (value.size() >= std::extent<T, 0>::value)
247 throw ExceptionWithStatus(DW_FAILURE,
"Array sizes don't match");
251 strncat(out, value.c_str(), value.size());
262 typename std::enable_if_t<
263 std::is_array<T>::value && std::rank<T>::value == 2 &&
264 !std::is_enum<std::remove_all_extents_t<T>>::value>* =
nullptr>
266 bool get(dw::core::StringView
const& key, T* out)
const
268 static_assert(std::extent<T, 0>::value > 0,
"Array must have 1st dimension size greater zero");
269 static_assert(std::extent<T, 1>::value > 0,
"Array must have 2nd dimension size greater zero");
270 using TElement =
typename std::remove_all_extents_t<T>;
271 std::vector<TElement> value;
272 if (!
get(key, &value))
277 constexpr size_t size =
sizeof(T) /
sizeof(TElement);
278 if (value.size() != size)
280 throw ExceptionWithStatus(DW_FAILURE,
"Array sizes don't match");
283 TElement* element = &(out[0][0]);
284 for (
size_t i = 0; i < size; ++i)
286 *(element + i) = value[i];
297 template <
typename S,
typename T>
300 if (!getOptional<S, T>(key, out))
302 throw ExceptionWithStatus(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
312 template <
typename S,
typename T>
313 void getRequired(dw::core::StringView
const& key,
size_t const index, T* out)
const
315 if (!getOptional<S, T>(key, index, out))
317 throw ExceptionWithStatus(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
327 template <
typename S,
typename T>
333 return get<S, T>(key, out);
335 catch (ExceptionWithStatus& e)
339 throw ExceptionWithStatus(e.statusCode(),
"Failed to get unnamed parameter with mangled semantic type: ",
typeid(S).name(),
" - ", e.message());
343 throw ExceptionWithStatus(e.statusCode(),
"Failed to get parameter with semantic by name: ", key,
" - ", e.message());
354 template <
typename S,
typename T>
355 bool getOptional(dw::core::StringView
const& key,
size_t const index, T* out)
const
360 return get<S, T>(key, index, out);
362 catch (ExceptionWithStatus& e)
366 throw ExceptionWithStatus(e.statusCode(),
"Failed to get unnamed parameter with mangled semantic type and index: ",
typeid(S).name(),
" ", index,
" - ", e.message());
370 throw ExceptionWithStatus(e.statusCode(),
"Failed to get parameter with semantic by name and index: ", key,
" ", index,
" - ", e.message());
381 typename S,
typename T,
382 std::enable_if_t<!std::is_enum<T>::value>* =
nullptr>
384 bool get(dw::core::StringView
const& key, T* out)
const
386 static_assert(!std::is_same<T, dw::core::StringView>::value,
"T shouldn't be a dw::core::StringView, use FixedString<N> instead");
388 static_assert(!std::is_same<T, std::string>::value,
"T shouldn't be a std::string, use FixedString<N> instead");
394 return get(
this, key,
typeid(S),
typeid(T), out);
403 typename S,
typename T,
size_t N,
404 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* =
nullptr>
407 bool get(dw::core::StringView
const& key, dw::core::FixedString<N>* out)
const
409 dw::core::StringView str{};
411 const auto& semanticTypeInfo{std::is_same<S, dw::core::FixedString<N>>::value ?
typeid(dw::core::StringView) :
typeid(S)};
412 bool success{
get(
this, key, semanticTypeInfo,
typeid(dw::core::StringView), &str)};
418 throw ExceptionWithStatus(DW_BUFFER_FULL,
"The FixedString parameter '", key,
"' has a maximum capacity of N=", N,
" but the value has a length of ", str.size() + 1U,
"(including trailing \\0)");
420 out->copyFrom(str.data(), str.size());
433 typename S,
typename T,
434 std::enable_if_t<std::is_enum<T>::value>* =
nullptr>
436 bool get(dw::core::StringView
const& key, T* out)
const
441 return get(
this, key,
typeid(S),
typeid(T), out);
444 dw::core::StringView str;
445 if (!
get(
this, key,
typeid(dw::core::StringView),
typeid(dw::core::StringView), &str))
451 *out = mapEnumNameToValue<T>(str);
454 catch (ExceptionWithStatus& e)
456 throw ExceptionWithStatus(e.statusCode(),
"Failed to map enum name '", str,
"' for parameter '", key,
"' to numeric value: ", e.message());
466 typename S,
typename T,
467 std::enable_if_t<!std::is_enum<T>::value>* =
nullptr>
469 bool get(dw::core::StringView
const& key,
size_t const index, T* out)
const
471 return get(
this, key, index,
typeid(S),
typeid(T), out);
480 typename S,
typename T,
size_t N,
481 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* =
nullptr>
484 bool get(dw::core::StringView
const& key,
size_t const index, dw::core::FixedString<N>* out)
const
486 dw::core::StringView str{};
488 const auto& semanticTypeInfo{std::is_same<S, dw::core::FixedString<N>>::value ?
typeid(dw::core::StringView) :
typeid(S)};
489 bool success{
get(
this, key, index, semanticTypeInfo,
typeid(dw::core::StringView), &str)};
495 throw ExceptionWithStatus(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() + 1U,
"(including trailing \\0)");
497 out->copyFrom(str.data(), str.size());
510 typename S,
typename T,
511 std::enable_if_t<std::is_enum<T>::value>* =
nullptr>
513 bool get(dw::core::StringView
const& key,
size_t const index, T* out)
const
518 return get(
this, key, index,
typeid(S),
typeid(T), out);
521 dw::core::StringView str;
522 if (!
get(
this, key, index,
typeid(dw::core::StringView),
typeid(dw::core::StringView), &str))
528 *out = mapEnumNameToValue<T>(str);
531 catch (ExceptionWithStatus& e)
533 throw ExceptionWithStatus(e.statusCode(),
"Failed to map enum name '", str,
"' for parameter '", key,
"' and index ", index,
" to numeric value: ", e.message());
553 dw::core::StringView
const& key,
554 const std::type_info& semanticTypeInfo,
555 const std::type_info& dataTypeInfo,
556 void* out)
const = 0;
575 dw::core::StringView
const& key,
size_t const index,
576 const std::type_info& semanticTypeInfo,
577 const std::type_info& dataTypeInfo,
578 void* out)
const = 0;
The interface to access parameter values identified by name and/or (semantic) type.
virtual bool get(ParameterProvider const *const parentProvider, dw::core::StringView const &key, size_t const index, const std::type_info &semanticTypeInfo, const std::type_info &dataTypeInfo, void *out) const =0
ParameterProvider & operator=(ParameterProvider const &) &=default
Copy assignment operator.
void getRequired(dw::core::StringView const &key, T *out) const
void getRequired(dw::core::StringView const &key, T *out) const
ParameterProvider(ParameterProvider const &)=default
Copy constructor.
virtual bool get(ParameterProvider const *const parentProvider, dw::core::StringView const &key, const std::type_info &semanticTypeInfo, const std::type_info &dataTypeInfo, void *out) const =0
bool get(dw::core::StringView const &key, size_t const index, T *out) const
virtual ~ParameterProvider()=default
Destructor.
bool get(dw::core::StringView const &key, T *out) const
void getRequired(dw::core::StringView const &key, size_t const index, T *out) const
bool getOptional(dw::core::StringView const &key, size_t const index, T *out) const
bool getOptional(dw::core::StringView const &key, size_t const index, T *out) const
ParameterProvider(ParameterProvider &&)=default
Move constructor.
bool getOptional(dw::core::StringView const &key, T *out) const
bool get(dw::core::StringView const &key, T *out) const
ParameterProvider()=default
Default constructor.
void getRequired(dw::core::StringView const &key, size_t const index, T *out) const
bool getOptional(dw::core::StringView const &key, T *out) const
bool get(dw::core::StringView const &key, size_t const index, T *out) const
bool get(dw::core::StringView const &key, size_t const index, dw::core::FixedString< N > *out) const
bool get(dw::core::StringView const &key, dw::core::FixedString< N > *out) const
ParameterProvider & operator=(ParameterProvider &&) &=default
Move assignment operator.
bool get(dw::core::StringView const &key, std::vector< T > *out) const