Compute Graph Framework SDK Reference  5.16
NodeFactory.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_NODEFACTORY_HPP_
32#define DW_FRAMEWORK_NODEFACTORY_HPP_
33
34#include <dwcgf/node/Node.hpp>
35
36#include <dwshared/dwfoundation/dw/core/logger/Logger.hpp>
41
42#include <dwshared/dwfoundation/dw/core/container/VectorFixed.hpp>
43#include <dwshared/dwfoundation/dw/core/container/StringView.hpp>
44
45#include <iostream>
46#include <map>
47#include <memory>
48#include <mutex>
49#include <vector>
50
51namespace dw
52{
53namespace framework
54{
55
56class Node;
57class ParameterProvider;
58
59namespace detail
60{
61
62class AbstractMetaObject
63{
64public:
65 AbstractMetaObject(dw::core::StringView&& className);
66
67 virtual ~AbstractMetaObject() = default;
68
69 const dw::core::StringView& className() const;
70
71 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
72 virtual const PortCollectionDescriptor& getInputPorts() const = 0;
73
74 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
75 virtual const PortCollectionDescriptor& getOutputPorts() const = 0;
76
77 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
78 virtual const ParameterCollectionDescriptor& getParameters() const = 0;
79
80 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
81 virtual const PassCollectionDescriptor& getPasses() const = 0;
82
83 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
84 virtual std::unique_ptr<Node> create(ParameterProvider& provider) const = 0;
85
86 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
87 virtual GenericDataReference createInputPortSpecimen(const dw::core::StringView& identifier) const = 0;
88
89 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
90 virtual GenericDataReference createOutputPortSpecimen(const dw::core::StringView& identifier) const = 0;
91
92protected:
93 dw::core::StringView m_className;
94};
95
96using FactoryMap = std::map<dw::core::StringView, std::unique_ptr<AbstractMetaObject>>;
97
98FactoryMap& getFactoryMap();
99
100using FactoryErrorMap = std::map<dw::core::StringView, std::vector<dw::core::StringView>>;
101
102FactoryErrorMap& getFactoryErrorMap();
103
104std::recursive_mutex& getFactoryMutex();
105
106// coverity[autosar_cpp14_a14_1_1_violation]
107template <typename NodeT>
108class MetaObject : public AbstractMetaObject
109{
110public:
111 using AbstractMetaObject::AbstractMetaObject;
112
113 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
114 const PortCollectionDescriptor& getInputPorts() const override
115 {
116 // coverity[autosar_cpp14_a3_3_2_violation]
117 static const PortCollectionDescriptor descriptor{createPortCollectionDescriptor<NodeT, PortDirection::INPUT>()};
118 return descriptor;
119 }
120
121 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
122 const PortCollectionDescriptor& getOutputPorts() const override
123 {
124 // coverity[autosar_cpp14_a3_3_2_violation]
125 static const PortCollectionDescriptor descriptor{createPortCollectionDescriptor<NodeT, PortDirection::OUTPUT>()};
126 return descriptor;
127 }
128
129 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
130 const ParameterCollectionDescriptor& getParameters() const override
131 {
132 // coverity[autosar_cpp14_a3_3_2_violation]
133 static const ParameterCollectionDescriptor descriptor{createParameterCollectionDescriptor<NodeT>()};
134 return descriptor;
135 }
136
137 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
138 const PassCollectionDescriptor& getPasses() const override
139 {
140 // coverity[autosar_cpp14_a3_3_2_violation]
141 static const PassCollectionDescriptor descriptor{createPassCollectionDescriptor<NodeT>()};
142 return descriptor;
143 }
144
145 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
146 std::unique_ptr<Node> create(ParameterProvider& provider) const override
147 {
148 return NodeT::create(provider);
149 }
150
151 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
152 GenericDataReference createInputPortSpecimen(const dw::core::StringView& identifier) const override
153 {
154 size_t const inputDescriptorIndex{getInputPorts().getDescriptorIndex(identifier.data())};
155 return dw::framework::detail::createPortSpecimen<NodeT, PortDirection::INPUT>(inputDescriptorIndex);
156 }
157
158 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
159 GenericDataReference createOutputPortSpecimen(const dw::core::StringView& identifier) const override
160 {
161 size_t const outputDescriptorIndex{getOutputPorts().getDescriptorIndex(identifier.data())};
162 return dw::framework::detail::createPortSpecimen<NodeT, PortDirection::OUTPUT>(outputDescriptorIndex);
163 }
164};
165
166} // namespace detail
167
168template <typename NodeT>
169// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
170void registerNode(const char* className) noexcept
171{
172 std::unique_ptr<detail::MetaObject<NodeT>> metaObject{std::make_unique<detail::MetaObject<NodeT>>(className)};
173
174 // coverity[autosar_cpp14_m0_1_3_violation] RFD Accepted: TID-1995
175 std::lock_guard<std::recursive_mutex> lock{detail::getFactoryMutex()};
176 if (nullptr == metaObject.get())
177 {
178 detail::FactoryErrorMap& factoryErrorMap{detail::getFactoryErrorMap()};
179 factoryErrorMap[className].push_back(dw::core::StringView("Failed to allocate meta object"));
180 return;
181 }
182
183 detail::FactoryMap& factoryMap{detail::getFactoryMap()};
184 if (factoryMap.find(className) != factoryMap.end())
185 {
186 detail::FactoryErrorMap& factoryErrorMap{detail::getFactoryErrorMap()};
187 factoryErrorMap[className].push_back(dw::core::StringView("Repeated registration of the same class name"));
188 return;
189 }
190
191 factoryMap[className] = std::move(metaObject);
192}
193
194dw::core::HeapVectorFixed<dw::core::StringView> getNodeNames();
195
196const PortCollectionDescriptor& getInputPorts(const dw::core::StringView& className);
197
198const PortCollectionDescriptor& getOutputPorts(const dw::core::StringView& className);
199
200const ParameterCollectionDescriptor& getParameters(const dw::core::StringView& className);
201
202const PassCollectionDescriptor& getPasses(const dw::core::StringView& className);
203
204std::unique_ptr<Node> createNode(const dw::core::StringView& className, ParameterProvider& provider);
205
207 const dw::core::StringView& className,
208 const dw::core::StringView& identifier);
209
211 const dw::core::StringView& className,
212 const dw::core::StringView& identifier);
213
214bool hasRegistrationErrors(bool logErrors = true);
215
216namespace detail
217{
218
219dw::core::HeapVectorFixed<dw::core::StringView> getNodeNamesWithErrors();
220
221dw::core::HeapVectorFixed<dw::core::StringView> getRegistrationErrors(const dw::core::StringView& className);
222
223} // namespace detail
224} // namespace framework
225} // namespace dw
226
227#define DW_CGF_NODE_FACTORY_JOIN(a, b) a##b
228
229#define DW_REGISTER_NODE_WITH_SUFFIX_(NodeT, UniqueSuffix) \
230 namespace \
231 { \
232 class DW_CGF_NODE_FACTORY_JOIN(Proxy, UniqueSuffix) \
233 { \
234 public: \
235 DW_CGF_NODE_FACTORY_JOIN(Proxy, UniqueSuffix) \
236 () \
237 { \
238 dw::framework::registerNode<NodeT>(#NodeT); \
239 } \
240 }; \
241 static DW_CGF_NODE_FACTORY_JOIN(Proxy, UniqueSuffix) DW_CGF_NODE_FACTORY_JOIN(g_registerNode, UniqueSuffix){}; \
242 } // namespace
243
244#define DW_REGISTER_NODE_EXPAND_(NodeT, UniqueSuffixMacro) DW_REGISTER_NODE_WITH_SUFFIX_(NodeT, UniqueSuffixMacro)
245
246#define DW_REGISTER_NODE(NodeT) DW_REGISTER_NODE_EXPAND_(NodeT, __LINE__)
247
248#endif //DW_FRAMEWORK_NODEFACTORY_HPP_
The interface to access parameter values identified by name and/or (semantic) type.
size_t getDescriptorIndex(const char *identifier) const
GenericDataReference createOutputPortSpecimen(const dw::core::StringView &className, const dw::core::StringView &identifier)
const PortCollectionDescriptor & getOutputPorts(const dw::core::StringView &className)
GenericDataReference createInputPortSpecimen(const dw::core::StringView &className, const dw::core::StringView &identifier)
dw::core::HeapVectorFixed< dw::core::StringView > getNodeNames()
const PassCollectionDescriptor & getPasses(const dw::core::StringView &className)
std::unique_ptr< Node > createNode(const dw::core::StringView &className, ParameterProvider &provider)
void registerNode(const char *className) noexcept
const PortCollectionDescriptor & getInputPorts(const dw::core::StringView &className)
const ParameterCollectionDescriptor & getParameters(const dw::core::StringView &className)
bool hasRegistrationErrors(bool logErrors=true)
Definition: Buffer.hpp:40