Compute Graph Framework SDK Reference  5.22
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-2024 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/Span.hpp>
39#include <dwshared/dwfoundation/dw/core/container/StringView.hpp>
40
41#include <cstdint>
42#include <string>
43#include <typeinfo>
44#include <vector>
45
46namespace dw
47{
48namespace framework
49{
50
53{
54protected:
63
64public:
66 ParameterProvider() = default;
68 virtual ~ParameterProvider() = default;
69
76 template <typename T>
77 void getRequired(dw::core::StringView const& key, T& out) const
78 {
79 if (!getOptional(key, out))
80 {
81 throw ExceptionWithStatus(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
82 }
83 }
84
91 template <typename T>
92 void getRequiredWithIndex(dw::core::StringView const& key, size_t const index, T& out) const
93 {
94 if (!getOptionalWithIndex(key, index, out))
95 {
96 throw ExceptionWithStatus(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
97 }
98 }
99
106 template <typename T>
107 bool getOptional(dw::core::StringView const& key, T& out) const
108 {
109 try
110 {
111 return get(key, out);
112 }
113 catch (ExceptionWithStatus& e)
114 {
115 throw ExceptionWithStatus(e.statusCode(), "Failed to get parameter by name: ", key, " - ", e.message());
116 }
117 }
118
125 template <typename T>
126 bool getOptionalWithIndex(dw::core::StringView const& key, size_t const index, T& out) const
127 {
128 try
129 {
130 return getWithIndex(key, index, out);
131 }
132 catch (ExceptionWithStatus& e)
133 {
134 throw ExceptionWithStatus(e.statusCode(), "Failed to get parameter by name and index: ", key, "[", index, "] - ", e.message());
135 }
136 }
137
143 template <
144 typename T,
145 typename std::enable_if_t<
146 !std::is_array<T>::value &&
147 !std::is_enum<T>::value>* = nullptr>
148 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
149 bool get(dw::core::StringView const& key, T& out) const
150 {
151 return getImpl(this, key, typeid(T), typeid(T), &out);
152 }
153
159 template <
160 typename T,
161 typename std::enable_if_t<
162 !std::is_array<T>::value &&
163 !std::is_enum<T>::value>* = nullptr>
164 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
165 bool getWithIndex(dw::core::StringView const& key, size_t const index, T& out) const
166 {
167 return getImplWithIndex(this, key, index, typeid(T), typeid(T), &out);
168 }
169
175 template <
176 typename T,
177 typename std::enable_if_t<
178 !std::is_array<T>::value &&
179 !std::is_enum<T>::value>* = nullptr>
180 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
181 bool get(dw::core::StringView const& key, std::vector<T>* out) const
182 {
183 return getImpl(this, key, typeid(std::vector<T>), typeid(std::vector<T>), &out);
184 }
185
191 template <
192 typename T,
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>
198 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
199 bool get(dw::core::StringView const& key, T& out) const
200 {
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))
205 {
206 return false;
207 }
208
209 constexpr size_t size{sizeof(T) / sizeof(TElement)};
210 if (value.size() != size)
211 {
212 throw ExceptionWithStatus(DW_FAILURE, "Array sizes don't match");
213 }
214
215 TElement* element{&out[0]};
216 dw::core::span<TElement> elementSpan{element, size};
217 for (size_t i{0U}; i < size; ++i)
218 {
219 elementSpan[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<std::remove_extent_t<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 dw::core::StringView 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 using TElement = typename std::remove_extent_t<T>;
251 TElement* element{&out[0]};
252 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
253 static_cast<void>(memcpy(element, value.data(), value.size()));
254 dw::core::span<TElement> elementSpan{element, std::extent<T, 0>::value};
255 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
256 elementSpan[value.size()] = static_cast<char8_t>(0);
257 return true;
258 }
259
265 template <
266 typename T,
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>
270 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
271 bool get(dw::core::StringView const& key, T& out) const
272 {
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))
278 {
279 return false;
280 }
281
282 constexpr size_t size{sizeof(T) / sizeof(TElement)};
283 if (value.size() != size)
284 {
285 throw ExceptionWithStatus(DW_FAILURE, "Array sizes don't match");
286 }
287
288 TElement* element{&out[0][0]};
289 dw::core::span<TElement> elementSpan{element, size};
290 for (size_t i{0U}; i < size; ++i)
291 {
292 elementSpan[i] = value[i];
293 }
294 return true;
295 }
296
303 template <typename S, typename T>
304 void getRequired(dw::core::StringView const& key, T& out) const
305 {
306 if (!getOptional<S, T>(key, out))
307 {
308 throw ExceptionWithStatus(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
309 }
310 }
311
318 template <typename S, typename T>
319 void getRequiredWithIndex(dw::core::StringView const& key, size_t const index, T& out) const
320 {
321 if (!getOptionalWithIndex<S, T>(key, index, out))
322 {
323 throw ExceptionWithStatus(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
324 }
325 }
326
333 template <typename S, typename T>
334 bool getOptional(dw::core::StringView const& key, T& out) const
335 {
336 try
337 {
338 // coverity[cert_err59_cpp_violation] RFD Pending: TID-2587
339 return get<S, T>(key, out);
340 }
341 catch (ExceptionWithStatus& e)
342 {
343 if (key.empty())
344 {
345 throw ExceptionWithStatus(e.statusCode(), "Failed to get unnamed parameter with mangled semantic type: ", typeid(S).name(), " - ", e.message());
346 }
347 else
348 {
349 throw ExceptionWithStatus(e.statusCode(), "Failed to get parameter with semantic by name: ", key, " - ", e.message());
350 }
351 }
352 }
353
360 template <typename S, typename T>
361 bool getOptionalWithIndex(dw::core::StringView const& key, size_t const index, T& out) const
362 {
363 try
364 {
365 // coverity[cert_err59_cpp_violation] RFD Pending: TID-2587
366 return getWithIndex<S, T>(key, index, out);
367 }
368 catch (ExceptionWithStatus& e)
369 {
370 if (key.empty())
371 {
372 throw ExceptionWithStatus(e.statusCode(), "Failed to get unnamed parameter with mangled semantic type and index: ", typeid(S).name(), " ", index, " - ", e.message());
373 }
374 else
375 {
376 throw ExceptionWithStatus(e.statusCode(), "Failed to get parameter with semantic by name and index: ", key, " ", index, " - ", e.message());
377 }
378 }
379 }
380
386 template <
387 typename S, typename T,
388 std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
389 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
390 bool get(dw::core::StringView const& key, T& out) const
391 {
392 static_assert(!std::is_same<T, dw::core::StringView>::value, "T shouldn't be a dw::core::StringView, use FixedString<N> instead");
393
394 static_assert(!std::is_same<T, std::string>::value, "T shouldn't be a std::string, use FixedString<N> instead");
395
396 // 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
397 // atm this is only used by custom parameter providers which provides values from a static singleton
398 // static_assert(!std::is_same<T, const char*>::value, "T shouldn't be a C-style string, use FixedString<N> instead");
399
400 return getImpl(this, key, typeid(S), typeid(T), &out);
401 }
402
408 template <
409 typename S, typename T, size_t N,
410 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* = nullptr>
411 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
412 bool get(dw::core::StringView const& key, dw::core::FixedString<N>& out) const
413 {
414 dw::core::StringView str{};
415 bool success{getImpl(this, key, getSemanticTypeInfo<S, N>(), typeid(dw::core::StringView), &str)};
416 if (success)
417 {
418 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
419 if (N <= str.size())
420 {
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)");
422 }
423 out.copyFrom(str.data(), str.size());
424 }
425 return success;
426 }
427
435 template <
436 typename S, typename T,
437 std::enable_if_t<std::is_enum<T>::value>* = nullptr>
438 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
439 bool get(dw::core::StringView const& key, T& out) const
440 {
441 // get enum parameter from semantic parameter when key is empty
442 if (key.empty())
443 {
444 return getImpl(this, key, typeid(S), typeid(T), &out);
445 }
446
447 dw::core::StringView str{};
448 if (!getImpl(this, key, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
449 {
450 return false;
451 }
452 try
453 {
454 // coverity[cert_err59_cpp_violation] RFD Pending: TID-2587
455 out = mapEnumNameToValue<T>(str);
456 return true;
457 }
458 catch (ExceptionWithStatus& e)
459 {
460 throw ExceptionWithStatus(e.statusCode(), "Failed to map enum name '", str, "' for parameter '", key, "' to numeric value: ", e.message());
461 }
462 }
463
469 template <
470 typename S, typename T,
471 std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
472 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
473 bool getWithIndex(dw::core::StringView const& key, size_t const index, T& out) const
474 {
475 return getImplWithIndex(this, key, index, typeid(S), typeid(T), &out);
476 }
477
483 template <
484 typename S, typename T, size_t N,
485 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* = nullptr>
486 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
487 bool getWithIndex(dw::core::StringView const& key, size_t const index, dw::core::FixedString<N>& out) const
488 {
489 dw::core::StringView str{};
490 bool success{getImplWithIndex(this, key, index, getSemanticTypeInfo<S, N>(), typeid(dw::core::StringView), &str)};
491 if (success)
492 {
493 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
494 if (N <= str.size())
495 {
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)");
497 }
498 out.copyFrom(str.data(), str.size());
499 }
500 return success;
501 }
502
510 template <
511 typename S, typename T,
512 std::enable_if_t<std::is_enum<T>::value>* = nullptr>
513 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
514 bool getWithIndex(dw::core::StringView const& key, size_t const index, T& out) const
515 {
516 // get enum parameter from semantic parameter when key is empty
517 if (key.empty())
518 {
519 return getImplWithIndex(this, key, index, typeid(S), typeid(T), &out);
520 }
521
522 dw::core::StringView str{};
523 if (!getImplWithIndex(this, key, index, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
524 {
525 return false;
526 }
527 try
528 {
529 // coverity[cert_err59_cpp_violation] RFD Pending: TID-2587
530 out = mapEnumNameToValue<T>(str);
531 return true;
532 }
533 catch (ExceptionWithStatus& e)
534 {
535 throw ExceptionWithStatus(e.statusCode(), "Failed to map enum name '", str, "' for parameter '", key, "' and index ", index, " to numeric value: ", e.message());
536 }
537 }
538
551 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
552 virtual bool getImpl(
553 ParameterProvider const* const parentProvider,
554 dw::core::StringView const& key,
555 const std::type_info& semanticTypeInfo,
556 const std::type_info& dataTypeInfo,
557 void* out) const = 0;
558
572 virtual bool getImplWithIndex(
573 ParameterProvider const* const parentProvider,
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;
578
579private:
580 // these two template specializations only exist since using a ternary expression instead
581 // results in a coverity violation [identical_branches] if S is dw::core::StringView
582 template <
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()
586 {
587 return typeid(dw::core::StringView);
588 }
589
590 template <
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()
594 {
595 return typeid(S);
596 }
597};
598
599} // namespace framework
600} // namespace dw
601
602#endif // DW_FRAMEWORK_PARAMETERPROVIDER_HPP_
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
Definition: Buffer.hpp:41