Compute Graph Framework SDK Reference  5.14
PortDescriptor.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_PORTDESCRIPTOR_HPP_
32#define DW_FRAMEWORK_PORTDESCRIPTOR_HPP_
33
34#include <dwshared/dwfoundation/dw/core/container/StringView.hpp>
35#include <dwcgf/port/Port.hpp>
36#include <dwshared/dwfoundation/dw/core/language/cxx20.hpp>
37#include <dwshared/dwfoundation/dw/core/language/Tuple.hpp>
38
39#include <functional>
40#include <type_traits>
41#include <utility>
42
43namespace dw
44{
45namespace framework
46{
47
48// API needed to declare the ports of a node.
49
50template <typename... Args>
51// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
52constexpr auto describePortCollection(Args&&... args) -> dw::core::Tuple<Args...>
53{
54 return dw::core::make_tuple<Args...>(std::forward<Args>(args)...);
55}
56
57enum class PortBinding : uint8_t
58{
59 OPTIONAL = 0,
60 REQUIRED = 1
61};
62
63// coverity[autosar_cpp14_a14_1_1_violation]
64template <typename PortType, size_t ArraySize>
66{
67 // coverity[autosar_cpp14_a0_1_6_violation]
68 using Type = PortType;
69 dw::core::StringView typeName;
70 dw::core::StringView name;
71 static constexpr size_t arraySize{ArraySize};
73 dw::core::StringView comment;
74
75 constexpr PortDescriptorT(dw::core::StringView const&& typeName_, dw::core::StringView const&& name_, PortBinding binding_ = PortBinding::OPTIONAL, dw::core::StringView comment_ = ""_sv)
76 : typeName{std::move(typeName_)}
77 , name{std::move(name_)}
78 , binding{std::move(binding_)}
79 , comment{std::move(comment_)}
80 {
81 }
82};
83
84#define DW_PORT_TYPE_NAME_STRING_VIEW(TYPE_NAME_STR) TYPE_NAME_STR##_sv
85#define DW_DESCRIBE_PORT(TYPE_NAME, args...) dw::framework::describePort<TYPE_NAME>(DW_PORT_TYPE_NAME_STRING_VIEW(#TYPE_NAME), ##args)
86
87template <typename PortType>
88// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
89constexpr auto describePort(
90 dw::core::StringView typeName, dw::core::StringView name, PortBinding binding = PortBinding::OPTIONAL, dw::core::StringView comment = ""_sv) -> PortDescriptorT<PortType, 0>
91{
93 std::move(typeName),
94 std::move(name),
95 std::move(binding),
96 std::move(comment));
97}
98
99template <typename PortType>
100// Overloaded functions are provided for ease of use
101// coverity[autosar_cpp14_a2_10_5_violation]
102constexpr auto describePort(
103 dw::core::StringView typeName, dw::core::StringView name, dw::core::StringView comment)
104{
105 return describePort<PortType>(
106 std::move(typeName),
107 std::move(name),
108 std::move(PortBinding::OPTIONAL),
109 std::move(comment));
110}
111
112#define DW_DESCRIBE_PORT_ARRAY(TYPE_NAME, ARRAYSIZE, args...) dw::framework::describePortArray<TYPE_NAME, ARRAYSIZE>(DW_PORT_TYPE_NAME_STRING_VIEW(#TYPE_NAME), ##args)
113
114template <
115 typename PortType,
116 size_t ArraySize,
117 typename std::enable_if_t<ArraySize != 0, void>* = nullptr>
118// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
119constexpr auto describePortArray(
120 dw::core::StringView typeName, dw::core::StringView name, PortBinding binding = PortBinding::OPTIONAL, dw::core::StringView comment = ""_sv) -> PortDescriptorT<PortType, ArraySize>
121{
123 std::move(typeName),
124 std::move(name),
125 std::move(binding),
126 std::move(comment));
127}
128
129template <
130 typename PortType,
131 size_t ArraySize,
132 typename std::enable_if_t<ArraySize != 0, void>* = nullptr>
133// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
134constexpr auto describePortArray(
135 dw::core::StringView typeName, dw::core::StringView name, dw::core::StringView comment)
136{
137 return describePortArray<PortType, ArraySize>(
138 std::move(typeName),
139 std::move(name),
140 std::move(PortBinding::OPTIONAL),
141 std::move(comment));
142}
143
144// API to access declared ports of a node.
145
146template <typename Node>
147// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
148// coverity[autosar_cpp14_a7_1_5_violation] RFD Accepted: TID-1984
149constexpr auto describeInputPorts()
150{
152}
153
154template <typename Node>
155// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
156// coverity[autosar_cpp14_a7_1_5_violation] RFD Accepted: TID-1984
157constexpr auto describeOutputPorts()
158{
160}
161
162template <
163 typename Node,
164 PortDirection Direction,
165 typename std::enable_if_t<Direction == PortDirection::INPUT, void>* = nullptr>
166// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
167// coverity[autosar_cpp14_a7_1_5_violation] RFD Accepted: TID-1984
168constexpr auto describePorts()
169{
170 return describeInputPorts<Node>();
171}
172
173template <
174 typename Node,
175 PortDirection Direction,
176 typename std::enable_if_t<Direction == PortDirection::OUTPUT, void>* = nullptr>
177// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
178// coverity[autosar_cpp14_a7_1_5_violation] RFD Accepted: TID-1984
179constexpr auto describePorts()
180{
181 return describeOutputPorts<Node>();
182}
183
184// API to query information about declared ports of a node.
185
186// Number of input or output port descriptors
187template <typename Node, PortDirection Direction>
188// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
189constexpr std::size_t portDescriptorSize()
190{
191 return dw::core::tuple_size<decltype(describePorts<Node, Direction>())>::value;
192}
193
194// The flag if the port described by a specific descriptor is an array
195template <typename Node, PortDirection Direction, size_t DescriptorIndex>
196constexpr bool descriptorPortArray()
197{
198 constexpr size_t array_length{dw::core::tuple_element_t<
199 DescriptorIndex,
200 decltype(describePorts<Node, Direction>())>::arraySize};
201 return array_length > 0;
202}
203
204// The number of input or output ports described by a specific descriptor
205// 1 for non-array descriptors, ARRAY_SIZE for array descriptors
206template <typename Node, PortDirection Direction, size_t DescriptorIndex>
207// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
208constexpr size_t descriptorPortSize()
209{
210 constexpr size_t array_length{dw::core::tuple_element_t<
211 DescriptorIndex,
212 decltype(describePorts<Node, Direction>())>::arraySize};
213 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
214 if (array_length == 0U)
215 {
216 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
217 return 1U;
218 }
219 return array_length;
220}
221
222// The binding of input or output ports described by a specific descriptor
223template <typename Node, PortDirection Direction, size_t DescriptorIndex>
225{
226 constexpr PortBinding port_binding = dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()).binding;
227 return port_binding;
228}
229
230// The comment of input or output ports described by a specific descriptor
231template <typename Node, PortDirection Direction, size_t DescriptorIndex>
232constexpr dw::core::StringView descriptorPortComment()
233{
234 constexpr dw::core::StringView port_comment = dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()).comment;
235 return port_comment;
236}
237
238// Return type is the type of the descriptor, to be used with decltype()
239template <typename Node, PortDirection Direction, size_t DescriptorIndex>
240// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
241// coverity[autosar_cpp14_a7_1_5_violation]
242constexpr auto portDescriptorType()
243{
244 return typename dw::core::tuple_element_t<
245 DescriptorIndex,
246 decltype(describePorts<Node, Direction>())>::Type();
247}
248
249// Number of ports for a specific direction (sum across all descriptors)
250namespace detail
251{
252
253template <
254 typename Node, PortDirection Direction, size_t DescriptorIndex,
255 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
256// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
257constexpr std::size_t portSize_()
258{
259 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
260 return 0U;
261}
262
263template <
264 typename Node, PortDirection Direction, size_t DescriptorIndex,
265 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
266 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
267 constexpr std::size_t portSize_()
268{
269 return descriptorPortSize<Node, Direction, DescriptorIndex>() + portSize_<Node, Direction, DescriptorIndex + 1>();
270}
271
272} // namespace detail
273
274template <typename Node, PortDirection Direction>
275// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
276constexpr std::size_t portSize()
277{
278 return detail::portSize_<Node, Direction, 0>();
279}
280
281// Descriptor index from port index
282namespace detail
283{
284
285template <
286 typename Node, PortDirection Direction, size_t DescriptorIndex, size_t RemainingPortIndex,
287 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
288// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
289constexpr std::size_t descriptorIndex_()
290{
291 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
292 return 0U;
293}
294
295template <
296 typename Node, PortDirection Direction, size_t DescriptorIndex, size_t RemainingPortIndex,
297 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
298 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
299 constexpr std::size_t descriptorIndex_()
300{
301 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
302 if (RemainingPortIndex < descriptorPortSize<Node, Direction, DescriptorIndex>())
303 {
304 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
305 return 0U;
306 }
307 // coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
308 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
309 // coverity[autosar_cpp14_m0_1_4_violation] FP: nvbugs/2813925
310 constexpr size_t remainingPortIndex{RemainingPortIndex - descriptorPortSize<Node, Direction, DescriptorIndex>()};
311 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
312 return 1U + descriptorIndex_<Node, Direction, DescriptorIndex + 1, remainingPortIndex>();
313}
314
315} // namespace detail
316
317template <typename Node, PortDirection Direction, size_t PortIndex>
318// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
319constexpr size_t descriptorIndex()
320{
321 if (Direction == PortDirection::OUTPUT)
322 {
323 return detail::descriptorIndex_<Node, Direction, 0, PortIndex - portSize<Node, PortDirection::INPUT>()>();
324 }
325 return detail::descriptorIndex_<Node, Direction, 0, PortIndex>();
326}
327
328template <typename Node, PortDirection Direction, size_t PortIndex>
329// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
330constexpr dw::core::StringView portName()
331{
332 // coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
333 // coverity[autosar_cpp14_m0_1_4_violation] FP: nvbugs/2813925
334 constexpr size_t index{descriptorIndex<Node, Direction, PortIndex>()};
335 return dw::core::get<index>(describePorts<Node, Direction>()).name;
336}
337
338// Return type is the type of the port, to be used with decltype()
339template <typename Node, PortDirection Direction, size_t PortIndex>
340// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
341// coverity[autosar_cpp14_a7_1_5_violation]
342constexpr auto portType()
343{
344 // coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
345 // coverity[autosar_cpp14_m0_1_4_violation] FP: nvbugs/2813925
346 constexpr size_t index{descriptorIndex<Node, Direction, PortIndex>()};
347 return portDescriptorType<Node, Direction, index>();
348}
349
350// Check if port index is valid
351template <typename Node, PortDirection Direction>
352constexpr bool isValidPortIndex(std::size_t portID)
353{
354 // only temporarily for backward compatibility with enum value
355 // output port indices are offset by the number of input ports
356 if (Direction == PortDirection::OUTPUT)
357 {
358 return portID >= portSize<Node, PortDirection::INPUT>() && portID < portSize<Node, PortDirection::INPUT>() + portSize<Node, Direction>();
359 }
360 return portID < portSize<Node, Direction>();
361}
362
363// Array size for an array port name, 0 for non-array ports
364namespace detail
365{
366
367template <
368 typename Node, PortDirection Direction, size_t DescriptorIndex,
369 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
370constexpr std::size_t portArraySize_(StringView identifier)
371{
372 (void)identifier;
373 return 0;
374}
375
376template <
377 typename Node, PortDirection Direction, size_t DescriptorIndex,
378 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
379 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
380 constexpr std::size_t portArraySize_(StringView identifier)
381{
382 constexpr auto descriptorName = dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()).name;
383 if (descriptorName == identifier)
384 {
385 return descriptorPortSize<Node, Direction, DescriptorIndex>();
386 }
387 return portArraySize_<Node, Direction, DescriptorIndex + 1>(identifier);
388}
389
390} // namespace detail
391
392template <typename Node, PortDirection Direction>
393constexpr size_t portArraySize(StringView identifier)
394{
395 return detail::portArraySize_<Node, Direction, 0>(identifier);
396}
397
398// Get the port index for a give port name
399namespace detail
400{
401
402template <
403 typename Node, PortDirection Direction, size_t DescriptorIndex,
404 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
405// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
406constexpr std::size_t portIndex_(StringView identifier)
407{
408 static_cast<void>(identifier);
409 // since output port indices follow input port indices
410 // this must add the number of output port for invalid input port identifier
411 // to avoid that for an invalid input port identifier the index of the first output port is returned
412 if (Direction == PortDirection::INPUT)
413 {
414 return dw::framework::portSize<Node, PortDirection::OUTPUT>();
415 }
416 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
417 return 0U;
418}
419
420template <
421 typename Node, PortDirection Direction, size_t DescriptorIndex,
422 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
423 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
424 constexpr std::size_t portIndex_(StringView identifier)
425{
426 constexpr StringView descriptorName{dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()).name};
427 if (descriptorName == identifier)
428 {
429 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
430 return 0U;
431 }
432 return descriptorPortSize<Node, Direction, DescriptorIndex>() + portIndex_<Node, Direction, DescriptorIndex + 1>(identifier);
433}
434
435} // namespace detail
436
437template <typename Node, PortDirection Direction>
438// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
439constexpr size_t portIndex(StringView identifier)
440{
441 // only temporarily for backward compatibility with enum value
442 // output port indices are offset by the number of input ports
443 if (Direction == PortDirection::OUTPUT)
444 {
445 return portSize<Node, PortDirection::INPUT>() + detail::portIndex_<Node, Direction, 0>(identifier);
446 }
447 return detail::portIndex_<Node, Direction, 0>(identifier);
448}
449
450// Check if given string is a valid port name
451template <typename Node, PortDirection Direction>
452constexpr bool isValidPortIdentifier(StringView identifier)
453{
454 constexpr size_t index = portIndex<Node, Direction>(identifier);
455 return isValidPortIndex<Node, Direction>(index);
456}
457
458// Get the port index for a give port name
459namespace detail
460{
461
462template <
463 typename Node, PortDirection Direction, size_t DescriptorIndex,
464 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
465constexpr std::size_t portDescriptorIndex_(StringView identifier)
466{
467 (void)identifier;
468 return 0;
469}
470
471template <
472 typename Node, PortDirection Direction, size_t DescriptorIndex,
473 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
474 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
475 constexpr std::size_t portDescriptorIndex_(StringView identifier)
476{
477 constexpr auto descriptorName = dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()).name;
478 if (descriptorName == identifier)
479 {
480 return 0;
481 }
482 return 1 + portDescriptorIndex_<Node, Direction, DescriptorIndex + 1>(identifier);
483}
484
485} // namespace detail
486
487template <typename Node, PortDirection Direction>
488constexpr size_t portDescriptorIndex(StringView identifier)
489{
490 return detail::portDescriptorIndex_<Node, Direction, 0>(identifier);
491}
492
493} // namespace framework
494} // namespace dw
495
496#endif // DW_FRAMEWORK_PORTDESCRIPTOR_HPP_
constexpr auto describePort(dw::core::StringView typeName, dw::core::StringView name, PortBinding binding=PortBinding::OPTIONAL, dw::core::StringView comment=""_sv) -> PortDescriptorT< PortType, 0 >
constexpr auto describePorts()
constexpr auto describePortArray(dw::core::StringView typeName, dw::core::StringView name, PortBinding binding=PortBinding::OPTIONAL, dw::core::StringView comment=""_sv) -> PortDescriptorT< PortType, ArraySize >
constexpr bool descriptorPortArray()
constexpr size_t portIndex(StringView identifier)
constexpr size_t descriptorPortSize()
constexpr size_t portDescriptorIndex(StringView identifier)
constexpr auto portDescriptorType()
constexpr size_t portArraySize(StringView identifier)
constexpr std::size_t portDescriptorSize()
constexpr auto describeOutputPorts()
constexpr auto describePortCollection(Args &&... args) -> dw::core::Tuple< Args... >
constexpr bool isValidPortIndex(std::size_t portID)
constexpr std::size_t portSize()
constexpr PortBinding descriptorPortBinding()
constexpr dw::core::StringView portName()
constexpr auto describeInputPorts()
constexpr bool isValidPortIdentifier(StringView identifier)
constexpr dw::core::StringView descriptorPortComment()
constexpr auto portType()
constexpr size_t descriptorIndex()
Definition: Buffer.hpp:40
static constexpr size_t arraySize
dw::core::StringView typeName
dw::core::StringView comment
constexpr PortDescriptorT(dw::core::StringView const &&typeName_, dw::core::StringView const &&name_, PortBinding binding_=PortBinding::OPTIONAL, dw::core::StringView comment_=""_sv)