Compute Graph Framework SDK Reference  5.14
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 // coverity[cert_err56_cpp_violation]
211 throw ExceptionWithStatus(DW_FAILURE, "Array sizes don't match");
212 }
213
214 TElement* element{&(*out[0])};
215 for (size_t i{0U}; i < size; ++i)
216 {
217 // coverity[autosar_cpp14_a13_5_3_violation]
218 // coverity[autosar_cpp14_m5_0_15_violation]
219 *(element + i) = value[i];
220 }
221 return true;
222 }
223
229 template <
230 typename T,
231 typename std::enable_if_t<
232 std::is_array<T>::value &&
233 std::rank<T>::value == 1 &&
234 std::is_same<T, char8_t>::value>* = nullptr>
235 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
236 bool get(dw::core::StringView const& key, T* out) const
237 {
238 static_assert(std::extent<T>::value > 0, "Array must have size greater zero");
239 std::string value;
240 if (!get(key, &value))
241 {
242 return false;
243 }
244
245 if (value.size() >= std::extent<T, 0>::value)
246 {
247 throw ExceptionWithStatus(DW_FAILURE, "Array sizes don't match");
248 }
249
250 out[0] = '\n';
251 strncat(out, value.c_str(), value.size());
252 return true;
253 }
254
260 template <
261 typename T,
262 typename std::enable_if_t<
263 std::is_array<T>::value && std::rank<T>::value == 2 &&
264 !std::is_enum<std::remove_all_extents_t<T>>::value>* = nullptr>
265 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
266 bool get(dw::core::StringView const& key, T* out) const
267 {
268 static_assert(std::extent<T, 0>::value > 0, "Array must have 1st dimension size greater zero");
269 static_assert(std::extent<T, 1>::value > 0, "Array must have 2nd dimension size greater zero");
270 using TElement = typename std::remove_all_extents_t<T>;
271 std::vector<TElement> value;
272 if (!get(key, &value))
273 {
274 return false;
275 }
276
277 constexpr size_t size = sizeof(T) / sizeof(TElement);
278 if (value.size() != size)
279 {
280 throw ExceptionWithStatus(DW_FAILURE, "Array sizes don't match");
281 }
282
283 TElement* element = &(out[0][0]);
284 for (size_t i = 0; i < size; ++i)
285 {
286 *(element + i) = value[i];
287 }
288 return true;
289 }
290
297 template <typename S, typename T>
298 void getRequired(dw::core::StringView const& key, T* out) const
299 {
300 if (!getOptional<S, T>(key, out))
301 {
302 throw ExceptionWithStatus(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
303 }
304 }
305
312 template <typename S, typename T>
313 void getRequired(dw::core::StringView const& key, size_t const index, T* out) const
314 {
315 if (!getOptional<S, T>(key, index, out))
316 {
317 throw ExceptionWithStatus(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
318 }
319 }
320
327 template <typename S, typename T>
328 bool getOptional(dw::core::StringView const& key, T* out) const
329 {
330 try
331 {
332 // coverity[cert_err59_cpp_violation]
333 return get<S, T>(key, out);
334 }
335 catch (ExceptionWithStatus& e)
336 {
337 if (key.empty())
338 {
339 throw ExceptionWithStatus(e.statusCode(), "Failed to get unnamed parameter with mangled semantic type: ", typeid(S).name(), " - ", e.message());
340 }
341 else
342 {
343 throw ExceptionWithStatus(e.statusCode(), "Failed to get parameter with semantic by name: ", key, " - ", e.message());
344 }
345 }
346 }
347
354 template <typename S, typename T>
355 bool getOptional(dw::core::StringView const& key, size_t const index, T* out) const
356 {
357 try
358 {
359 // coverity[cert_err59_cpp_violation]
360 return get<S, T>(key, index, out);
361 }
362 catch (ExceptionWithStatus& e)
363 {
364 if (key.empty())
365 {
366 throw ExceptionWithStatus(e.statusCode(), "Failed to get unnamed parameter with mangled semantic type and index: ", typeid(S).name(), " ", index, " - ", e.message());
367 }
368 else
369 {
370 throw ExceptionWithStatus(e.statusCode(), "Failed to get parameter with semantic by name and index: ", key, " ", index, " - ", e.message());
371 }
372 }
373 }
374
380 template <
381 typename S, typename T,
382 std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
383 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
384 bool get(dw::core::StringView const& key, T* out) const
385 {
386 static_assert(!std::is_same<T, dw::core::StringView>::value, "T shouldn't be a dw::core::StringView, use FixedString<N> instead");
387
388 static_assert(!std::is_same<T, std::string>::value, "T shouldn't be a std::string, use FixedString<N> instead");
389
390 // 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
391 // atm this is only used by custom parameter providers which provides values from a static singleton
392 // static_assert(!std::is_same<T, const char*>::value, "T shouldn't be a C-style string, use FixedString<N> instead");
393
394 return get(this, key, typeid(S), typeid(T), out);
395 }
396
402 template <
403 typename S, typename T, size_t N,
404 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* = nullptr>
405 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
406 // coverity[autosar_cpp14_a8_4_10_violation]
407 bool get(dw::core::StringView const& key, dw::core::FixedString<N>* out) const
408 {
409 dw::core::StringView str{};
410 // coverity[autosar_cpp14_a8_5_3_violation]
411 const auto& semanticTypeInfo{std::is_same<S, dw::core::FixedString<N>>::value ? typeid(dw::core::StringView) : typeid(S)};
412 bool success{get(this, key, semanticTypeInfo, typeid(dw::core::StringView), &str)};
413 if (success)
414 {
415 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
416 if (N <= str.size())
417 {
418 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)");
419 }
420 out->copyFrom(str.data(), str.size());
421 }
422 return success;
423 }
424
432 template <
433 typename S, typename T,
434 std::enable_if_t<std::is_enum<T>::value>* = nullptr>
435 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
436 bool get(dw::core::StringView const& key, T* out) const
437 {
438 // get enum parameter from semantic parameter when key is empty
439 if (key.empty())
440 {
441 return get(this, key, typeid(S), typeid(T), out);
442 }
443
444 dw::core::StringView str;
445 if (!get(this, key, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
446 {
447 return false;
448 }
449 try
450 {
451 *out = mapEnumNameToValue<T>(str);
452 return true;
453 }
454 catch (ExceptionWithStatus& e)
455 {
456 throw ExceptionWithStatus(e.statusCode(), "Failed to map enum name '", str, "' for parameter '", key, "' to numeric value: ", e.message());
457 }
458 }
459
465 template <
466 typename S, typename T,
467 std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
468 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
469 bool get(dw::core::StringView const& key, size_t const index, T* out) const
470 {
471 return get(this, key, index, typeid(S), typeid(T), out);
472 }
473
479 template <
480 typename S, typename T, size_t N,
481 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* = nullptr>
482 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
483 // coverity[autosar_cpp14_a8_4_10_violation]
484 bool get(dw::core::StringView const& key, size_t const index, dw::core::FixedString<N>* out) const
485 {
486 dw::core::StringView str{};
487 // coverity[autosar_cpp14_a8_5_3_violation]
488 const auto& semanticTypeInfo{std::is_same<S, dw::core::FixedString<N>>::value ? typeid(dw::core::StringView) : typeid(S)};
489 bool success{get(this, key, index, semanticTypeInfo, typeid(dw::core::StringView), &str)};
490 if (success)
491 {
492 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
493 if (N <= str.size())
494 {
495 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)");
496 }
497 out->copyFrom(str.data(), str.size());
498 }
499 return success;
500 }
501
509 template <
510 typename S, typename T,
511 std::enable_if_t<std::is_enum<T>::value>* = nullptr>
512 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
513 bool get(dw::core::StringView const& key, size_t const index, T* out) const
514 {
515 // get enum parameter from semantic parameter when key is empty
516 if (key.empty())
517 {
518 return get(this, key, index, typeid(S), typeid(T), out);
519 }
520
521 dw::core::StringView str;
522 if (!get(this, key, index, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
523 {
524 return false;
525 }
526 try
527 {
528 *out = mapEnumNameToValue<T>(str);
529 return true;
530 }
531 catch (ExceptionWithStatus& e)
532 {
533 throw ExceptionWithStatus(e.statusCode(), "Failed to map enum name '", str, "' for parameter '", key, "' and index ", index, " to numeric value: ", e.message());
534 }
535 }
536
549 // Overloaded functions are provided for ease of use
550 // coverity[autosar_cpp14_a2_10_5_violation]
551 virtual bool get(
552 ParameterProvider const* const parentProvider,
553 dw::core::StringView const& key,
554 const std::type_info& semanticTypeInfo,
555 const std::type_info& dataTypeInfo,
556 void* out) const = 0;
557
571 // Overloaded functions are provided for ease of use
572 // coverity[autosar_cpp14_a2_10_5_violation]
573 virtual bool get(
574 ParameterProvider const* const parentProvider,
575 dw::core::StringView const& key, size_t const index,
576 const std::type_info& semanticTypeInfo,
577 const std::type_info& dataTypeInfo,
578 void* out) const = 0;
579};
580
581} // namespace framework
582} // namespace dw
583
584#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