• <xmp id="om0om">
  • <table id="om0om"><noscript id="om0om"></noscript></table>
  • Compute Graph Framework SDK Reference  5.6
    All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
    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 // coverity[autosar_cpp14_a2_7_2_violation]
    392 // static_assert(!std::is_same<T, std::string>::value, "T shouldn't be a std::string, use FixedString<N> instead");
    393
    394 // 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
    395 // atm this is only used by custom parameter providers which provides values from a static singleton
    396 // static_assert(!std::is_same<T, const char*>::value, "T shouldn't be a C-style string, use FixedString<N> instead");
    397
    398 return get(this, key, typeid(S), typeid(T), out);
    399 }
    400
    406 template <
    407 typename S, typename T, size_t N,
    408 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* = nullptr>
    409 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
    410 // coverity[autosar_cpp14_a2_10_5_violation]
    411 bool get(dw::core::StringView const& key, dw::core::FixedString<N>* out) const
    412 {
    413 dw::core::StringView str;
    414 const auto& semanticTypeInfo = std::is_same<S, dw::core::FixedString<N>>::value ? typeid(dw::core::StringView) : typeid(S);
    415 bool success = get(this, key, semanticTypeInfo, typeid(dw::core::StringView), &str);
    416 if (success)
    417 {
    418 if (N <= str.size())
    419 {
    420 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)");
    421 }
    422 out->copyFrom(str.data(), str.size());
    423 }
    424 return success;
    425 }
    426
    434 template <
    435 typename S, typename T,
    436 std::enable_if_t<std::is_enum<T>::value>* = nullptr>
    437 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
    438 // coverity[autosar_cpp14_a2_10_5_violation]
    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 get(this, key, typeid(S), typeid(T), out);
    445 }
    446
    447 dw::core::StringView str;
    448 if (!get(this, key, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
    449 {
    450 return false;
    451 }
    452 try
    453 {
    454 *out = mapEnumNameToValue<T>(str);
    455 return true;
    456 }
    457 catch (Exception& e)
    458 {
    459 throw Exception(e.status(), "Failed to map enum name '", str, "' for parameter '", key, "' to numeric value: ", e.messageStr());
    460 }
    461 }
    462
    468 template <
    469 typename S, typename T,
    470 std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
    471 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
    472 // coverity[autosar_cpp14_a2_10_5_violation]
    473 bool get(dw::core::StringView const& key, size_t const index, T* out) const
    474 {
    475 return get(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 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
    487 // coverity[autosar_cpp14_a2_10_5_violation]
    488 bool get(dw::core::StringView const& key, size_t const index, dw::core::FixedString<N>* out) const
    489 {
    490 dw::core::StringView str;
    491 const auto& semanticTypeInfo = std::is_same<S, dw::core::FixedString<N>>::value ? typeid(dw::core::StringView) : typeid(S);
    492 bool success = get(this, key, index, semanticTypeInfo, typeid(dw::core::StringView), &str);
    493 if (success)
    494 {
    495 if (N <= str.size())
    496 {
    497 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)");
    498 }
    499 out->copyFrom(str.data(), str.size());
    500 }
    501 return success;
    502 }
    503
    511 template <
    512 typename S, typename T,
    513 std::enable_if_t<std::is_enum<T>::value>* = nullptr>
    514 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
    515 // coverity[autosar_cpp14_a2_10_5_violation]
    516 bool get(dw::core::StringView const& key, size_t const index, T* out) const
    517 {
    518 // get enum parameter from semantic parameter when key is empty
    519 if (key.empty())
    520 {
    521 return get(this, key, index, typeid(S), typeid(T), out);
    522 }
    523
    524 dw::core::StringView str;
    525 if (!get(this, key, index, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
    526 {
    527 return false;
    528 }
    529 try
    530 {
    531 *out = mapEnumNameToValue<T>(str);
    532 return true;
    533 }
    534 catch (Exception& e)
    535 {
    536 throw Exception(e.status(), "Failed to map enum name '", str, "' for parameter '", key, "' and index ", index, " to numeric value: ", e.messageStr());
    537 }
    538 }
    539
    552 // Overloaded functions are provided for ease of use
    553 // coverity[autosar_cpp14_a2_10_5_violation]
    554 virtual bool get(
    555 ParameterProvider const* const parentProvider,
    556 dw::core::StringView const& key,
    557 const std::type_info& semanticTypeInfo,
    558 const std::type_info& dataTypeInfo,
    559 void* out) const = 0;
    560
    574 // Overloaded functions are provided for ease of use
    575 // coverity[autosar_cpp14_a2_10_5_violation]
    576 virtual bool get(
    577 ParameterProvider const* const parentProvider,
    578 dw::core::StringView const& key, size_t const index,
    579 const std::type_info& semanticTypeInfo,
    580 const std::type_info& dataTypeInfo,
    581 void* out) const = 0;
    582};
    583
    584} // namespace framework
    585} // namespace dw
    586
    587#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
    人人超碰97caoporen国产