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