• <xmp id="om0om">
  • <table id="om0om"><noscript id="om0om"></noscript></table>
  • Compute Graph Framework SDK Reference  5.10
    All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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 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 (Exception& e)
    113 {
    114 throw ExceptionWithStatus(e.status(), "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 (Exception& e)
    132 {
    133 throw ExceptionWithStatus(e.status(), "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 // 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 ExceptionWithStatus(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 ExceptionWithStatus(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 ExceptionWithStatus(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 ExceptionWithStatus(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 ExceptionWithStatus(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 ExceptionWithStatus(e.status(), "Failed to get unnamed parameter with mangled semantic type: ", typeid(S).name(), " - ", e.message());
    342 }
    343 else
    344 {
    345 throw ExceptionWithStatus(e.status(), "Failed to get parameter with semantic by name: ", key, " - ", e.message());
    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 ExceptionWithStatus(e.status(), "Failed to get unnamed parameter with mangled semantic type and index: ", typeid(S).name(), " ", index, " - ", e.message());
    368 }
    369 else
    370 {
    371 throw ExceptionWithStatus(e.status(), "Failed to get parameter with semantic by name and index: ", key, " ", index, " - ", e.message());
    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 static_assert(!std::is_same<T, std::string>::value, "T shouldn't be a std::string, use FixedString<N> instead");
    391
    392 // 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
    393 // atm this is only used by custom parameter providers which provides values from a static singleton
    394 // static_assert(!std::is_same<T, const char*>::value, "T shouldn't be a C-style string, use FixedString<N> instead");
    395
    396 return get(this, key, typeid(S), typeid(T), out);
    397 }
    398
    404 template <
    405 typename S, typename T, size_t N,
    406 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* = nullptr>
    407 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
    408 // coverity[autosar_cpp14_a2_10_5_violation]
    409 bool get(dw::core::StringView const& key, dw::core::FixedString<N>* out) const
    410 {
    411 dw::core::StringView str;
    412 const auto& semanticTypeInfo = std::is_same<S, dw::core::FixedString<N>>::value ? typeid(dw::core::StringView) : typeid(S);
    413 bool success = get(this, key, semanticTypeInfo, typeid(dw::core::StringView), &str);
    414 if (success)
    415 {
    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() + 1, "(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 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
    436 // coverity[autosar_cpp14_a2_10_5_violation]
    437 bool get(dw::core::StringView const& key, T* out) const
    438 {
    439 // get enum parameter from semantic parameter when key is empty
    440 if (key.empty())
    441 {
    442 return get(this, key, typeid(S), typeid(T), out);
    443 }
    444
    445 dw::core::StringView str;
    446 if (!get(this, key, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
    447 {
    448 return false;
    449 }
    450 try
    451 {
    452 *out = mapEnumNameToValue<T>(str);
    453 return true;
    454 }
    455 catch (Exception& e)
    456 {
    457 throw ExceptionWithStatus(e.status(), "Failed to map enum name '", str, "' for parameter '", key, "' to numeric value: ", e.message());
    458 }
    459 }
    460
    466 template <
    467 typename S, typename T,
    468 std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
    469 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
    470 // coverity[autosar_cpp14_a2_10_5_violation]
    471 bool get(dw::core::StringView const& key, size_t const index, T* out) const
    472 {
    473 return get(this, key, index, typeid(S), typeid(T), out);
    474 }
    475
    481 template <
    482 typename S, typename T, size_t N,
    483 std::enable_if_t<std::is_same<T, dw::core::FixedString<N>>::value>* = nullptr>
    484 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
    485 // coverity[autosar_cpp14_a2_10_5_violation]
    486 bool get(dw::core::StringView const& key, size_t const index, dw::core::FixedString<N>* out) const
    487 {
    488 dw::core::StringView str;
    489 const auto& semanticTypeInfo = std::is_same<S, dw::core::FixedString<N>>::value ? typeid(dw::core::StringView) : typeid(S);
    490 bool success = get(this, key, index, semanticTypeInfo, typeid(dw::core::StringView), &str);
    491 if (success)
    492 {
    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() + 1, "(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 // TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
    513 // coverity[autosar_cpp14_a2_10_5_violation]
    514 bool get(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 get(this, key, index, typeid(S), typeid(T), out);
    520 }
    521
    522 dw::core::StringView str;
    523 if (!get(this, key, index, typeid(dw::core::StringView), typeid(dw::core::StringView), &str))
    524 {
    525 return false;
    526 }
    527 try
    528 {
    529 *out = mapEnumNameToValue<T>(str);
    530 return true;
    531 }
    532 catch (Exception& e)
    533 {
    534 throw ExceptionWithStatus(e.status(), "Failed to map enum name '", str, "' for parameter '", key, "' and index ", index, " to numeric value: ", e.message());
    535 }
    536 }
    537
    550 // Overloaded functions are provided for ease of use
    551 // coverity[autosar_cpp14_a2_10_5_violation]
    552 virtual bool get(
    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 // Overloaded functions are provided for ease of use
    573 // coverity[autosar_cpp14_a2_10_5_violation]
    574 virtual bool get(
    575 ParameterProvider const* const parentProvider,
    576 dw::core::StringView const& key, size_t const index,
    577 const std::type_info& semanticTypeInfo,
    578 const std::type_info& dataTypeInfo,
    579 void* out) const = 0;
    580};
    581
    582} // namespace framework
    583} // namespace dw
    584
    585#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
    人人超碰97caoporen国产