Compute Graph Framework SDK Reference  5.22
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-2024 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#include <dwshared/dwfoundation/dw/core/safety/Safety.hpp>
39
40#include <array>
41#include <functional>
42#include <type_traits>
43#include <utility>
44
45namespace dw
46{
47namespace framework
48{
49
50// API needed to declare the ports of a node.
51
52template <typename... Args>
53// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
54constexpr auto describePortCollection(Args&&... args) -> dw::core::Tuple<Args...>
55{
56 return dw::core::make_tuple<Args...>(std::forward<Args>(args)...);
57}
58
59enum class PortBinding : uint8_t
60{
61 OPTIONAL = 0,
62 REQUIRED = 1
63};
64
65template <typename PortType, size_t ArraySize, size_t NameSize>
67{
68 static_assert(std::is_constructible<PortType>::value, "PortType must be constructible");
69
70 // coverity[autosar_cpp14_a0_1_6_violation]
71 using Type = PortType;
72 dw::core::StringView typeName;
73 dw::core::StringView name;
74 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
75 static constexpr size_t arraySize{ArraySize};
76 // coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
77 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
78 // coverity[autosar_cpp14_m0_1_4_violation] FP: nvbugs/2813925
79 static constexpr size_t nameSize{NameSize};
81 dw::core::StringView comment;
82
83 constexpr PortDescriptorT(dw::core::StringView&& typeName_, dw::core::StringView&& name_, PortBinding binding_ = PortBinding::OPTIONAL, dw::core::StringView comment_ = ""_sv)
84 : typeName{std::move(typeName_)}
85 , name{std::move(name_)}
86 , binding{std::move(binding_)}
87 , comment{std::move(comment_)}
88 {
89 }
90};
91
92#define DW_PORT_TYPE_NAME_STRING_VIEW_IMPL(TYPE_NAME_STR) TYPE_NAME_STR##_sv
93#define DW_PORT_TYPE_NAME_STRING_VIEW(TYPE_NAME) DW_PORT_TYPE_NAME_STRING_VIEW_IMPL(#TYPE_NAME)
94#define DW_DESCRIBE_PORT(TYPE_NAME, NAME, args...) dw::framework::describePort<TYPE_NAME, NAME.size()>(DW_PORT_TYPE_NAME_STRING_VIEW(TYPE_NAME), NAME, ##args)
95
96template <typename PortType, size_t NameSize>
97// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
98constexpr auto describePort(
99 dw::core::StringView typeName, dw::core::StringView name, PortBinding binding = PortBinding::OPTIONAL, dw::core::StringView comment = ""_sv) -> PortDescriptorT<PortType, 0, NameSize>
100{
102 std::move(typeName),
103 std::move(name),
104 std::move(binding),
105 std::move(comment));
106}
107
108#define DW_DESCRIBE_PORT_ARRAY(TYPE_NAME, ARRAYSIZE, NAME, args...) dw::framework::describePortArray<TYPE_NAME, ARRAYSIZE, NAME.size()>(DW_PORT_TYPE_NAME_STRING_VIEW(TYPE_NAME), NAME, ##args)
109template <
110 typename PortType,
111 size_t ArraySize,
112 size_t NameSize,
113 typename std::enable_if_t<ArraySize != 0, void>* = nullptr>
114// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
115constexpr auto describePortArray(
116 dw::core::StringView typeName, dw::core::StringView name, PortBinding binding = PortBinding::OPTIONAL, dw::core::StringView comment = ""_sv) -> PortDescriptorT<PortType, ArraySize, NameSize>
117{
119 std::move(typeName),
120 std::move(name),
121 std::move(binding),
122 std::move(comment));
123}
124
125template <
126 typename PortType,
127 size_t ArraySize,
128 size_t NameSize,
129 typename std::enable_if_t<ArraySize != 0, void>* = nullptr>
130// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
131constexpr auto describePortArray(
132 dw::core::StringView typeName, dw::core::StringView name, dw::core::StringView comment)
133{
134 return describePortArray<PortType, ArraySize, NameSize>(
135 std::move(typeName),
136 std::move(name),
137 std::move(PortBinding::OPTIONAL),
138 std::move(comment));
139}
140
141// API to access declared ports of a node.
142
143// LCOV_EXCL_START no coverage data for compile time evaluated function
144template <typename Node>
145// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
146// coverity[autosar_cpp14_a7_1_5_violation] RFD Accepted: TID-2201
148{
149 return Node::describeInputPorts();
150}
151// LCOV_EXCL_STOP
152
153template <typename Node>
154// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
155// coverity[autosar_cpp14_a7_1_5_violation] RFD Accepted: TID-2201
157{
158 return Node::describeOutputPorts();
159}
160
161// LCOV_EXCL_START no coverage data for compile time evaluated function
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-2201
168constexpr auto describePorts()
169{
170 return describeNodeInputPorts<Node>();
171}
172// LCOV_EXCL_STOP
173
174template <
175 typename Node,
176 PortDirection Direction,
177 typename std::enable_if_t<Direction == PortDirection::OUTPUT, void>* = nullptr>
178// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
179// coverity[autosar_cpp14_a7_1_5_violation] RFD Accepted: TID-2201
180constexpr auto describePorts()
181{
182 return describeNodeOutputPorts<Node>();
183}
184
185// API to query information about declared ports of a node.
186
187// Number of input or output port descriptors
188template <typename Node, PortDirection Direction>
189// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
190constexpr std::size_t portDescriptorSize()
191{
192 return dw::core::tuple_size<decltype(describePorts<Node, Direction>())>::value;
193}
194
195// The flag if the port described by a specific descriptor is an array
196template <typename Node, PortDirection Direction, size_t DescriptorIndex>
197// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
198constexpr bool descriptorPortArray()
199{
200 constexpr size_t array_length{dw::core::tuple_element_t<
201 DescriptorIndex,
202 decltype(describePorts<Node, Direction>())>::arraySize};
203 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
204 return array_length > 0U;
205}
206
207// The number of input or output ports described by a specific descriptor
208// 1 for non-array descriptors, ARRAY_SIZE for array descriptors
209template <typename Node, PortDirection Direction, size_t DescriptorIndex>
210// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
211constexpr size_t descriptorPortSize()
212{
213 constexpr size_t array_length{dw::core::tuple_element_t<
214 DescriptorIndex,
215 decltype(describePorts<Node, Direction>())>::arraySize};
216 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
217 if (0U == array_length)
218 {
219 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
220 return 1U;
221 }
222 return array_length;
223}
224
225// The binding of input or output ports described by a specific descriptor
226template <typename Node, PortDirection Direction, size_t DescriptorIndex>
228{
229 constexpr PortBinding port_binding = dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()).binding;
230 return port_binding;
231}
232
233// The comment of input or output ports described by a specific descriptor
234template <typename Node, PortDirection Direction, size_t DescriptorIndex>
235constexpr dw::core::StringView descriptorPortComment()
236{
237 constexpr dw::core::StringView port_comment = dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()).comment;
238 return port_comment;
239}
240
241// Return type is the type of the descriptor, to be used with decltype()
242template <typename Node, PortDirection Direction, size_t DescriptorIndex>
243// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
244// coverity[autosar_cpp14_a7_1_5_violation] RFD Accepted: TID-2201
245constexpr auto portDescriptorType()
246{
247 return typename dw::core::tuple_element_t<
248 DescriptorIndex,
249 decltype(describePorts<Node, Direction>())>::Type();
250}
251
252// Number of ports for a specific direction (sum across all descriptors)
253namespace detail
254{
255
256template <
257 typename Node, PortDirection Direction, size_t DescriptorIndex,
258 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
259// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
260constexpr std::size_t portSize_()
261{
262 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
263 return 0U;
264}
265
266template <
267 typename Node, PortDirection Direction, size_t DescriptorIndex,
268 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
269 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
270 constexpr std::size_t portSize_()
271{
272 return descriptorPortSize<Node, Direction, DescriptorIndex>() + portSize_<Node, Direction, DescriptorIndex + 1>();
273}
274
275} // namespace detail
276
277template <typename Node, PortDirection Direction>
278// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
279constexpr std::size_t portSize()
280{
281 return detail::portSize_<Node, Direction, 0>();
282}
283
284// Descriptor index from port index
285namespace detail
286{
287
288template <
289 typename Node, PortDirection Direction, size_t DescriptorIndex, size_t RemainingPortIndex,
290 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
291// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
292constexpr std::size_t descriptorIndex_()
293{
294 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
295 return 0U;
296}
297
298template <
299 typename Node, PortDirection Direction, size_t DescriptorIndex, size_t RemainingPortIndex,
300 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
301 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
302 constexpr std::size_t descriptorIndex_()
303{
304 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
305 if (RemainingPortIndex < descriptorPortSize<Node, Direction, DescriptorIndex>())
306 {
307 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
308 return 0U;
309 }
310 // coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
311 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
312 // coverity[autosar_cpp14_m0_1_4_violation] FP: nvbugs/2813925
313 constexpr size_t remainingPortIndex{RemainingPortIndex - descriptorPortSize<Node, Direction, DescriptorIndex>()};
314 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
315 return 1U + descriptorIndex_<Node, Direction, DescriptorIndex + 1, remainingPortIndex>();
316}
317
318} // namespace detail
319
320template <typename Node, PortDirection Direction, size_t PortIndex>
321// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
322constexpr size_t descriptorIndex()
323{
324 if (PortDirection::OUTPUT == Direction)
325 {
326 return detail::descriptorIndex_<Node, Direction, 0, PortIndex - portSize<Node, PortDirection::INPUT>()>();
327 }
328 return detail::descriptorIndex_<Node, Direction, 0, PortIndex>();
329}
330
331template <typename Node, PortDirection Direction, size_t PortIndex>
332// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
333constexpr dw::core::StringView portName()
334{
335 // coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
336 // coverity[autosar_cpp14_m0_1_4_violation] FP: nvbugs/2813925
337 constexpr size_t index{descriptorIndex<Node, Direction, PortIndex>()};
338 return dw::core::get<index>(describePorts<Node, Direction>()).name;
339}
340
341namespace detail
342{
343
344// coverity[autosar_cpp14_m3_4_1_violation] RFD Pending: TID-2586
345constexpr const size_t DECIMAL_BASE{10U};
346
347constexpr size_t numberOfDigits(size_t number)
348{
349 static_assert(std::numeric_limits<size_t>::digits10 <= std::numeric_limits<size_t>::max(), "size_t number of digits exceeds size_t (not possible)");
350 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
351 if (0U == number)
352 {
353 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
354 return 1U;
355 }
356 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
357 size_t count{0U};
358 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
359 while (number > 0U)
360 {
361 number = number / DECIMAL_BASE;
362 // without this check coverity would flag violation of cert_int30_c
363 if (std::numeric_limits<size_t>::max() == count)
364 {
365 throw std::logic_error("size_t number of digits exceeds size_t (not possible)");
366 }
367 ++count;
368 }
369 return count;
370}
371
372constexpr size_t getArrayNameSize(size_t portNameSize, size_t arrayIndex)
373{
374 // port name size + '[' + number of digits of the array index + ']'
375 constexpr size_t MAX_SIZE_WITHOUT_BRACKETS{std::numeric_limits<size_t>::max() - 1U - 1U};
376 if (portNameSize >= MAX_SIZE_WITHOUT_BRACKETS)
377 {
378 throw std::runtime_error("Array name too long");
379 }
380 if (MAX_SIZE_WITHOUT_BRACKETS - portNameSize < numberOfDigits(arrayIndex))
381 {
382 throw std::runtime_error("Array name + digits for array index too long");
383 }
384 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
385 return portNameSize + 1U + numberOfDigits(arrayIndex) + 1U;
386}
387
388template <size_t NameSize, size_t ArraySize>
389// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
390constexpr size_t getMaximumArrayNameSize()
391{
392 static_assert(NameSize > 0U, "Name size must not be zero");
393 static_assert(ArraySize > 0U, "Array size must not be zero");
394 // number of digits for the largest array index
395 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
396 return getArrayNameSize(NameSize, ArraySize - 1U);
397}
398
399template <size_t NameSize, size_t ArraySize>
400class PortNamesGenerator
401{
402public:
403 static_assert(NameSize > 0U, "Name size must not be zero");
404 static_assert(ArraySize > 0U, "Array size must not be zero");
405 // same size for all names even though smaller indices might be shorter
406 // + 1 for a null character for each name to be defensive in case the string view is used incorrectly
407 static_assert(std::numeric_limits<size_t>::max() / ArraySize > getMaximumArrayNameSize<NameSize, ArraySize>() + 1U, "The storage size exceeds size_t");
408 // coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
409 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
410 // coverity[autosar_cpp14_m0_1_4_violation] FP: nvbugs/2813925
411 static constexpr size_t StorageSize{ArraySize * (getMaximumArrayNameSize<NameSize, ArraySize>() + 1U)};
412 constexpr PortNamesGenerator(dw::core::StringView baseName)
413 : m_data()
414 {
415 if (baseName.size() > NameSize)
416 {
417 // LCOV_EXCL_START the calling code uses the size of the StringView as the template parameter
418 throw ExceptionWithStatus(DW_INTERNAL_ERROR, "The passed string size ", baseName.size(), " exceeds the template parameter NameSize ", NameSize);
419 // LCOV_EXCL_STOP
420 }
421 size_t i{0U};
422 for (size_t arrayIndex{0U}; ArraySize != arrayIndex; ++arrayIndex)
423 {
424 // copy base port name
425 for (size_t j{0U}; j < baseName.size(); ++j)
426 {
427 m_data.at(i) = baseName[j];
428 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
429 dw::core::safeIncrement(i, 1U);
430 }
431
432 // append the array index wrapped in brackets
433 const char8_t OPENING_BRACKET{'['};
434 m_data.at(i) = OPENING_BRACKET;
435 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
436 dw::core::safeIncrement(i, 1U);
437
438 size_t remainingValue{arrayIndex};
439 // the length of the port name isn't close to the maximum value of size_t, hence no risk of overflow
440 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
441 const size_t INDEX_LAST_DIGIT{dw::core::safeAdd(i, numberOfDigits(arrayIndex) - 1U).value()};
442 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
443 for (size_t j{0U}; j < numberOfDigits(arrayIndex); ++j)
444 {
445 // fill the array index digits in reverse order
446 constexpr char8_t digits[10]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
447 // without this check coverity would flag violation of cert_int30_c
448 if (INDEX_LAST_DIGIT < j)
449 {
450 throw std::logic_error("index j must never be greater than the index of the last digit");
451 }
452 m_data.at(INDEX_LAST_DIGIT - j) = digits[remainingValue % DECIMAL_BASE];
453 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
454 dw::core::safeIncrement(i, 1U);
455 remainingValue = remainingValue / DECIMAL_BASE;
456 }
457
458 const char8_t CLOSING_BRACKET{']'};
459 m_data.at(i) = CLOSING_BRACKET;
460 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
461 dw::core::safeIncrement(i, 1U);
462 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
463 m_data.at(i) = static_cast<char8_t>(0);
464 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
465 dw::core::safeIncrement(i, 1U);
466 // without this check coverity would flag violation of cert_int30_c
467 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
468 if (i > std::numeric_limits<size_t>::max() - numberOfDigits(ArraySize - 1U))
469 {
470 throw std::logic_error("index j must never be greater than the index of the last digit");
471 }
472 // skip delta which this name is shorter compared to the maximum length
473 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
474 i += numberOfDigits(ArraySize - 1U) - numberOfDigits(arrayIndex);
475 }
476 }
477
478 dw::core::StringView getName(size_t arrayIndex) const
479 {
480 if (arrayIndex >= ArraySize)
481 {
482 throw ExceptionWithStatus(DW_OUT_OF_BOUNDS, "Array index ", arrayIndex, " out of bound for array size ", ArraySize);
483 }
484 // coverity[autosar_cpp14_a5_1_1_violation] FP: nvbugs/3364868
485 return dw::core::StringView(&m_data.at(arrayIndex * (getMaximumArrayNameSize<NameSize, ArraySize>() + 1U)), getArrayNameSize(NameSize, arrayIndex));
486 }
487
488private:
489 std::array<char8_t, StorageSize> m_data;
490};
491
492} // namespace detail
493
494template <typename Node, PortDirection Direction, size_t PortIndex>
495// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
496dw::core::StringView portName(size_t arrayIndex)
497{
498 // coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
499 // coverity[autosar_cpp14_m0_1_4_violation] FP: nvbugs/2813925
500 constexpr size_t index{descriptorIndex<Node, Direction, PortIndex>()};
501 // coverity[autosar_cpp14_a8_5_2_violation] FP: nvbugs/3904083
502 constexpr auto desc = dw::core::get<index>(describePorts<Node, Direction>());
503 static_assert(desc.arraySize > 0U, "A port name with an array index argument is only applicable to array ports");
504 // coverity[autosar_cpp14_a3_3_2_violation] RFD Pending: TID-2534
505 static const detail::PortNamesGenerator<desc.nameSize, desc.arraySize> generatedNames{desc.name};
506 return generatedNames.getName(arrayIndex);
507}
508
509// Return type is the type of the port, to be used with decltype()
510template <typename Node, PortDirection Direction, size_t PortIndex>
511// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
512// coverity[autosar_cpp14_a7_1_5_violation] RFD Accepted: TID-2201
513constexpr auto portType()
514{
515 // coverity[autosar_cpp14_a0_1_1_violation] FP: nvbugs/2813925
516 // coverity[autosar_cpp14_m0_1_4_violation] FP: nvbugs/2813925
517 constexpr size_t index{descriptorIndex<Node, Direction, PortIndex>()};
518 return portDescriptorType<Node, Direction, index>();
519}
520
521// Check if port index is valid
522template <typename Node, PortDirection Direction>
523constexpr bool isValidPortIndex(std::size_t portID)
524{
525 // only temporarily for backward compatibility with enum value
526 // output port indices are offset by the number of input ports
527 if (PortDirection::OUTPUT == Direction)
528 {
529 return portID >= portSize<Node, PortDirection::INPUT>() && portID < portSize<Node, PortDirection::INPUT>() + portSize<Node, Direction>();
530 }
531 return portID < portSize<Node, Direction>();
532}
533
534// Array size for an array port name, 0 for non-array ports
535namespace detail
536{
537
538template <
539 typename Node, PortDirection Direction, size_t DescriptorIndex,
540 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
541constexpr std::size_t portArraySize_(StringView identifier)
542{
543 (void)identifier;
544 return 0;
545}
546
547template <
548 typename Node, PortDirection Direction, size_t DescriptorIndex,
549 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
550 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
551 constexpr std::size_t portArraySize_(StringView identifier)
552{
553 constexpr auto descriptorName = dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()).name;
554 if (descriptorName == identifier)
555 {
556 return descriptorPortSize<Node, Direction, DescriptorIndex>();
557 }
558 return portArraySize_<Node, Direction, DescriptorIndex + 1>(identifier);
559}
560
561} // namespace detail
562
563template <typename Node, PortDirection Direction>
564constexpr size_t portArraySize(StringView identifier)
565{
566 return detail::portArraySize_<Node, Direction, 0>(identifier);
567}
568
569// LCOV_EXCL_START no coverage data for compile time evaluated function
570// Get the port index for a give port name
571namespace detail
572{
573
574template <
575 typename Node, PortDirection Direction, size_t DescriptorIndex,
576 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
577// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
578constexpr std::size_t portIndex_(StringView identifier)
579{
580 static_cast<void>(identifier);
581 // since output port indices follow input port indices
582 // this must add the number of output port for invalid input port identifier
583 // to avoid that for an invalid input port identifier the index of the first output port is returned
584 if (PortDirection::INPUT == Direction)
585 {
586 return dw::framework::portSize<Node, PortDirection::OUTPUT>();
587 }
588 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
589 return 0U;
590}
591
592template <
593 typename Node, PortDirection Direction, size_t DescriptorIndex,
594 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
595 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
596 constexpr std::size_t portIndex_(StringView identifier)
597{
598 constexpr StringView descriptorName{dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()).name};
599 if (descriptorName == identifier)
600 {
601 // coverity[autosar_cpp14_a5_1_1_violation] RFD Accepted: TID-2056
602 return 0U;
603 }
604 return descriptorPortSize<Node, Direction, DescriptorIndex>() + portIndex_<Node, Direction, DescriptorIndex + 1>(identifier);
605}
606
607} // namespace detail
608
609template <typename Node, PortDirection Direction>
610// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
611constexpr size_t portIndex(StringView identifier)
612{
613 // only temporarily for backward compatibility with enum value
614 // output port indices are offset by the number of input ports
615 if (PortDirection::OUTPUT == Direction)
616 {
617 return portSize<Node, PortDirection::INPUT>() + detail::portIndex_<Node, Direction, 0>(identifier);
618 }
619 return detail::portIndex_<Node, Direction, 0>(identifier);
620}
621// LCOV_EXCL_STOP
622
623// Check if given string is a valid port name
624template <typename Node, PortDirection Direction>
625constexpr bool isValidPortIdentifier(StringView identifier)
626{
627 constexpr size_t index = portIndex<Node, Direction>(identifier);
628 return isValidPortIndex<Node, Direction>(index);
629}
630
631// Get the port index for a give port name
632namespace detail
633{
634
635template <
636 typename Node, PortDirection Direction, size_t DescriptorIndex,
637 typename std::enable_if_t<DescriptorIndex == portDescriptorSize<Node, Direction>(), void>* = nullptr>
638constexpr std::size_t portDescriptorIndex_(StringView identifier)
639{
640 (void)identifier;
641 return 0;
642}
643
644template <
645 typename Node, PortDirection Direction, size_t DescriptorIndex,
646 typename std::enable_if_t<DescriptorIndex<portDescriptorSize<Node, Direction>(), void>* = nullptr>
647 // coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
648 constexpr std::size_t portDescriptorIndex_(StringView identifier)
649{
650 constexpr auto descriptorName = dw::core::get<DescriptorIndex>(describePorts<Node, Direction>()).name;
651 if (descriptorName == identifier)
652 {
653 return 0;
654 }
655 return 1 + portDescriptorIndex_<Node, Direction, DescriptorIndex + 1>(identifier);
656}
657
658} // namespace detail
659
660template <typename Node, PortDirection Direction>
661constexpr size_t portDescriptorIndex(StringView identifier)
662{
663 return detail::portDescriptorIndex_<Node, Direction, 0>(identifier);
664}
665
666} // namespace framework
667} // namespace dw
668
669#endif // DW_FRAMEWORK_PORTDESCRIPTOR_HPP_
constexpr auto describePorts()
constexpr bool descriptorPortArray()
constexpr size_t portIndex(StringView identifier)
constexpr size_t descriptorPortSize()
constexpr auto describePortArray(dw::core::StringView typeName, dw::core::StringView name, PortBinding binding=PortBinding::OPTIONAL, dw::core::StringView comment=""_sv) -> PortDescriptorT< PortType, ArraySize, NameSize >
constexpr size_t portDescriptorIndex(StringView identifier)
constexpr auto describeNodeInputPorts()
constexpr auto portDescriptorType()
constexpr auto describePort(dw::core::StringView typeName, dw::core::StringView name, PortBinding binding=PortBinding::OPTIONAL, dw::core::StringView comment=""_sv) -> PortDescriptorT< PortType, 0, NameSize >
constexpr size_t portArraySize(StringView identifier)
constexpr std::size_t portDescriptorSize()
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 describeNodeOutputPorts()
constexpr bool isValidPortIdentifier(StringView identifier)
constexpr dw::core::StringView descriptorPortComment()
constexpr auto portType()
constexpr size_t descriptorIndex()
Definition: Buffer.hpp:41
static constexpr size_t arraySize
dw::core::StringView typeName
dw::core::StringView comment
constexpr PortDescriptorT(dw::core::StringView &&typeName_, dw::core::StringView &&name_, PortBinding binding_=PortBinding::OPTIONAL, dw::core::StringView comment_=""_sv)
static constexpr size_t nameSize