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>
149 bool get(dw::core::StringView
const& key, T* out)
const
151 return get(
this, key,
typeid(T),
typeid(T), out);
161 typename std::enable_if_t<
162 !std::is_array<T>::value &&
163 !std::is_enum<T>::value>* =
nullptr>
166 bool get(dw::core::StringView
const& key,
size_t const index, T* out)
const
168 return get(
this, key, index,
typeid(T),
typeid(T), out);
178 typename std::enable_if_t<
179 !std::is_array<T>::value &&
180 !std::is_enum<T>::value>* =
nullptr>
183 bool get(dw::core::StringView
const& key, std::vector<T>* out)
const
185 return get(
this, key,
typeid(std::vector<T>),
typeid(std::vector<T>), out);
195 typename std::enable_if_t<
196 std::is_array<T>::value &&
197 std::rank<T>::value == 1 &&
198 !std::is_enum<std::remove_extent_t<T>>::value>* =
nullptr>
201 bool get(dw::core::StringView
const& key, T* out)
const
203 static_assert(std::extent<T>::value > 0,
"Array must have size greater zero");
204 using TElement =
typename std::remove_extent_t<T>;
205 std::vector<TElement> value;
206 if (!
get(key, &value))
211 constexpr size_t size =
sizeof(T) /
sizeof(TElement);
212 if (value.size() != size)
214 throw Exception(DW_FAILURE,
"Array sizes don't match");
217 TElement* element = &(*out[0]);
218 for (
size_t i = 0; i < size; ++i)
220 *(element + i) = value[i];
232 typename std::enable_if_t<
233 std::is_array<T>::value &&
234 std::rank<T>::value == 1 &&
235 std::is_same<T, char8_t>::value>* =
nullptr>
238 bool get(dw::core::StringView
const& key, T* out)
const
240 static_assert(std::extent<T>::value > 0,
"Array must have size greater zero");
242 if (!
get(key, &value))
247 if (value.size() >= std::extent<T, 0>::value)
249 throw Exception(DW_FAILURE,
"Array sizes don't match");
253 strncat(out, value.c_str(), value.size());
264 typename std::enable_if_t<
265 std::is_array<T>::value && std::rank<T>::value == 2 &&
266 !std::is_enum<std::remove_all_extents_t<T>>::value>* =
nullptr>
269 bool get(dw::core::StringView
const& key, T* out)
const
271 static_assert(std::extent<T, 0>::value > 0,
"Array must have 1st dimension size greater zero");
272 static_assert(std::extent<T, 1>::value > 0,
"Array must have 2nd dimension size greater zero");
273 using TElement =
typename std::remove_all_extents_t<T>;
274 std::vector<TElement> value;
275 if (!
get(key, &value))
280 constexpr size_t size =
sizeof(T) /
sizeof(TElement);
281 if (value.size() != size)
283 throw Exception(DW_FAILURE,
"Array sizes don't match");
286 TElement* element = &(out[0][0]);
287 for (
size_t i = 0; i < size; ++i)
289 *(element + i) = value[i];
300 template <
typename S,
typename T>
303 if (!getOptional<S, T>(key, out))
305 throw Exception(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
315 template <
typename S,
typename T>
316 void getRequired(dw::core::StringView
const& key,
size_t const index, T* out)
const
318 if (!getOptional<S, T>(key, index, out))
320 throw Exception(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
330 template <
typename S,
typename T>
335 return get<S, T>(key, out);
341 throw Exception(e.
status(),
"Failed to get unnamed parameter with mangled semantic type: ",
typeid(S).name(),
" - ", e.
messageStr());
356 template <
typename S,
typename T>
357 bool getOptional(dw::core::StringView
const& key,
size_t const index, T* out)
const
361 return get<S, T>(key, index, out);
367 throw Exception(e.
status(),
"Failed to get unnamed parameter with mangled semantic type and index: ",
typeid(S).name(),
" ", index,
" - ", e.
messageStr());
371 throw Exception(e.
status(),
"Failed to get parameter with semantic by name and index: ", key,
" ", index,
" - ", e.
messageStr());
382 typename S,
typename T,
383 std::enable_if_t<!std::is_enum<T>::value>* =
nullptr>
386 bool get(dw::core::StringView
const& key, T* out)
const
388 static_assert(!std::is_same<T, dw::core::StringView>::value,
"T shouldn't be a dw::core::StringView, use FixedString<N> instead");
398 return get(
this, key,
typeid(S),
typeid(T), out);
407 typename S,
typename T,
size_t N,
408 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* =
nullptr>
411 bool get(dw::core::StringView
const& key, dw::core::FixedString<N>* out)
const
413 dw::core::StringView str;
414 const auto& semanticTypeInfo = std::is_same<S, dw::core::FixedString<N>>::value ?
typeid(dw::core::StringView) :
typeid(S);
415 bool success =
get(
this, key, semanticTypeInfo,
typeid(dw::core::StringView), &str);
420 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)");
422 out->copyFrom(str.data(), str.size());
435 typename S,
typename T,
436 std::enable_if_t<std::is_enum<T>::value>* =
nullptr>
439 bool get(dw::core::StringView
const& key, T* out)
const
444 return get(
this, key,
typeid(S),
typeid(T), out);
447 dw::core::StringView str;
448 if (!
get(
this, key,
typeid(dw::core::StringView),
typeid(dw::core::StringView), &str))
454 *out = mapEnumNameToValue<T>(str);
459 throw Exception(e.
status(),
"Failed to map enum name '", str,
"' for parameter '", key,
"' to numeric value: ", e.
messageStr());
469 typename S,
typename T,
470 std::enable_if_t<!std::is_enum<T>::value>* =
nullptr>
473 bool get(dw::core::StringView
const& key,
size_t const index, T* out)
const
475 return get(
this, key, index,
typeid(S),
typeid(T), out);
484 typename S,
typename T,
size_t N,
485 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* =
nullptr>
488 bool get(dw::core::StringView
const& key,
size_t const index, dw::core::FixedString<N>* out)
const
490 dw::core::StringView str;
491 const auto& semanticTypeInfo = std::is_same<S, dw::core::FixedString<N>>::value ?
typeid(dw::core::StringView) :
typeid(S);
492 bool success =
get(
this, key, index, semanticTypeInfo,
typeid(dw::core::StringView), &str);
497 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)");
499 out->copyFrom(str.data(), str.size());
512 typename S,
typename T,
513 std::enable_if_t<std::is_enum<T>::value>* =
nullptr>
516 bool get(dw::core::StringView
const& key,
size_t const index, T* out)
const
521 return get(
this, key, index,
typeid(S),
typeid(T), out);
524 dw::core::StringView str;
525 if (!
get(
this, key, index,
typeid(dw::core::StringView),
typeid(dw::core::StringView), &str))
531 *out = mapEnumNameToValue<T>(str);
536 throw Exception(e.
status(),
"Failed to map enum name '", str,
"' for parameter '", key,
"' and index ", index,
" to numeric value: ", e.
messageStr());
556 dw::core::StringView
const& key,
557 const std::type_info& semanticTypeInfo,
558 const std::type_info& dataTypeInfo,
559 void* out)
const = 0;
578 dw::core::StringView
const& key,
size_t const index,
579 const std::type_info& semanticTypeInfo,
580 const std::type_info& dataTypeInfo,
581 void* out)
const = 0;
char8_t const * messageStr() const noexcept
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