Compute Graph Framework SDK Reference
5.4.5418 Release
For Test and Development only

ParameterCollectionDescriptor.hpp
Go to the documentation of this file.
1 //
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 
42 namespace dw
43 {
44 namespace framework
45 {
46 
49 {
50 public:
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 
78 private:
80  dw::core::StringView m_name;
82  dw::core::StringView m_typeName;
84  bool m_isIndex;
86  size_t m_arraySize;
87 };
88 
89 namespace detail
90 {
91 
93 template <
94  typename DefaultType,
95  std::enable_if_t<
96  !std::is_enum<DefaultType>::value>* = nullptr>
97 auto getDefaultValueForJson(DefaultType defaultValue)
98 {
99  return nlohmann::json(defaultValue);
100 }
101 
103 template <
104  typename DefaultType,
105  std::enable_if_t<
106  std::is_enum<DefaultType>::value>* = nullptr>
107 auto getDefaultValueForJson(DefaultType defaultValue)
108 {
109  StringView name = mapEnumValueToName<DefaultType>(defaultValue);
110  std::string nameStr{name.data(), name.size()};
111  return nlohmann::json(nameStr);
112 }
113 
115 template <
116  typename DefaultType, size_t N,
117  std::enable_if_t<
118  std::is_enum<DefaultType>::value>* = nullptr>
119 auto getDefaultValueForJson(const std::array<DefaultType, N>& defaultValues)
120 {
121  std::array<std::string, N> nameStrings;
122  for (size_t i = 0; i < N; ++i)
123  {
124  StringView name = mapEnumValueToName<DefaultType>(defaultValues[i]);
125  std::string nameStr{name.data(), name.size()};
126  nameStrings[i] = nameStr;
127  }
128  return nlohmann::json(nameStrings);
129 }
130 
131 } // namespace detail
132 
134 template <typename DefaultType>
136 {
137 public:
140  dw::core::StringView name,
141  dw::core::StringView typeName,
142  bool isIndex,
143  size_t arraySize,
144  DefaultType defaultValue)
145  : ParameterDescriptor(name, typeName, isIndex, arraySize)
146  , m_defaultValue(defaultValue)
147  {
148  }
150  ~ParameterDescriptorWithDefault() override = default;
160  void addDefault(nlohmann::ordered_json& object) const noexcept override
161  {
162  object["default"] = detail::getDefaultValueForJson(m_defaultValue);
163  }
164 
165 private:
167  DefaultType m_defaultValue;
168 };
169 
172 {
173 public:
175  explicit ParameterCollectionDescriptor(size_t const capacity);
176 
178  size_t getSize() const;
179 
181  const std::shared_ptr<const ParameterDescriptor>& getDescriptor(size_t const index) const;
182 
188  size_t getIndex(dw::core::StringView const& identifier) const;
189 
195  const std::shared_ptr<const ParameterDescriptor>& getDescriptor(dw::core::StringView const& identifier) const;
196 
198  bool isValid(size_t const index) const;
199 
201  bool isValid(dw::core::StringView const& identifier) const noexcept;
202 
208  void addDescriptor(const std::shared_ptr<const ParameterDescriptor>& descriptor);
209 
211  template <
212  typename NodeT, size_t ConstructorArgumentIndex,
213  typename std::enable_if_t<ConstructorArgumentIndex == std::tuple_size<decltype(describeParameters<NodeT>())>::value, void>* = nullptr>
215  {
216  }
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> void addDescriptors()
222  {
223  auto t = std::get<ConstructorArgumentIndex>(describeParameters<NodeT>());
224  const auto& t2 = std::get<PARAMETER_CONSTRUCTOR_ARGUMENT_DESCRIPTOR>(t);
225  // all parameters of this constructor argument
226  addDescriptors<0>(t2);
227  // next constructor argument
228  addDescriptors<NodeT, ConstructorArgumentIndex + 1>();
229  }
230 
231 private:
233  template <
234  size_t ParameterIndex, typename ParamsT,
235  typename std::enable_if_t<ParameterIndex == dw::core::tuple_size<ParamsT>::value, void>* = nullptr>
236  void addDescriptors(const ParamsT& params)
237  {
238  static_cast<void>(params);
239  }
240 
242  template <
243  size_t ParameterIndex, typename ParamsT,
244  typename std::enable_if_t<ParameterIndex<dw::core::tuple_size<ParamsT>::value, void>* = nullptr> void addDescriptors(const ParamsT& params)
245  {
246  const auto& p = dw::core::get<ParameterIndex>(params);
247  addDescriptor(p);
248  addDescriptors<ParameterIndex + 1>(params);
249  }
250 
252  template <
253  typename ParamT,
254  typename std::enable_if_t<std::tuple_size<ParamT>::value <= PARAMETER_DEFAULT_VALUE, void>* = nullptr>
255  void addDescriptor(const ParamT& param)
256  {
257  const auto d = std::make_shared<const ParameterDescriptor>(
258  std::get<PARAMETER_NAME>(param),
259  std::get<PARAMETER_TYPE_NAME>(param),
260  std::get<PARAMETER_IS_INDEX>(param),
261  std::get<PARAMETER_ARRAY_SIZE>(param));
262  addDescriptor(d);
263  }
264 
266  template <
267  typename ParamT,
268  typename std::enable_if_t<PARAMETER_DEFAULT_VALUE<std::tuple_size<ParamT>::value, void>* = nullptr> void addDescriptor(const ParamT& param)
269  {
270  using DefaultT = typename std::tuple_element_t<PARAMETER_DEFAULT_VALUE, ParamT>;
271  const auto d = std::make_shared<const ParameterDescriptorWithDefault<DefaultT>>(
272  std::get<PARAMETER_NAME>(param),
273  std::get<PARAMETER_TYPE_NAME>(param),
274  std::get<PARAMETER_IS_INDEX>(param),
275  std::get<PARAMETER_ARRAY_SIZE>(param),
276  std::get<PARAMETER_DEFAULT_VALUE>(param));
277  addDescriptor(d);
278  }
279 
281  dw::core::VectorFixed<std::shared_ptr<const ParameterDescriptor>> m_descriptors;
282 };
283 
285 template <typename NodeT>
287 {
288  ParameterCollectionDescriptor d(parameterSize<NodeT>());
289  d.addDescriptors<NodeT, 0>();
290  return d;
291 }
292 
293 } // namespace framework
294 } // namespace dw
295 
296 #endif // DW_FRAMEWORK_PARAMETERCOLLECTIONDESCRIPTOR_HPP_
virtual void addDefault(nlohmann::ordered_json &object) const noexcept
Add the default value to the passed JSON object (only used by ParameterDescriptorWithDefault()).
ParameterDescriptor(dw::core::StringView const &name, dw::core::StringView const &typeName, const bool isIndex, size_t const arraySize) noexcept
Constructor.
ParameterDescriptor & operator=(ParameterDescriptor const &)=delete
Copy assignment operator.
ParameterDescriptorWithDefault(dw::core::StringView name, dw::core::StringView typeName, bool isIndex, size_t arraySize, DefaultType defaultValue)
Constructor.
size_t getArraySize() const noexcept
Get the array size, 0 for non-array parameters.
void addDescriptors()
Terminate recursion to add parameter descriptors for each node constructor argument to the collection...
void addDefault(nlohmann::ordered_json &object) const noexcept override
Add the default value to the passed JSON object.
static ParameterCollectionDescriptor createParameterCollectionDescriptor()
Create a parameter collection descriptor for a give node.
dw::core::StringView const & getName() const noexcept
Get the parameter name, can be empty for unnamed parameters.
bool isIndex() const noexcept
Check if parameter is an index.
Definition: Exception.hpp:46
virtual ~ParameterDescriptor()=default
Destructor.
The description of a parameter with a default value.
dw::core::StringView const & getTypeName() const noexcept
Get the C++ type name of the parameter.
static constexpr size_t PARAMETER_DEFAULT_VALUE