Compute Graph Framework SDK Reference  5.22
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-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_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]
72 virtual const PortCollectionDescriptor& getInputPorts() const = 0;
73
74 // coverity[autosar_cpp14_a2_10_5_violation]
75 virtual const PortCollectionDescriptor& getOutputPorts() const = 0;
76
77 // coverity[autosar_cpp14_a2_10_5_violation]
78 virtual const ParameterCollectionDescriptor& getParameters() const = 0;
79
80 // coverity[autosar_cpp14_a2_10_5_violation]
81 virtual const PassCollectionDescriptor& getPasses() const = 0;
82
83 // coverity[autosar_cpp14_a2_10_5_violation]
84 virtual std::unique_ptr<Node> create(ParameterProvider& provider) const = 0;
85
86 // coverity[autosar_cpp14_a2_10_5_violation]
87 virtual GenericDataReference createInputPortSpecimen(const dw::core::StringView& identifier) const = 0;
88
89 // coverity[autosar_cpp14_a2_10_5_violation]
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
106template <typename NodeT>
107class MetaObject : public AbstractMetaObject
108{
109 static_assert(std::is_base_of<Node, NodeT>::value, "NodeT must inherit from Node");
110
111public:
112 using AbstractMetaObject::AbstractMetaObject;
113
114 // coverity[autosar_cpp14_a2_10_5_violation]
115 const PortCollectionDescriptor& getInputPorts() const override
116 {
117 // coverity[autosar_cpp14_a3_3_2_violation] RFD Pending: TID-2534
118 static const PortCollectionDescriptor descriptor{createPortCollectionDescriptor<NodeT, PortDirection::INPUT>()}; // LCOV_EXCL_LINE branches for thrown exceptions in defensive code can't be triggered
119 return descriptor;
120 }
121
122 // coverity[autosar_cpp14_a2_10_5_violation]
123 const PortCollectionDescriptor& getOutputPorts() const override
124 {
125 // coverity[autosar_cpp14_a3_3_2_violation] RFD Pending: TID-2534
126 static const PortCollectionDescriptor descriptor{createPortCollectionDescriptor<NodeT, PortDirection::OUTPUT>()}; // LCOV_EXCL_LINE branches for thrown exceptions in defensive code can't be triggered
127 return descriptor;
128 }
129
130 // coverity[autosar_cpp14_a2_10_5_violation]
131 const ParameterCollectionDescriptor& getParameters() const override
132 {
133 // coverity[autosar_cpp14_a3_3_2_violation] RFD Pending: TID-2534
134 static const ParameterCollectionDescriptor descriptor{createParameterCollectionDescriptor<NodeT>()}; // LCOV_EXCL_LINE branches for thrown exceptions in defensive code can't be triggered
135 return descriptor;
136 }
137
138 // coverity[autosar_cpp14_a2_10_5_violation]
139 const PassCollectionDescriptor& getPasses() const override
140 {
141 // coverity[autosar_cpp14_a3_3_2_violation] RFD Pending: TID-2534
142 static const PassCollectionDescriptor descriptor{createPassCollectionDescriptor<NodeT>()}; // LCOV_EXCL_LINE branches for thrown exceptions in defensive code can't be triggered
143 return descriptor;
144 }
145
146 // coverity[autosar_cpp14_a2_10_5_violation]
147 std::unique_ptr<Node> create(ParameterProvider& provider) const override
148 {
149 return NodeT::create(provider);
150 }
151
152 // coverity[autosar_cpp14_a2_10_5_violation]
153 GenericDataReference createInputPortSpecimen(const dw::core::StringView& identifier) const override
154 {
155 size_t const inputDescriptorIndex{getInputPorts().getDescriptorIndex(identifier.data())};
156 return dw::framework::detail::createPortSpecimen<NodeT, PortDirection::INPUT>(inputDescriptorIndex);
157 }
158
159 // coverity[autosar_cpp14_a2_10_5_violation]
160 GenericDataReference createOutputPortSpecimen(const dw::core::StringView& identifier) const override
161 {
162 size_t const outputDescriptorIndex{getOutputPorts().getDescriptorIndex(identifier.data())};
163 return dw::framework::detail::createPortSpecimen<NodeT, PortDirection::OUTPUT>(outputDescriptorIndex);
164 }
165};
166
167} // namespace detail
168
169template <typename NodeT>
170// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
171void registerNode(const char* className) noexcept
172{
173 std::unique_ptr<detail::MetaObject<NodeT>> metaObject{std::make_unique<detail::MetaObject<NodeT>>(className)};
174
175 // coverity[autosar_cpp14_m0_1_3_violation] RFD Accepted: TID-1995
176 std::lock_guard<std::recursive_mutex> lock{detail::getFactoryMutex()};
177 detail::FactoryMap& factoryMap{detail::getFactoryMap()};
178 if (factoryMap.find(metaObject->className()) != factoryMap.end())
179 {
180 detail::FactoryErrorMap& factoryErrorMap{detail::getFactoryErrorMap()};
181 factoryErrorMap[metaObject->className()].push_back(dw::core::StringView("Repeated registration of the same class name"));
182 return;
183 }
184
185 factoryMap[metaObject->className()] = std::move(metaObject);
186}
187
188dw::core::HeapVectorFixed<dw::core::StringView> getNodeNames();
189
190const PortCollectionDescriptor& getInputPorts(const dw::core::StringView& className);
191
192const PortCollectionDescriptor& getOutputPorts(const dw::core::StringView& className);
193
194const ParameterCollectionDescriptor& getParameters(const dw::core::StringView& className);
195
196const PassCollectionDescriptor& getPasses(const dw::core::StringView& className);
197
198std::unique_ptr<Node> createNode(const dw::core::StringView& className, ParameterProvider& provider);
199
201 const dw::core::StringView& className,
202 const dw::core::StringView& identifier);
203
205 const dw::core::StringView& className,
206 const dw::core::StringView& identifier);
207
208bool hasRegistrationErrors(bool logErrors = true);
209
210namespace detail
211{
212
213dw::core::HeapVectorFixed<dw::core::StringView> getNodeNamesWithErrors();
214
215dw::core::HeapVectorFixed<dw::core::StringView> getRegistrationErrors(const dw::core::StringView& className);
216
217} // namespace detail
218} // namespace framework
219} // namespace dw
220
221#define DW_CGF_NODE_FACTORY_JOIN(a, b) a##b
222
223#define DW_REGISTER_NODE_WITH_SUFFIX_(NodeT, UniqueSuffix) \
224 namespace \
225 { \
226 class DW_CGF_NODE_FACTORY_JOIN(Proxy, UniqueSuffix) \
227 { \
228 public: \
229 DW_CGF_NODE_FACTORY_JOIN(Proxy, UniqueSuffix) \
230 () \
231 { \
232 dw::framework::registerNode<NodeT>(#NodeT); \
233 } \
234 }; \
235 static DW_CGF_NODE_FACTORY_JOIN(Proxy, UniqueSuffix) DW_CGF_NODE_FACTORY_JOIN(g_registerNode, UniqueSuffix){}; \
236 } // namespace
237
238#define DW_REGISTER_NODE_EXPAND_(NodeT, UniqueSuffixMacro) DW_REGISTER_NODE_WITH_SUFFIX_(NodeT, UniqueSuffixMacro)
239
240#define DW_REGISTER_NODE(NodeT) DW_REGISTER_NODE_EXPAND_(NodeT, __LINE__)
241
242#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:41