Compute Graph Framework SDK Reference  5.8
ChannelPacketTypes.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) 2019-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_CHANNEL_PACKET_TYPES_HPP_
32#define DW_FRAMEWORK_CHANNEL_PACKET_TYPES_HPP_
33
34#include <typeinfo>
35#include <cstddef>
36#include <cstdint>
37#include <memory>
38#include <type_traits>
39#include <iostream>
40#include <nvscisync.h>
41#include <dw/core/language/Function.hpp>
42
43namespace dw
44{
45namespace framework
46{
47
49
51{
52 virtual const char* name() const = 0;
53};
54
55template <class T>
57{
58 const char* name() const final
59 {
60 return typeid(T).name();
61 }
62};
63
65{
66public:
68 : GenericData(nullptr, 0)
69 {
70 }
71
72 GenericData(void* data, size_t size)
73 : m_data{data}
74 , m_size{size}
75 , m_typeInfo{nullptr}
76 {
77 }
78
79 template <typename T>
81 : m_data{data}
82 , m_size{sizeof(T)}
83 {
84 static constexpr TypeInformation<T> g_typeInfo{};
85 m_typeInfo = &g_typeInfo;
86 }
87
88 size_t size() const
89 {
90 return m_size;
91 }
92
93 template <typename T>
94 T* getData() const
95 {
96 if (m_typeInfo == nullptr && m_size == sizeof(T))
97 {
98 // Type info not found but sizes match
99 return static_cast<T*>(m_data);
100 }
101 else if (m_size != sizeof(T))
102 {
103 // Wrong size
104 return nullptr;
105 }
106 else if (dynamic_cast<const TypeInformation<T>*>(m_typeInfo) != nullptr)
107 {
108 // Type info found and matched
109 return static_cast<T*>(m_data);
110 }
111 else
112 {
113 // Wrong type
114 return nullptr;
115 }
116 }
117
118 void* getPointer() const
119 {
120 return m_data;
121 }
122
123private:
124 void* m_data;
125 size_t m_size;
126 const ITypeInformation* m_typeInfo;
127};
128
129using ChannelPacketTypeID = uint32_t;
131constexpr uint32_t DWFRAMEWORK_SYNCED_PACKET_TYPE_ID_OFFSET = 0x80000000;
132constexpr uint32_t DWFRAMEWORK_MAX_INTERNAL_TYPES = 0x400;
133
134#define DW_CHANNEL_PACKET_TYPES_LIST(_s) \
135 _s(DW_IMAGE_HANDLE) \
136 _s(DW_LATENCY) \
137 _s(DW_PYRAMID_IMAGE) \
138 _s(DW_FEATURE_ARRAY) \
139 _s(DW_FEATURE_HISTORY_ARRAY) \
140 _s(DW_FEATURE_NCC_SCORES) \
141 _s(DW_SENSOR_NODE_RAW_DATA) \
142 _s(DW_RADAR_SCAN) \
143 _s(DW_LIDAR_DECODE_PACKET) \
144 _s(DW_EGOMOTION_STATE_HANDLE) \
145 _s(DW_POINT_CLOUD) \
146 _s(DW_LIDAR_PACKETS_ARRAY) \
147 _s(DW_TRACE_NODE_DATA) \
148 _s(DW_CODEC_PACKET) \
149 _s(DW_SENSOR_SERVICE_RAW_DATA)
150
151// Enumerate the classes for channel packet types
152// The DEFUALT class assumes the data is a contiguous chunk of memory
153// Other classes are defined for specific complex objects.
154// See packet_traits<> helper for mapping from this enum to the type the
155// enum corresponds to.
156// TODO: add APIs to allow external complex types to be registered.
157
158#define DW_CHANNEL_GENERATE_ENUM(x) x,
159
161{
162 DEFAULT = DWFRAMEWORK_PACKET_ID_DEFAULT, // Assumes packets are contiguous chunk of memory
165};
166
168{
169 uint32_t syncCount;
171};
172
176using OnSetSyncAttrs = dw::core::Function<void(NvSciSyncAttrList)>;
177
179// Specimen for GenericData type of channel
180// There are two modes of use for this struct:
181// 1. The data pointed to by data is non-owned, pCopy is null.
182// 2. The data pointed to by data is pCopy.get(), pCopy is non-null, memory is owned.
184{
185 std::shared_ptr<void> pCopy;
188 size_t typeSize;
191};
192
193template <typename T>
195{
196 // TODO(chale): enable following code once we actually fix everywhere that POD is used with channel
197 // but is not declared as such.
198 // static_assert(!std::is_same<T, T>::value,
199 // "Attempting to use type with Port/Channel that has no declared packet handling. "
200 // "A packet handling must be declared with DWFRAMEWORK_DECLARE_PACKET_TYPE_RELATION.");
201 using SpecimenT = T;
203 static constexpr bool IsDeclared = false;
204};
205
206template <ChannelPacketTypeID PacketTID>
208{
209 // TODO(chale): enable following code once we actually fix everywhere that POD is used with channel
210 // but is not declared as such.
211 // static_assert(PacketTID != PacketTID,
212 // "Attempting to use type with Port/Channel that has no declared packet handling. "
213 // "A packet handling must be declared with DWFRAMEWORK_DECLARE_PACKET_TYPE_RELATION.");
214};
215
216// Creates a generic data speciemn wrapper for type T.
217// The data specimen wrapper points to a copy of the passed specimen on the heap that it owns.
218// This is necessary to make the specimen passable/copyable by value.
219template <typename T>
221{
222 GenericDataReference result{};
224 result.typeSize = sizeof(T);
225 if (specimen != nullptr)
226 {
227 auto heapCopy = std::make_shared<typename parameter_traits<T>::SpecimenT>(*specimen);
228 result.pCopy = heapCopy;
229 result.data = GenericData(heapCopy.get());
230 }
231 else
232 {
233 result.pCopy = nullptr;
234 result.data = GenericData(specimen);
235 }
236
237 return result;
238}
239
240} // namespace framework
241} // namespace dw
242
251#define DWFRAMEWORK_DECLARE_PACKET_TYPE_RELATION(DATA_TYPE, SPECIMEN_TYPE, PACKET_TYPE_ID) \
252 namespace dw \
253 { \
254 namespace framework \
255 { \
256 template <> \
257 struct parameter_traits<DATA_TYPE> \
258 { \
259 using SpecimenT = SPECIMEN_TYPE; \
260 static constexpr ChannelPacketTypeID PacketTID = static_cast<ChannelPacketTypeID>(PACKET_TYPE_ID); \
261 static constexpr bool IsDeclared = true; \
262 }; \
263 template <> \
264 struct packet_traits<static_cast<ChannelPacketTypeID>(PACKET_TYPE_ID)> \
265 { \
266 using PacketT = DATA_TYPE; \
267 }; \
268 } \
269 }
270
275#define DWFRAMEWORK_DECLARE_PACKET_TYPE_POD(DATA_TYPE) \
276 namespace dw \
277 { \
278 namespace framework \
279 { \
280 template <> \
281 struct parameter_traits<DATA_TYPE> \
282 { \
283 using SpecimenT = DATA_TYPE; \
284 static constexpr ChannelPacketTypeID PacketTID = DWFRAMEWORK_PACKET_ID_DEFAULT; \
285 static constexpr bool IsDeclared = true; \
286 }; \
287 } \
288 }
289
290// Pre-declare some types as POD.
297
298#endif // DW_FRAMEWORK_CHANNEL_PACKET_TYPES_HPP_
GenericData(void *data, size_t size)
#define DWFRAMEWORK_DECLARE_PACKET_TYPE_POD(DATA_TYPE)
#define DW_CHANNEL_GENERATE_ENUM(x)
#define DW_CHANNEL_PACKET_TYPES_LIST(_s)
static GenericDataReference make_specimen(typename parameter_traits< T >::SpecimenT *specimen)
constexpr uint32_t DWFRAMEWORK_MAX_INTERNAL_TYPES
dw::core::Function< void(NvSciSyncAttrList)> OnSetSyncAttrs
constexpr uint32_t DWFRAMEWORK_SYNCED_PACKET_TYPE_ID_OFFSET
uint32_t ChannelPacketTypeID
constexpr ChannelPacketTypeID DWFRAMEWORK_PACKET_ID_DEFAULT
Definition: Exception.hpp:47
virtual const char * name() const =0
const char * name() const final
static constexpr ChannelPacketTypeID PacketTID