Compute Graph Framework SDK Reference  5.8
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-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
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 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 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
148 // coverity[autosar_cpp14_a2_10_5_violation]
149 bool get(dw::core::StringView const& key, T* out) const
150 {
151 return get(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 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
165 // coverity[autosar_cpp14_a2_10_5_violation]
166 bool get(dw::core::StringView const& key, size_t const index, T* out) const
167 {
168 return get(this, key, index, typeid(T), typeid(T), out);
169 }
170
176 template <
177 typename T,
178 typename std::enable_if_t<
179 !std::is_array<T>::value &&
180 !std::is_enum<T>::value>* = nullptr>
181 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
182 // coverity[autosar_cpp14_a2_10_5_violation]
183 bool get(dw::core::StringView const& key, std::vector<T>* out) const
184 {
185 return get(this, key, typeid(std::vector<T>), typeid(std::vector<T>), out);
186 }
187
193 template <
194 typename T,
195 typename std::enable_if_t<
196 std::is_array<T>::value &&
197 std::rank<T>::value == 1 &&
198 !std::is_enum<std::remove_extent_t<T>>::value>* = nullptr>
199 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
200 // coverity[autosar_cpp14_a2_10_5_violation]
201 bool get(dw::core::StringView const& key, T* out) const
202 {
203 static_assert(std::extent<T>::value > 0, "Array must have size greater zero");
204 using TElement = typename std::remove_extent_t<T>;
205 std::vector<TElement> value;
206 if (!get(key, &value))
207 {
208 return false;
209 }
210
211 constexpr size_t size = sizeof(T) / sizeof(TElement);
212 if (value.size() != size)
213 {
214 throw Exception(DW_FAILURE, "Array sizes don't match");
215 }
216
217 TElement* element = &(*out[0]);
218 for (size_t i = 0; i < size; ++i)
219 {
220 *(element + i) = value[i];
221 }
222 return true;
223 }
224
230 template <
231 typename T,
232 typename std::enable_if_t<
233 std::is_array<T>::value &&
234 std::rank<T>::value == 1 &&
235 std::is_same<T, char8_t>::value>* = nullptr>
236 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
237 // coverity[autosar_cpp14_a2_10_5_violation]
238 bool get(dw::core::StringView const& key, T* out) const
239 {
240 static_assert(std::extent<T>::value > 0, "Array must have size greater zero");
241 std::string value;
242 if (!get(key, &value))
243 {
244 return false;
245 }
246
247 if (value.size() >= std::extent<T, 0>::value)
248 {
249 throw Exception(DW_FAILURE, "Array sizes don't match");
250 }
251
252 out[0] = '\n';
253 strncat(out, value.c_str(), value.size());
254 return true;
255 }
256
262 template <
263 typename T,
264 typename std::enable_if_t<
265 std::is_array<T>::value && std::rank<T>::value == 2 &&
266 !std::is_enum<std::remove_all_extents_t<T>>::value>* = nullptr>
267 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
268 // coverity[autosar_cpp14_a2_10_5_violation]
269 bool get(dw::core::StringView const& key, T* out) const
270 {
271 static_assert(std::extent<T, 0>::value > 0, "Array must have 1st dimension size greater zero");
272 static_assert(std::extent<T, 1>::value > 0, "Array must have 2nd dimension size greater zero");
273 using TElement = typename std::remove_all_extents_t<T>;
274 std::vector<TElement> value;
275 if (!get(key, &value))
276 {
277 return false;
278 }
279
280 constexpr size_t size = sizeof(T) / sizeof(TElement);
281 if (value.size() != size)
282 {
283 throw Exception(DW_FAILURE, "Array sizes don't match");
284 }
285
286 TElement* element = &(out[0][0]);
287 for (size_t i = 0; i < size; ++i)
288 {
289 *(element + i) = value[i];
290 }
291 return true;
292 }
293
300 template <typename S, typename T>
301 void getRequired(dw::core::StringView const& key, T* out) const
302 {
303 if (!getOptional<S, T>(key, out))
304 {
305 throw Exception(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
306 }
307 }
308
315 template <typename S, typename T>
316 void getRequired(dw::core::StringView const& key, size_t const index, T* out) const
317 {
318 if (!getOptional<S, T>(key, index, out))
319 {
320 throw Exception(DW_NOT_AVAILABLE, "Required parameter not available: ", key);
321 }
322 }
323
330 template <typename S, typename T>
331 bool getOptional(dw::core::StringView const& key, T* out) const
332 {
333 try
334 {
335 return get<S, T>(key, out);
336 }
337 catch (Exception& e)
338 {
339 if (key.empty())
340 {
341 throw Exception(e.status(), "Failed to get unnamed parameter with mangled semantic type: ", typeid(S).name(), " - ", e.messageStr());
342 }
343 else
344 {
345 throw Exception(e.status(), "Failed to get parameter with semantic by name: ", key, " - ", e.messageStr());
346 }
347 }
348 }
349
356 template <typename S, typename T>
357 bool getOptional(dw::core::StringView const& key, size_t const index, T* out) const
358 {
359 try
360 {
361 return get<S, T>(key, index, out);
362 }
363 catch (Exception& e)
364 {
365 if (key.empty())
366 {
367 throw Exception(e.status(), "Failed to get unnamed parameter with mangled semantic type and index: ", typeid(S).name(), " ", index, " - ", e.messageStr());
368 }
369 else
370 {
371 throw Exception(e.status(), "Failed to get parameter with semantic by name and index: ", key, " ", index, " - ", e.messageStr());
372 }
373 }
374 }
375
381 template <
382 typename S, typename T,
383 std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
384 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
385 // coverity[autosar_cpp14_a2_10_5_violation]
386 bool get(dw::core::StringView const& key, T* out) const
387 {
388 static_assert(!std::is_same<T, dw::core::StringView>::value, "T shouldn't be a dw::core::StringView, use FixedString<N> instead");
389
390 // TODO(dirkt) to be commented in once all usage has been removed
391 // static_assert(!std::is_same<T, std::string>::value, "T shouldn't be a std::string, use FixedString<N> instead");
392
393 // 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
394 // atm this is only used by custom parameter providers which provides values from a static singleton
395 // static_assert(!std::is_same<T, const char*>::value, "T shouldn't be a C-style string, use FixedString<N> instead");
396
397 return get(this, key, typeid(S), typeid(T), out);
398 }
399
405 template <
406 typename S, typename T, size_t N,
407 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* = nullptr>
408 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
409 // coverity[autosar_cpp14_a2_10_5_violation]
410 bool get(dw::core::StringView const& key, dw::core::FixedString<N>* out) const
411 {
412 dw::core::StringView str;
413 const auto& semanticTypeInfo = std::is_same<S, dw::core::FixedString<N>>::value ? typeid(dw::core::StringView) : typeid(S);
414 bool success = get(this, key, semanticTypeInfo, typeid(dw::core::StringView), &str);
415 if (success)
416 {
417 if (N <= str.size())
418 {
419 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)");
420 }
421 out->copyFrom(str.data(), str.size());
422 }
423 return success;
424 }
425
433 template <
434 typename S, typename T,
435 std::enable_if_t<std::is_enum<T>::value>* = nullptr>
436 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
437 // coverity[autosar_cpp14_a2_10_5_violation]
438 bool get(dw::core::StringView const& key, T* out) const
439 {
440 // get enum parameter from semantic parameter when key is empty
441 if (key.empty())
442 {
443 return get(this, key, typeid(S), typeid(T), out);
444 }
445
446 dw::core::StringView str;
447 if (!get(this, key, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
448 {
449 return false;
450 }
451 try
452 {
453 *out = mapEnumNameToValue<T>(str);
454 return true;
455 }
456 catch (Exception& e)
457 {
458 throw Exception(e.status(), "Failed to map enum name '", str, "' for parameter '", key, "' to numeric value: ", e.messageStr());
459 }
460 }
461
467 template <
468 typename S, typename T,
469 std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
470 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
471 // coverity[autosar_cpp14_a2_10_5_violation]
472 bool get(dw::core::StringView const& key, size_t const index, T* out) const
473 {
474 return get(this, key, index, typeid(S), typeid(T), out);
475 }
476
482 template <
483 typename S, typename T, size_t N,
484 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* = nullptr>
485 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
486 // coverity[autosar_cpp14_a2_10_5_violation]
487 bool get(dw::core::StringView const& key, size_t const index, dw::core::FixedString<N>* out) const
488 {
489 dw::core::StringView str;
490 const auto& semanticTypeInfo = std::is_same<S, dw::core::FixedString<N>>::value ? typeid(dw::core::StringView) : typeid(S);
491 bool success = get(this, key, index, semanticTypeInfo, typeid(dw::core::StringView), &str);
492 if (success)
493 {
494 if (N <= str.size())
495 {
496 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)");
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 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
514 // coverity[autosar_cpp14_a2_10_5_violation]
515 bool get(dw::core::StringView const& key, size_t const index, T* out) const
516 {
517 // get enum parameter from semantic parameter when key is empty
518 if (key.empty())
519 {
520 return get(this, key, index, typeid(S), typeid(T), out);
521 }
522
523 dw::core::StringView str;
524 if (!get(this, key, index, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
525 {
526 return false;
527 }
528 try
529 {
530 *out = mapEnumNameToValue<T>(str);
531 return true;
532 }
533 catch (Exception& e)
534 {
535 throw Exception(e.status(), "Failed to map enum name '", str, "' for parameter '", key, "' and index ", index, " to numeric value: ", e.messageStr());
536 }
537 }
538
551 // Overloaded functions are provided for ease of use
552 // coverity[autosar_cpp14_a2_10_5_violation]
553 virtual bool get(
554 ParameterProvider const* const parentProvider,
555 dw::core::StringView const& key,
556 const std::type_info& semanticTypeInfo,
557 const std::type_info& dataTypeInfo,
558 void* out) const = 0;
559
573 // Overloaded functions are provided for ease of use
574 // coverity[autosar_cpp14_a2_10_5_violation]
575 virtual bool get(
576 ParameterProvider const* const parentProvider,
577 dw::core::StringView const& key, size_t const index,
578 const std::type_info& semanticTypeInfo,
579 const std::type_info& dataTypeInfo,
580 void* out) const = 0;
581};
582
583} // namespace framework
584} // namespace dw
585
586#endif // DW_FRAMEWORK_PARAMETERPROVIDER_HPP_
char8_t const * messageStr() const noexcept
Definition: Exception.hpp:84
dwStatus status() const
Definition: Exception.hpp:79
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: Exception.hpp:47