Compute Graph Framework SDK Reference  5.12
ParameterCollectionDescriptor.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_PARAMETERCOLLECTIONDESCRIPTOR_HPP_
32#define DW_FRAMEWORK_PARAMETERCOLLECTIONDESCRIPTOR_HPP_
33
35
36#include <dwshared/dwfoundation/dw/core/container/VectorFixed.hpp>
37
38#include <modern-json/json.hpp>
39
40#include <memory>
41
42namespace dw
43{
44namespace framework
45{
46
49{
50public:
53 dw::core::StringView const& name,
54 dw::core::StringView const& typeName,
55 const bool isIndex,
56 size_t const arraySize) noexcept;
58 virtual ~ParameterDescriptor() = default;
68 dw::core::StringView const& getName() const noexcept;
70 dw::core::StringView const& getTypeName() const noexcept;
72 bool isIndex() const noexcept;
74 size_t getArraySize() const noexcept;
76 virtual void addDefault(nlohmann::ordered_json& object) const;
77
78private:
80 dw::core::StringView m_name;
82 dw::core::StringView m_typeName;
84 bool m_isIndex;
86 size_t m_arraySize;
87};
88
89namespace detail
90{
91
93template <
94 typename DefaultType,
95 std::enable_if_t<
96 !std::is_enum<DefaultType>::value>* = nullptr>
97// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
98nlohmann::json getDefaultValueForJson(DefaultType defaultValue)
99{
100 return nlohmann::json(defaultValue);
101}
102
104template <
105 typename DefaultType,
106 std::enable_if_t<
107 std::is_enum<DefaultType>::value>* = nullptr>
108// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
109nlohmann::json getDefaultValueForJson(DefaultType defaultValue)
110{
111 StringView name{mapEnumValueToName<DefaultType>(defaultValue)};
112 std::string nameStr{name.data(), name.size()};
113 return nlohmann::json(nameStr);
114}
115
117template <
118 typename DefaultType, size_t N,
119 std::enable_if_t<
120 std::is_enum<DefaultType>::value>* = nullptr>
121// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
122nlohmann::json getDefaultValueForJson(const std::array<DefaultType, N>& defaultValues)
123{
124 std::array<std::string, N> nameStrings{};
125 // coverity[autosar_cpp14_a5_1_1_violation]
126 for (size_t i{0U}; i < N; ++i)
127 {
128 StringView name{mapEnumValueToName<DefaultType>(defaultValues[i])};
129 std::string nameStr{name.data(), name.size()};
130 nameStrings[i] = std::move(nameStr);
131 }
132 return nlohmann::json(nameStrings);
133}
134
135} // namespace detail
136
138// static_assert needed covering all possible type accepted by nlohmann::json()
139// beside enums and std::arrays of such types
140// coverity[autosar_cpp14_a14_1_1_violation]
141template <typename DefaultType>
143{
144
145public:
148 dw::core::StringView name,
149 dw::core::StringView typeName,
150 bool isIndex,
151 size_t arraySize,
152 DefaultType defaultValue)
153 : ParameterDescriptor(name, typeName, isIndex, arraySize)
154 , m_defaultValue(defaultValue)
155 {
156 }
168 void addDefault(nlohmann::ordered_json& object) const override
169 {
170 // coverity[autosar_cpp14_a5_1_1_violation]
171 object["default"] = detail::getDefaultValueForJson(m_defaultValue);
172 }
173
174private:
176 DefaultType m_defaultValue;
177};
178
181{
182public:
184 explicit ParameterCollectionDescriptor(size_t const capacity);
185
187 size_t getSize() const;
188
190 const std::shared_ptr<const ParameterDescriptor>& getDescriptor(size_t const index) const;
191
197 size_t getIndex(dw::core::StringView const& identifier) const;
198
204 const std::shared_ptr<const ParameterDescriptor>& getDescriptor(dw::core::StringView const& identifier) const;
205
207 bool isValid(size_t const index) const;
208
210 bool isValid(dw::core::StringView const& identifier) const noexcept;
211
217 void addDescriptor(const std::shared_ptr<const ParameterDescriptor>& descriptor);
218
220 template <
221 typename NodeT, size_t ConstructorArgumentIndex,
222 typename std::enable_if_t<ConstructorArgumentIndex == std::tuple_size<decltype(describeParameters<NodeT>())>::value, void>* = nullptr>
223 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
224 // coverity[autosar_cpp14_m0_1_8_violation]
226 {
227 }
228
230 template <
231 typename NodeT, size_t ConstructorArgumentIndex,
232 typename std::enable_if_t<ConstructorArgumentIndex<std::tuple_size<decltype(describeParameters<NodeT>())>::value, void>* = nullptr>
233 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
234 void addDescriptors()
235 {
236 // coverity[autosar_cpp14_a8_5_2_violation]
237 auto t = std::get<ConstructorArgumentIndex>(describeParameters<NodeT>());
238 // coverity[autosar_cpp14_a8_5_2_violation]
239 const auto& t2 = std::get<PARAMETER_CONSTRUCTOR_ARGUMENT_DESCRIPTOR>(t);
240 // all parameters of this constructor argument
241 addDescriptors<0>(t2);
242 // next constructor argument
243 addDescriptors<NodeT, ConstructorArgumentIndex + 1>();
244 }
245
246private:
248 template <
249 size_t ParameterIndex, typename ParamsT,
250 typename std::enable_if_t<ParameterIndex == dw::core::tuple_size<ParamsT>::value, void>* = nullptr>
251 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
252 void addDescriptors(const ParamsT& params)
253 {
254 static_cast<void>(params);
255 }
256
258 template <
259 size_t ParameterIndex, typename ParamsT,
260 typename std::enable_if_t<ParameterIndex<dw::core::tuple_size<ParamsT>::value, void>* = nullptr>
261 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
262 void addDescriptors(const ParamsT& params)
263 {
264 // coverity[autosar_cpp14_a8_5_2_violation]
265 const auto& p = dw::core::get<ParameterIndex>(params);
266 addDescriptor(p);
267 addDescriptors<ParameterIndex + 1>(params);
268 }
269
271 template <
272 typename ParamT,
273 typename std::enable_if_t<std::tuple_size<ParamT>::value <= PARAMETER_DEFAULT_VALUE, void>* = nullptr>
274 void addDescriptor(const ParamT& param)
275 {
276 // coverity[autosar_cpp14_a8_5_2_violation]
277 const auto d = std::make_shared<const ParameterDescriptor>(
278 std::get<PARAMETER_NAME>(param),
279 std::get<PARAMETER_TYPE_NAME>(param),
280 std::tuple_element_t<PARAMETER_IS_INDEX, ParamT>::value,
281 std::tuple_element_t<PARAMETER_ARRAY_SIZE, ParamT>::value);
282 addDescriptor(d);
283 }
284
286 template <
287 typename ParamT,
288 typename std::enable_if_t<PARAMETER_DEFAULT_VALUE<std::tuple_size<ParamT>::value, void>* = nullptr> void addDescriptor(const ParamT& param)
289 {
290 using DefaultT = typename std::tuple_element_t<PARAMETER_DEFAULT_VALUE, ParamT>;
291 // coverity[autosar_cpp14_a8_5_2_violation]
292 const auto d = std::make_shared<const ParameterDescriptorWithDefault<DefaultT>>(
293 std::get<PARAMETER_NAME>(param),
294 std::get<PARAMETER_TYPE_NAME>(param),
295 std::tuple_element_t<PARAMETER_IS_INDEX, ParamT>::value,
296 std::tuple_element_t<PARAMETER_ARRAY_SIZE, ParamT>::value,
297 std::get<PARAMETER_DEFAULT_VALUE>(param));
298 addDescriptor(d);
299 }
300
302 dw::core::VectorFixed<std::shared_ptr<const ParameterDescriptor>> m_descriptors;
303};
304
306template <typename NodeT>
307// coverity[autosar_cpp14_a2_10_4_violation]
308// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
310{
311 ParameterCollectionDescriptor d{parameterSize<NodeT>()};
312 d.addDescriptors<NodeT, 0>();
313 return d;
314}
315
316} // namespace framework
317} // namespace dw
318
319#endif // DW_FRAMEWORK_PARAMETERCOLLECTIONDESCRIPTOR_HPP_
const std::shared_ptr< const ParameterDescriptor > & getDescriptor(size_t const index) const
Get the parameter descriptor for the passed index.
size_t getSize() const
Get the number of parameter descriptors.
void addDescriptors()
Terminate recursion to add parameter descriptors for each node constructor argument to the collection...
bool isValid(dw::core::StringView const &identifier) const noexcept
Check if the passed parameter name matches a parameter descriptor in the collection.
void addDescriptor(const std::shared_ptr< const ParameterDescriptor > &descriptor)
bool isValid(size_t const index) const
Check if the passed index is valid, which mean is within [0, size()).
const std::shared_ptr< const ParameterDescriptor > & getDescriptor(dw::core::StringView const &identifier) const
ParameterCollectionDescriptor(size_t const capacity)
Constructor.
size_t getIndex(dw::core::StringView const &identifier) const
The description of a parameter with a default value.
auto operator=(ParameterDescriptorWithDefault &&) -> ParameterDescriptorWithDefault &=delete
Move assignment operator.
~ParameterDescriptorWithDefault() override=default
Destructor.
auto operator=(ParameterDescriptorWithDefault const &) -> ParameterDescriptorWithDefault &=delete
Copy assignment operator.
ParameterDescriptorWithDefault(dw::core::StringView name, dw::core::StringView typeName, bool isIndex, size_t arraySize, DefaultType defaultValue)
Constructor.
void addDefault(nlohmann::ordered_json &object) const override
Add the default value to the passed JSON object.
ParameterDescriptorWithDefault(ParameterDescriptorWithDefault &&)=delete
Move constructor.
ParameterDescriptorWithDefault(ParameterDescriptorWithDefault const &)=delete
Copy constructor.
dw::core::StringView const & getTypeName() const noexcept
Get the C++ type name of the parameter.
ParameterDescriptor & operator=(ParameterDescriptor const &) &=delete
Copy assignment operator.
virtual ~ParameterDescriptor()=default
Destructor.
ParameterDescriptor(dw::core::StringView const &name, dw::core::StringView const &typeName, const bool isIndex, size_t const arraySize) noexcept
Constructor.
ParameterDescriptor(ParameterDescriptor const &)=delete
Copy constructor.
dw::core::StringView const & getName() const noexcept
Get the parameter name, can be empty for unnamed parameters.
virtual void addDefault(nlohmann::ordered_json &object) const
Add the default value to the passed JSON object (only used by ParameterDescriptorWithDefault()).
ParameterDescriptor(ParameterDescriptor &&)=delete
Move constructor.
ParameterDescriptor & operator=(ParameterDescriptor &&) &=delete
Move assignment operator.
bool isIndex() const noexcept
Check if parameter is an index.
size_t getArraySize() const noexcept
Get the array size, 0 for non-array parameters.
static constexpr const size_t PARAMETER_DEFAULT_VALUE
static ParameterCollectionDescriptor createParameterCollectionDescriptor()
Create a parameter collection descriptor for a give node.
Definition: Buffer.hpp:40