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)
210 throw ExceptionWithStatus(DW_FAILURE,
"Array sizes don't match");
213 TElement* element{&(*out[0])};
214 for (
size_t i{0U}; i < size; ++i)
218 *(element + i) = value[i];
230 typename std::enable_if_t<
231 std::is_array<T>::value &&
232 std::rank<T>::value == 1 &&
233 std::is_same<T, char8_t>::value>* =
nullptr>
235 bool get(dw::core::StringView
const& key, T* out)
const
237 static_assert(std::extent<T>::value > 0,
"Array must have size greater zero");
239 if (!
get(key, &value))
244 if (value.size() >= std::extent<T, 0>::value)
246 throw ExceptionWithStatus(DW_FAILURE,
"Array sizes don't match");
250 strncat(out, value.c_str(), value.size());
261 typename std::enable_if_t<
262 std::is_array<T>::value && std::rank<T>::value == 2 &&
263 !std::is_enum<std::remove_all_extents_t<T>>::value>* =
nullptr>
265 bool get(dw::core::StringView
const& key, T* out)
const
267 static_assert(std::extent<T, 0>::value > 0,
"Array must have 1st dimension size greater zero");
268 static_assert(std::extent<T, 1>::value > 0,
"Array must have 2nd dimension size greater zero");
269 using TElement =
typename std::remove_all_extents_t<T>;
270 std::vector<TElement> value;
271 if (!
get(key, &value))
276 constexpr size_t size =
sizeof(T) /
sizeof(TElement);
277 if (value.size() != size)
279 throw ExceptionWithStatus(DW_FAILURE,
"Array sizes don't match");
282 TElement* element = &(out[0][0]);
283 for (
size_t i = 0; i < size; ++i)
285 *(element + i) = value[i];
296 template <
typename S,
typename T>
299 if (!getOptional<S, T>(key, out))
301 throw ExceptionWithStatus(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
311 template <
typename S,
typename T>
312 void getRequired(dw::core::StringView
const& key,
size_t const index, T* out)
const
314 if (!getOptional<S, T>(key, index, out))
316 throw ExceptionWithStatus(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
326 template <
typename S,
typename T>
332 return get<S, T>(key, out);
334 catch (ExceptionWithStatus& e)
338 throw ExceptionWithStatus(e.statusCode(),
"Failed to get unnamed parameter with mangled semantic type: ",
typeid(S).name(),
" - ", e.message());
342 throw ExceptionWithStatus(e.statusCode(),
"Failed to get parameter with semantic by name: ", key,
" - ", e.message());
353 template <
typename S,
typename T>
354 bool getOptional(dw::core::StringView
const& key,
size_t const index, T* out)
const
359 return get<S, T>(key, index, out);
361 catch (ExceptionWithStatus& e)
365 throw ExceptionWithStatus(e.statusCode(),
"Failed to get unnamed parameter with mangled semantic type and index: ",
typeid(S).name(),
" ", index,
" - ", e.message());
369 throw ExceptionWithStatus(e.statusCode(),
"Failed to get parameter with semantic by name and index: ", key,
" ", index,
" - ", e.message());
380 typename S,
typename T,
381 std::enable_if_t<!std::is_enum<T>::value>* =
nullptr>
383 bool get(dw::core::StringView
const& key, T* out)
const
385 static_assert(!std::is_same<T, dw::core::StringView>::value,
"T shouldn't be a dw::core::StringView, use FixedString<N> instead");
387 static_assert(!std::is_same<T, std::string>::value,
"T shouldn't be a std::string, use FixedString<N> instead");
393 return get(
this, key,
typeid(S),
typeid(T), out);
402 typename S,
typename T,
size_t N,
403 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* =
nullptr>
406 bool get(dw::core::StringView
const& key, dw::core::FixedString<N>* out)
const
408 dw::core::StringView str{};
409 const std::type_info& semanticTypeInfo{std::is_same<S, dw::core::FixedString<N>>::value ?
typeid(dw::core::StringView) :
typeid(S)};
410 bool success{
get(
this, key, semanticTypeInfo,
typeid(dw::core::StringView), &str)};
416 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)");
418 out->copyFrom(str.data(), str.size());
431 typename S,
typename T,
432 std::enable_if_t<std::is_enum<T>::value>* =
nullptr>
434 bool get(dw::core::StringView
const& key, T* out)
const
439 return get(
this, key,
typeid(S),
typeid(T), out);
442 dw::core::StringView str;
443 if (!
get(
this, key,
typeid(dw::core::StringView),
typeid(dw::core::StringView), &str))
449 *out = mapEnumNameToValue<T>(str);
452 catch (ExceptionWithStatus& e)
454 throw ExceptionWithStatus(e.statusCode(),
"Failed to map enum name '", str,
"' for parameter '", key,
"' to numeric value: ", e.message());
464 typename S,
typename T,
465 std::enable_if_t<!std::is_enum<T>::value>* =
nullptr>
467 bool get(dw::core::StringView
const& key,
size_t const index, T* out)
const
469 return get(
this, key, index,
typeid(S),
typeid(T), out);
478 typename S,
typename T,
size_t N,
479 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* =
nullptr>
482 bool get(dw::core::StringView
const& key,
size_t const index, dw::core::FixedString<N>* out)
const
484 dw::core::StringView str{};
485 const std::type_info& semanticTypeInfo{std::is_same<S, dw::core::FixedString<N>>::value ?
typeid(dw::core::StringView) :
typeid(S)};
486 bool success{
get(
this, key, index, semanticTypeInfo,
typeid(dw::core::StringView), &str)};
492 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)");
494 out->copyFrom(str.data(), str.size());
507 typename S,
typename T,
508 std::enable_if_t<std::is_enum<T>::value>* =
nullptr>
510 bool get(dw::core::StringView
const& key,
size_t const index, T* out)
const
515 return get(
this, key, index,
typeid(S),
typeid(T), out);
518 dw::core::StringView str;
519 if (!
get(
this, key, index,
typeid(dw::core::StringView),
typeid(dw::core::StringView), &str))
525 *out = mapEnumNameToValue<T>(str);
528 catch (ExceptionWithStatus& e)
530 throw ExceptionWithStatus(e.statusCode(),
"Failed to map enum name '", str,
"' for parameter '", key,
"' and index ", index,
" to numeric value: ", e.message());
550 dw::core::StringView
const& key,
551 const std::type_info& semanticTypeInfo,
552 const std::type_info& dataTypeInfo,
553 void* out)
const = 0;
572 dw::core::StringView
const& key,
size_t const index,
573 const std::type_info& semanticTypeInfo,
574 const std::type_info& dataTypeInfo,
575 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