31#ifndef DW_FRAMEWORK_PARAMETERPROVIDER_HPP_
32#define DW_FRAMEWORK_PARAMETERPROVIDER_HPP_
38#include <dwshared/dwfoundation/dw/core/container/Span.hpp>
39#include <dwshared/dwfoundation/dw/core/container/StringView.hpp>
77 void getRequired(dw::core::StringView
const& key, T& out)
const
81 throw ExceptionWithStatus(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
96 throw ExceptionWithStatus(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
106 template <
typename T>
111 return get(key, out);
113 catch (ExceptionWithStatus& e)
115 throw ExceptionWithStatus(e.statusCode(),
"Failed to get parameter by name: ", key,
" - ", e.message());
125 template <
typename T>
132 catch (ExceptionWithStatus& e)
134 throw ExceptionWithStatus(e.statusCode(),
"Failed to get parameter by name and index: ", key,
"[", index,
"] - ", e.message());
145 typename std::enable_if_t<
146 !std::is_array<T>::value &&
147 !std::is_enum<T>::value>* =
nullptr>
149 bool get(dw::core::StringView
const& key, T& out)
const
151 return getImpl(
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>
165 bool getWithIndex(dw::core::StringView
const& key,
size_t const index, T& out)
const
177 typename std::enable_if_t<
178 !std::is_array<T>::value &&
179 !std::is_enum<T>::value>* =
nullptr>
181 bool get(dw::core::StringView
const& key, std::vector<T>* out)
const
183 return getImpl(
this, key,
typeid(std::vector<T>),
typeid(std::vector<T>), &out);
193 typename std::enable_if_t<
194 std::is_array<T>::value &&
195 std::rank<T>::value == 1 &&
196 !std::is_enum<std::remove_extent_t<T>>::value &&
197 !std::is_same<std::remove_extent_t<T>,
char8_t>::value>* =
nullptr>
199 bool get(dw::core::StringView
const& key, T& out)
const
201 static_assert(std::extent<T>::value > 0,
"Array must have size greater zero");
202 using TElement =
typename std::remove_extent_t<T>;
203 std::vector<TElement> value{};
204 if (!
get(key, value))
209 constexpr size_t size{
sizeof(T) /
sizeof(TElement)};
210 if (value.size() != size)
212 throw ExceptionWithStatus(DW_FAILURE,
"Array sizes don't match");
215 TElement* element{&out[0]};
216 dw::core::span<TElement> elementSpan{element, size};
217 for (
size_t i{0U}; i < size; ++i)
219 elementSpan[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<std::remove_extent_t<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");
239 dw::core::StringView value{};
240 if (!
get(key, value))
245 if (value.size() >= std::extent<T, 0>::value)
247 throw ExceptionWithStatus(DW_FAILURE,
"Array sizes don't match");
250 using TElement =
typename std::remove_extent_t<T>;
251 TElement* element{&out[0]};
253 static_cast<void>(memcpy(element, value.data(), value.size()));
254 dw::core::span<TElement> elementSpan{element, std::extent<T, 0>::value};
256 elementSpan[value.size()] =
static_cast<char8_t>(0);
267 typename std::enable_if_t<
268 std::is_array<T>::value && std::rank<T>::value == 2 &&
269 !std::is_enum<std::remove_all_extents_t<T>>::value>* =
nullptr>
271 bool get(dw::core::StringView
const& key, T& out)
const
273 static_assert(std::extent<T, 0>::value > 0,
"Array must have 1st dimension size greater zero");
274 static_assert(std::extent<T, 1>::value > 0,
"Array must have 2nd dimension size greater zero");
275 using TElement =
typename std::remove_all_extents_t<T>;
276 std::vector<TElement> value{};
277 if (!
get(key, value))
282 constexpr size_t size{
sizeof(T) /
sizeof(TElement)};
283 if (value.size() != size)
285 throw ExceptionWithStatus(DW_FAILURE,
"Array sizes don't match");
288 TElement* element{&out[0][0]};
289 dw::core::span<TElement> elementSpan{element, size};
290 for (
size_t i{0U}; i < size; ++i)
292 elementSpan[i] = value[i];
303 template <
typename S,
typename T>
306 if (!getOptional<S, T>(key, out))
308 throw ExceptionWithStatus(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
318 template <
typename S,
typename T>
321 if (!getOptionalWithIndex<S, T>(key, index, out))
323 throw ExceptionWithStatus(DW_NOT_AVAILABLE,
"Required parameter not available: ", key);
333 template <
typename S,
typename T>
339 return get<S, T>(key, out);
341 catch (ExceptionWithStatus& e)
345 throw ExceptionWithStatus(e.statusCode(),
"Failed to get unnamed parameter with mangled semantic type: ",
typeid(S).name(),
" - ", e.message());
349 throw ExceptionWithStatus(e.statusCode(),
"Failed to get parameter with semantic by name: ", key,
" - ", e.message());
360 template <
typename S,
typename T>
366 return getWithIndex<S, T>(key, index, out);
368 catch (ExceptionWithStatus& e)
372 throw ExceptionWithStatus(e.statusCode(),
"Failed to get unnamed parameter with mangled semantic type and index: ",
typeid(S).name(),
" ", index,
" - ", e.message());
376 throw ExceptionWithStatus(e.statusCode(),
"Failed to get parameter with semantic by name and index: ", key,
" ", index,
" - ", e.message());
387 typename S,
typename T,
388 std::enable_if_t<!std::is_enum<T>::value>* =
nullptr>
390 bool get(dw::core::StringView
const& key, T& out)
const
392 static_assert(!std::is_same<T, dw::core::StringView>::value,
"T shouldn't be a dw::core::StringView, use FixedString<N> instead");
394 static_assert(!std::is_same<T, std::string>::value,
"T shouldn't be a std::string, use FixedString<N> instead");
400 return getImpl(
this, key,
typeid(S),
typeid(T), &out);
409 typename S,
typename T,
size_t N,
410 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* =
nullptr>
412 bool get(dw::core::StringView
const& key, dw::core::FixedString<N>& out)
const
414 dw::core::StringView str{};
415 bool success{
getImpl(
this, key, getSemanticTypeInfo<S, N>(),
typeid(dw::core::StringView), &str)};
421 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)");
423 out.copyFrom(str.data(), str.size());
436 typename S,
typename T,
437 std::enable_if_t<std::is_enum<T>::value>* =
nullptr>
439 bool get(dw::core::StringView
const& key, T& out)
const
444 return getImpl(
this, key,
typeid(S),
typeid(T), &out);
447 dw::core::StringView str{};
448 if (!
getImpl(
this, key,
typeid(dw::core::StringView),
typeid(dw::core::StringView), &str))
455 out = mapEnumNameToValue<T>(str);
458 catch (ExceptionWithStatus& e)
460 throw ExceptionWithStatus(e.statusCode(),
"Failed to map enum name '", str,
"' for parameter '", key,
"' to numeric value: ", e.message());
470 typename S,
typename T,
471 std::enable_if_t<!std::is_enum<T>::value>* =
nullptr>
473 bool getWithIndex(dw::core::StringView
const& key,
size_t const index, T& out)
const
484 typename S,
typename T,
size_t N,
485 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* =
nullptr>
487 bool getWithIndex(dw::core::StringView
const& key,
size_t const index, dw::core::FixedString<N>& out)
const
489 dw::core::StringView str{};
490 bool success{
getImplWithIndex(
this, key, index, getSemanticTypeInfo<S, N>(),
typeid(dw::core::StringView), &str)};
496 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)");
498 out.copyFrom(str.data(), str.size());
511 typename S,
typename T,
512 std::enable_if_t<std::is_enum<T>::value>* =
nullptr>
514 bool getWithIndex(dw::core::StringView
const& key,
size_t const index, T& out)
const
522 dw::core::StringView str{};
523 if (!
getImplWithIndex(
this, key, index,
typeid(dw::core::StringView),
typeid(dw::core::StringView), &str))
530 out = mapEnumNameToValue<T>(str);
533 catch (ExceptionWithStatus& e)
535 throw ExceptionWithStatus(e.statusCode(),
"Failed to map enum name '", str,
"' for parameter '", key,
"' and index ", index,
" to numeric value: ", e.message());
554 dw::core::StringView
const& key,
555 const std::type_info& semanticTypeInfo,
556 const std::type_info& dataTypeInfo,
557 void* out)
const = 0;
574 dw::core::StringView
const& key,
size_t const index,
575 const std::type_info& semanticTypeInfo,
576 const std::type_info& dataTypeInfo,
577 void* out)
const = 0;
583 typename S,
size_t N,
584 std::enable_if_t<std::is_same<S, dw::core::FixedString<N>>::value>* =
nullptr>
585 static const std::type_info& getSemanticTypeInfo()
587 return typeid(dw::core::StringView);
591 typename S,
size_t N,
592 std::enable_if_t<!std::is_same<S, dw::core::FixedString<N>>::value>* =
nullptr>
593 static const std::type_info& getSemanticTypeInfo()
The interface to access parameter values identified by name and/or (semantic) type.
void getRequired(dw::core::StringView const &key, T &out) const
ParameterProvider & operator=(ParameterProvider const &) &=default
Copy assignment operator.
bool getOptional(dw::core::StringView const &key, T &out) const
bool getOptionalWithIndex(dw::core::StringView const &key, size_t const index, T &out) const
bool getWithIndex(dw::core::StringView const &key, size_t const index, dw::core::FixedString< N > &out) const
bool get(dw::core::StringView const &key, T &out) const
ParameterProvider(ParameterProvider const &)=default
Copy constructor.
bool getOptionalWithIndex(dw::core::StringView const &key, size_t const index, T &out) const
virtual ~ParameterProvider()=default
Destructor.
virtual bool getImpl(ParameterProvider const *const parentProvider, dw::core::StringView const &key, const std::type_info &semanticTypeInfo, const std::type_info &dataTypeInfo, void *out) const =0
ParameterProvider(ParameterProvider &&)=default
Move constructor.
virtual bool getImplWithIndex(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
bool getWithIndex(dw::core::StringView const &key, size_t const index, T &out) const
bool get(dw::core::StringView const &key, T &out) const
bool getWithIndex(dw::core::StringView const &key, size_t const index, T &out) const
ParameterProvider()=default
Default constructor.
void getRequired(dw::core::StringView const &key, T &out) const
bool getOptional(dw::core::StringView const &key, T &out) const
bool get(dw::core::StringView const &key, dw::core::FixedString< N > &out) const
void getRequiredWithIndex(dw::core::StringView const &key, size_t const index, T &out) const
ParameterProvider & operator=(ParameterProvider &&) &=default
Move assignment operator.
void getRequiredWithIndex(dw::core::StringView const &key, size_t const index, T &out) const
bool get(dw::core::StringView const &key, std::vector< T > *out) const