Compute Graph Framework SDK Reference
5.4.5418 Release
For Test and Development only

ParameterProvider.hpp
Go to the documentation of this file.
1 //
3 // Notice
4 // ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES
5 // NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
6 // THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT,
7 // MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 //
9 // NVIDIA CORPORATION & AFFILIATES assumes no responsibility for the consequences of use of such
10 // information or for any infringement of patents or other rights of third parties that may
11 // result from its use. No license is granted by implication or otherwise under any patent
12 // or patent rights of NVIDIA CORPORATION & AFFILIATES. No third party distribution is allowed unless
13 // expressly authorized by NVIDIA. Details are subject to change without notice.
14 // This code supersedes and replaces all information previously supplied.
15 // NVIDIA CORPORATION & AFFILIATES products are not authorized for use as critical
16 // components in life support devices or systems without express written approval of
17 // NVIDIA CORPORATION & AFFILIATES.
18 //
19 // SPDX-FileCopyrightText: Copyright (c) 2021-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
20 // SPDX-License-Identifier: LicenseRef-NvidiaProprietary
21 //
22 // NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
23 // property and proprietary rights in and to this material, related
24 // documentation and any modifications thereto. Any use, reproduction,
25 // disclosure or distribution of this material and related documentation
26 // without an express license agreement from NVIDIA CORPORATION or
27 // its affiliates is strictly prohibited.
28 //
30 
31 #ifndef DW_FRAMEWORK_PARAMETERPROVIDER_HPP_
32 #define DW_FRAMEWORK_PARAMETERPROVIDER_HPP_
33 
35 
36 #include <dwcgf/Exception.hpp>
37 
38 #include <dw/core/container/StringView.hpp>
39 
40 #include <cstdint>
41 #include <string>
42 #include <typeinfo>
43 #include <vector>
44 
45 namespace dw
46 {
47 namespace framework
48 {
49 
52 {
53 protected:
55  ParameterProvider(ParameterProvider const&) = default;
62 
63 public:
65  ParameterProvider() = default;
67  virtual ~ParameterProvider() = default;
68 
75  template <typename T>
76  void getRequired(dw::core::StringView const& key, T* out) const
77  {
78  if (!getOptional(key, out))
79  {
80  throw Exception(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
81  }
82  }
83 
90  template <typename T>
91  void getRequired(dw::core::StringView const& key, size_t const index, T* out) const
92  {
93  if (!getOptional(key, index, out))
94  {
95  throw Exception(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
96  }
97  }
98 
105  template <typename T>
106  bool getOptional(dw::core::StringView const& key, T* out) const
107  {
108  try
109  {
110  return get(key, out);
111  }
112  catch (Exception& e)
113  {
114  throw Exception(e.status(), "Failed to get parameter by name: ", key, " - ", e.messageStr());
115  }
116  }
117 
124  template <typename T>
125  bool getOptional(dw::core::StringView const& key, size_t const index, T* out) const
126  {
127  try
128  {
129  return get(key, index, out);
130  }
131  catch (Exception& e)
132  {
133  throw Exception(e.status(), "Failed to get parameter by name and index: ", key, "[", index, "] - ", e.messageStr());
134  }
135  }
136 
142  template <
143  typename T,
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
148  {
149  return get(this, key, typeid(T), typeid(T), out);
150  }
151 
157  template <
158  typename T,
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
163  {
164  return get(this, key, index, typeid(T), typeid(T), out);
165  }
166 
172  template <
173  typename T,
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
178  {
179  return get(this, key, typeid(std::vector<T>), typeid(std::vector<T>), out);
180  }
181 
187  template <
188  typename T,
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
194  {
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))
199  {
200  return false;
201  }
202 
203  constexpr size_t size = sizeof(T) / sizeof(TElement);
204  if (value.size() != size)
205  {
206  throw Exception(DW_FAILURE, "Array sizes don't match");
207  }
208 
209  TElement* element = &(*out[0]);
210  for (size_t i = 0; i < size; ++i)
211  {
212  *(element + i) = value[i];
213  }
214  return true;
215  }
216 
222  template <
223  typename T,
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
229  {
230  static_assert(std::extent<T>::value > 0, "Array must have size greater zero");
231  std::string value;
232  if (!get(key, &value))
233  {
234  return false;
235  }
236 
237  if (value.size() >= std::extent<T, 0>::value)
238  {
239  throw Exception(DW_FAILURE, "Array sizes don't match");
240  }
241 
242  out[0] = '\n';
243  strncat(out, value.c_str(), value.size());
244  return true;
245  }
246 
252  template <
253  typename T,
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
258  {
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))
264  {
265  return false;
266  }
267 
268  constexpr size_t size = sizeof(T) / sizeof(TElement);
269  if (value.size() != size)
270  {
271  throw Exception(DW_FAILURE, "Array sizes don't match");
272  }
273 
274  TElement* element = &(out[0][0]);
275  for (size_t i = 0; i < size; ++i)
276  {
277  *(element + i) = value[i];
278  }
279  return true;
280  }
281 
288  template <typename S, typename T>
289  void getRequired(dw::core::StringView const& key, T* out) const
290  {
291  if (!getOptional<S, T>(key, out))
292  {
293  throw Exception(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
294  }
295  }
296 
303  template <typename S, typename T>
304  void getRequired(dw::core::StringView const& key, size_t const index, T* out) const
305  {
306  if (!getOptional<S, T>(key, index, out))
307  {
308  throw Exception(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
309  }
310  }
311 
318  template <typename S, typename T>
319  bool getOptional(dw::core::StringView const& key, T* out) const
320  {
321  try
322  {
323  return get<S, T>(key, out);
324  }
325  catch (Exception& e)
326  {
327  if (key.empty())
328  {
329  throw Exception(e.status(), "Failed to get unnamed parameter with mangled semantic type: ", typeid(S).name(), " - ", e.messageStr());
330  }
331  else
332  {
333  throw Exception(e.status(), "Failed to get parameter with semantic by name: ", key, " - ", e.messageStr());
334  }
335  }
336  }
337 
344  template <typename S, typename T>
345  bool getOptional(dw::core::StringView const& key, size_t const index, T* out) const
346  {
347  try
348  {
349  return get<S, T>(key, index, out);
350  }
351  catch (Exception& e)
352  {
353  if (key.empty())
354  {
355  throw Exception(e.status(), "Failed to get unnamed parameter with mangled semantic type and index: ", typeid(S).name(), " ", index, " - ", e.messageStr());
356  }
357  else
358  {
359  throw Exception(e.status(), "Failed to get parameter with semantic by name and index: ", key, " ", index, " - ", e.messageStr());
360  }
361  }
362  }
363 
369  template <
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
373  {
374  static_assert(!std::is_same<T, dw::core::StringView>::value, "T shouldn't be a dw::core::StringView, use FixedString<N> instead");
375 
376  // TODO(dirkt) to be commented in once all usage has been removed
377  // coverity[autosar_cpp14_a2_7_2_violation]
378  // static_assert(!std::is_same<T, std::string>::value, "T shouldn't be a std::string, use FixedString<N> instead");
379 
380  // as long as the parameter provider makes sure that the const char* is valid throughout the life time of the parameter struct this is fine
381  // atm this is only used by custom parameter providers which provides values from a static singleton
382  // static_assert(!std::is_same<T, const char*>::value, "T shouldn't be a C-style string, use FixedString<N> instead");
383 
384  return get(this, key, typeid(S), typeid(T), out);
385  }
386 
392  template <
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
396  {
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);
400  if (success)
401  {
402  if (N <= str.size())
403  {
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)");
405  }
406  out->copyFrom(str.data(), str.size());
407  }
408  return success;
409  }
410 
418  template <
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
422  {
423  // get enum parameter from semantic parameter when key is empty
424  if (key.empty())
425  {
426  return get(this, key, typeid(S), typeid(T), out);
427  }
428 
429  dw::core::StringView str;
430  if (!get(this, key, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
431  {
432  return false;
433  }
434  try
435  {
436  *out = mapEnumNameToValue<T>(str);
437  return true;
438  }
439  catch (Exception& e)
440  {
441  throw Exception(e.status(), "Failed to map enum name '", str, "' for parameter '", key, "' to numeric value: ", e.messageStr());
442  }
443  }
444 
450  template <
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
454  {
455  return get(this, key, index, typeid(S), typeid(T), out);
456  }
457 
463  template <
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
467  {
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);
471  if (success)
472  {
473  if (N <= str.size())
474  {
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)");
476  }
477  out->copyFrom(str.data(), str.size());
478  }
479  return success;
480  }
481 
489  template <
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
493  {
494  // get enum parameter from semantic parameter when key is empty
495  if (key.empty())
496  {
497  return get(this, key, index, typeid(S), typeid(T), out);
498  }
499 
500  dw::core::StringView str;
501  if (!get(this, key, index, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
502  {
503  return false;
504  }
505  try
506  {
507  *out = mapEnumNameToValue<T>(str);
508  return true;
509  }
510  catch (Exception& e)
511  {
512  throw Exception(e.status(), "Failed to map enum name '", str, "' for parameter '", key, "' and index ", index, " to numeric value: ", e.messageStr());
513  }
514  }
515 
528  virtual bool get(
529  ParameterProvider const* const parentProvider,
530  dw::core::StringView const& key,
531  const std::type_info& semanticTypeInfo,
532  const std::type_info& dataTypeInfo,
533  void* out) const = 0;
534 
548  virtual bool get(
549  ParameterProvider const* const parentProvider,
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;
554 };
555 
556 } // namespace framework
557 } // namespace dw
558 
559 #endif // DW_FRAMEWORK_PARAMETERPROVIDER_HPP_
char8_t const * messageStr() const noexcept
Definition: Exception.hpp:84
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.
dwStatus status() const
Definition: Exception.hpp:79
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.
Definition: Exception.hpp:46
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.