Compute Graph Framework SDK Reference  5.8
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-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_PARAMETERCOLLECTIONDESCRIPTOR_HPP_
32#define DW_FRAMEWORK_PARAMETERCOLLECTIONDESCRIPTOR_HPP_
33
35
36#include <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 noexcept;
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>
97auto getDefaultValueForJson(DefaultType defaultValue)
98{
99 return nlohmann::json(defaultValue);
100}
101
103template <
104 typename DefaultType,
105 std::enable_if_t<
106 std::is_enum<DefaultType>::value>* = nullptr>
107// TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
108// coverity[autosar_cpp14_a2_10_5_violation]
109auto 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// TODO(dwplc): FP -- The specific specialization of this templated function is selected by enable_if
122// coverity[autosar_cpp14_a2_10_5_violation]
123auto getDefaultValueForJson(const std::array<DefaultType, N>& defaultValues)
124{
125 std::array<std::string, N> nameStrings;
126 for (size_t i = 0; i < N; ++i)
127 {
128 StringView name = mapEnumValueToName<DefaultType>(defaultValues[i]);
129 std::string nameStr{name.data(), name.size()};
130 nameStrings[i] = 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 noexcept override
169 {
170 object["default"] = detail::getDefaultValueForJson(m_defaultValue);
171 }
172
173private:
175 DefaultType m_defaultValue;
176};
177
180{
181public:
183 explicit ParameterCollectionDescriptor(size_t const capacity);
184
186 size_t getSize() const;
187
189 const std::shared_ptr<const ParameterDescriptor>& getDescriptor(size_t const index) const;
190
196 size_t getIndex(dw::core::StringView const& identifier) const;
197
203 const std::shared_ptr<const ParameterDescriptor>& getDescriptor(dw::core::StringView const& identifier) const;
204
206 bool isValid(size_t const index) const;
207
209 bool isValid(dw::core::StringView const& identifier) const noexcept;
210
216 void addDescriptor(const std::shared_ptr<const ParameterDescriptor>& descriptor);
217
219 template <
220 typename NodeT, size_t ConstructorArgumentIndex,
221 typename std::enable_if_t<ConstructorArgumentIndex == std::tuple_size<decltype(describeParameters<NodeT>())>::value, void>* = nullptr>
223 {
224 }
225
227 template <
228 typename NodeT, size_t ConstructorArgumentIndex,
229 typename std::enable_if_t<ConstructorArgumentIndex<std::tuple_size<decltype(describeParameters<NodeT>())>::value, void>* = nullptr> void addDescriptors()
230 {
231 auto t = std::get<ConstructorArgumentIndex>(describeParameters<NodeT>());
232 const auto& t2 = std::get<PARAMETER_CONSTRUCTOR_ARGUMENT_DESCRIPTOR>(t);
233 // all parameters of this constructor argument
234 addDescriptors<0>(t2);
235 // next constructor argument
236 addDescriptors<NodeT, ConstructorArgumentIndex + 1>();
237 }
238
239private:
241 template <
242 size_t ParameterIndex, typename ParamsT,
243 typename std::enable_if_t<ParameterIndex == dw::core::tuple_size<ParamsT>::value, void>* = nullptr>
244 void addDescriptors(const ParamsT& params)
245 {
246 static_cast<void>(params);
247 }
248
250 template <
251 size_t ParameterIndex, typename ParamsT,
252 typename std::enable_if_t<ParameterIndex<dw::core::tuple_size<ParamsT>::value, void>* = nullptr> void addDescriptors(const ParamsT& params)
253 {
254 const auto& p = dw::core::get<ParameterIndex>(params);
255 addDescriptor(p);
256 addDescriptors<ParameterIndex + 1>(params);
257 }
258
260 template <
261 typename ParamT,
262 typename std::enable_if_t<std::tuple_size<ParamT>::value <= PARAMETER_DEFAULT_VALUE, void>* = nullptr>
263 void addDescriptor(const ParamT& param)
264 {
265 const auto d = std::make_shared<const ParameterDescriptor>(
266 std::get<PARAMETER_NAME>(param),
267 std::get<PARAMETER_TYPE_NAME>(param),
268 std::get<PARAMETER_IS_INDEX>(param),
269 std::get<PARAMETER_ARRAY_SIZE>(param));
270 addDescriptor(d);
271 }
272
274 template <
275 typename ParamT,
276 typename std::enable_if_t<PARAMETER_DEFAULT_VALUE<std::tuple_size<ParamT>::value, void>* = nullptr> void addDescriptor(const ParamT& param)
277 {
278 using DefaultT = typename std::tuple_element_t<PARAMETER_DEFAULT_VALUE, ParamT>;
279 const auto d = std::make_shared<const ParameterDescriptorWithDefault<DefaultT>>(
280 std::get<PARAMETER_NAME>(param),
281 std::get<PARAMETER_TYPE_NAME>(param),
282 std::get<PARAMETER_IS_INDEX>(param),
283 std::get<PARAMETER_ARRAY_SIZE>(param),
284 std::get<PARAMETER_DEFAULT_VALUE>(param));
285 addDescriptor(d);
286 }
287
289 dw::core::VectorFixed<std::shared_ptr<const ParameterDescriptor>> m_descriptors;
290};
291
293template <typename NodeT>
295{
296 ParameterCollectionDescriptor d(parameterSize<NodeT>());
297 d.addDescriptors<NodeT, 0>();
298 return d;
299}
300
301} // namespace framework
302} // namespace dw
303
304#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.
void addDefault(nlohmann::ordered_json &object) const noexcept override
Add the default value to the passed JSON object.
~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.
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 noexcept
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 size_t PARAMETER_DEFAULT_VALUE
static ParameterCollectionDescriptor createParameterCollectionDescriptor()
Create a parameter collection descriptor for a give node.
Definition: Exception.hpp:47