Compute Graph Framework SDK Reference  5.16
ParameterProvider.hpp
Go to the documentation of this file.
1
2//
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-2023 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 <dwshared/dwfoundation/dw/core/container/StringView.hpp>
39
40#include <cstdint>
41#include <string>
42#include <typeinfo>
43#include <vector>
44
45namespace dw
46{
47namespace framework
48{
49
52{
53protected:
62
63public:
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 ExceptionWithStatus(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 ExceptionWithStatus(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 (ExceptionWithStatus& e)
113 {
114 throw ExceptionWithStatus(e.statusCode(), "Failed to get parameter by name: ", key, " - ", e.message());
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 (ExceptionWithStatus& e)
132 {
133 throw ExceptionWithStatus(e.statusCode(), "Failed to get parameter by name and index: ", key, "[", index, "] - ", e.message());
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 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
148 bool get(dw::core::StringView const& key, T* out) const
149 {
150 return get(this, key, typeid(T), typeid(T), out);
151 }
152
158 template <
159 typename T,
160 typename std::enable_if_t<
161 !std::is_array<T>::value &&
162 !std::is_enum<T>::value>* = nullptr>
163 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
164 bool get(dw::core::StringView const& key, size_t const index, T* out) const
165 {
166 return get(this, key, index, typeid(T), typeid(T), out);
167 }
168
174 template <
175 typename T,
176 typename std::enable_if_t<
177 !std::is_array<T>::value &&
178 !std::is_enum<T>::value>* = nullptr>
179 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
180 bool get(dw::core::StringView const& key, std::vector<T>* out) const
181 {
182 return get(this, key, typeid(std::vector<T>), typeid(std::vector<T>), out);
183 }
184
190 template <
191 typename T,
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>
196 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
197 bool get(dw::core::StringView const& key, T* out) const
198 {
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))
203 {
204 return false;
205 }
206
207 constexpr size_t size{sizeof(T) / sizeof(TElement)};
208 if (value.size() != size)
209 {
210 throw ExceptionWithStatus(DW_FAILURE, "Array sizes don't match");
211 }
212
213 TElement* element{&(*out[0])};
214 for (size_t i{0U}; i < size; ++i)
215 {
216 // coverity[autosar_cpp14_a13_5_3_violation]
217 // coverity[autosar_cpp14_m5_0_15_violation]
218 *(element + i) = value[i];
219 }
220 return true;
221 }
222
228 template <
229 typename T,
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>
234 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
235 bool get(dw::core::StringView const& key, T* out) const
236 {
237 static_assert(std::extent<T>::value > 0, "Array must have size greater zero");
238 std::string value;
239 if (!get(key, &value))
240 {
241 return false;
242 }
243
244 if (value.size() >= std::extent<T, 0>::value)
245 {
246 throw ExceptionWithStatus(DW_FAILURE, "Array sizes don't match");
247 }
248
249 out[0] = '\n';
250 strncat(out, value.c_str(), value.size());
251 return true;
252 }
253
259 template <
260 typename T,
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>
264 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
265 bool get(dw::core::StringView const& key, T* out) const
266 {
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))
272 {
273 return false;
274 }
275
276 constexpr size_t size = sizeof(T) / sizeof(TElement);
277 if (value.size() != size)
278 {
279 throw ExceptionWithStatus(DW_FAILURE, "Array sizes don't match");
280 }
281
282 TElement* element = &(out[0][0]);
283 for (size_t i = 0; i < size; ++i)
284 {
285 *(element + i) = value[i];
286 }
287 return true;
288 }
289
296 template <typename S, typename T>
297 void getRequired(dw::core::StringView const& key, T* out) const
298 {
299 if (!getOptional<S, T>(key, out))
300 {
301 throw ExceptionWithStatus(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
302 }
303 }
304
311 template <typename S, typename T>
312 void getRequired(dw::core::StringView const& key, size_t const index, T* out) const
313 {
314 if (!getOptional<S, T>(key, index, out))
315 {
316 throw ExceptionWithStatus(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
317 }
318 }
319
326 template <typename S, typename T>
327 bool getOptional(dw::core::StringView const& key, T* out) const
328 {
329 try
330 {
331 // coverity[cert_err59_cpp_violation]
332 return get<S, T>(key, out);
333 }
334 catch (ExceptionWithStatus& e)
335 {
336 if (key.empty())
337 {
338 throw ExceptionWithStatus(e.statusCode(), "Failed to get unnamed parameter with mangled semantic type: ", typeid(S).name(), " - ", e.message());
339 }
340 else
341 {
342 throw ExceptionWithStatus(e.statusCode(), "Failed to get parameter with semantic by name: ", key, " - ", e.message());
343 }
344 }
345 }
346
353 template <typename S, typename T>
354 bool getOptional(dw::core::StringView const& key, size_t const index, T* out) const
355 {
356 try
357 {
358 // coverity[cert_err59_cpp_violation]
359 return get<S, T>(key, index, out);
360 }
361 catch (ExceptionWithStatus& e)
362 {
363 if (key.empty())
364 {
365 throw ExceptionWithStatus(e.statusCode(), "Failed to get unnamed parameter with mangled semantic type and index: ", typeid(S).name(), " ", index, " - ", e.message());
366 }
367 else
368 {
369 throw ExceptionWithStatus(e.statusCode(), "Failed to get parameter with semantic by name and index: ", key, " ", index, " - ", e.message());
370 }
371 }
372 }
373
379 template <
380 typename S, typename T,
381 std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
382 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
383 bool get(dw::core::StringView const& key, T* out) const
384 {
385 static_assert(!std::is_same<T, dw::core::StringView>::value, "T shouldn't be a dw::core::StringView, use FixedString<N> instead");
386
387 static_assert(!std::is_same<T, std::string>::value, "T shouldn't be a std::string, use FixedString<N> instead");
388
389 // 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
390 // atm this is only used by custom parameter providers which provides values from a static singleton
391 // static_assert(!std::is_same<T, const char*>::value, "T shouldn't be a C-style string, use FixedString<N> instead");
392
393 return get(this, key, typeid(S), typeid(T), out);
394 }
395
401 template <
402 typename S, typename T, size_t N,
403 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* = nullptr>
404 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
405 // coverity[autosar_cpp14_a8_4_10_violation]
406 bool get(dw::core::StringView const& key, dw::core::FixedString<N>* out) const
407 {
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)};
411 if (success)
412 {
413 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
414 if (N <= str.size())
415 {
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)");
417 }
418 out->copyFrom(str.data(), str.size());
419 }
420 return success;
421 }
422
430 template <
431 typename S, typename T,
432 std::enable_if_t<std::is_enum<T>::value>* = nullptr>
433 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
434 bool get(dw::core::StringView const& key, T* out) const
435 {
436 // get enum parameter from semantic parameter when key is empty
437 if (key.empty())
438 {
439 return get(this, key, typeid(S), typeid(T), out);
440 }
441
442 dw::core::StringView str;
443 if (!get(this, key, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
444 {
445 return false;
446 }
447 try
448 {
449 *out = mapEnumNameToValue<T>(str);
450 return true;
451 }
452 catch (ExceptionWithStatus& e)
453 {
454 throw ExceptionWithStatus(e.statusCode(), "Failed to map enum name '", str, "' for parameter '", key, "' to numeric value: ", e.message());
455 }
456 }
457
463 template <
464 typename S, typename T,
465 std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
466 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
467 bool get(dw::core::StringView const& key, size_t const index, T* out) const
468 {
469 return get(this, key, index, typeid(S), typeid(T), out);
470 }
471
477 template <
478 typename S, typename T, size_t N,
479 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* = nullptr>
480 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
481 // coverity[autosar_cpp14_a8_4_10_violation]
482 bool get(dw::core::StringView const& key, size_t const index, dw::core::FixedString<N>* out) const
483 {
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)};
487 if (success)
488 {
489 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
490 if (N <= str.size())
491 {
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)");
493 }
494 out->copyFrom(str.data(), str.size());
495 }
496 return success;
497 }
498
506 template <
507 typename S, typename T,
508 std::enable_if_t<std::is_enum<T>::value>* = nullptr>
509 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
510 bool get(dw::core::StringView const& key, size_t const index, T* out) const
511 {
512 // get enum parameter from semantic parameter when key is empty
513 if (key.empty())
514 {
515 return get(this, key, index, typeid(S), typeid(T), out);
516 }
517
518 dw::core::StringView str;
519 if (!get(this, key, index, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
520 {
521 return false;
522 }
523 try
524 {
525 *out = mapEnumNameToValue<T>(str);
526 return true;
527 }
528 catch (ExceptionWithStatus& e)
529 {
530 throw ExceptionWithStatus(e.statusCode(), "Failed to map enum name '", str, "' for parameter '", key, "' and index ", index, " to numeric value: ", e.message());
531 }
532 }
533
546 // Overloaded functions are provided for ease of use
547 // coverity[autosar_cpp14_a2_10_5_violation]
548 virtual bool get(
549 ParameterProvider const* const parentProvider,
550 dw::core::StringView const& key,
551 const std::type_info& semanticTypeInfo,
552 const std::type_info& dataTypeInfo,
553 void* out) const = 0;
554
568 // Overloaded functions are provided for ease of use
569 // coverity[autosar_cpp14_a2_10_5_violation]
570 virtual bool get(
571 ParameterProvider const* const parentProvider,
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;
576};
577
578} // namespace framework
579} // namespace dw
580
581#endif // DW_FRAMEWORK_PARAMETERPROVIDER_HPP_
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
Definition: Buffer.hpp:40