Compute Graph Framework SDK Reference  5.14
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
50namespace dw
51{
52namespace framework
53{
54
55class Node;
56class ParameterProvider;
57
58namespace detail
59{
60
61class AbstractMetaObject
62{
63public:
64 AbstractMetaObject(const dw::core::StringView className);
65
66 virtual ~AbstractMetaObject() = default;
67
68 const dw::core::StringView& className() const;
69
70 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
71 virtual const PortCollectionDescriptor& getInputPorts() const = 0;
72
73 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
74 virtual const PortCollectionDescriptor& getOutputPorts() const = 0;
75
76 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
77 virtual const ParameterCollectionDescriptor& getParameters() const = 0;
78
79 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
80 virtual const PassCollectionDescriptor& getPasses() const = 0;
81
82 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
83 virtual std::unique_ptr<Node> create(ParameterProvider& provider) const = 0;
84
85 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
86 virtual GenericDataReference createInputPortSpecimen(const dw::core::StringView& identifier) const = 0;
87
88 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
89 virtual GenericDataReference createOutputPortSpecimen(const dw::core::StringView& identifier) const = 0;
90
91protected:
92 dw::core::StringView m_className;
93};
94
95using FactoryMap = std::map<dw::core::StringView, std::unique_ptr<AbstractMetaObject>>;
96
97FactoryMap& getFactoryMap();
98
99std::recursive_mutex& getFactoryMapMutex();
100
101// coverity[autosar_cpp14_a14_1_1_violation]
102template <typename NodeT>
103class MetaObject : public AbstractMetaObject
104{
105public:
106 using AbstractMetaObject::AbstractMetaObject;
107
108 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
109 const PortCollectionDescriptor& getInputPorts() const override
110 {
111 // coverity[autosar_cpp14_a3_3_2_violation]
112 static const PortCollectionDescriptor descriptor{createPortCollectionDescriptor<NodeT, PortDirection::INPUT>()};
113 return descriptor;
114 }
115
116 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
117 const PortCollectionDescriptor& getOutputPorts() const override
118 {
119 // coverity[autosar_cpp14_a3_3_2_violation]
120 static const PortCollectionDescriptor descriptor{createPortCollectionDescriptor<NodeT, PortDirection::OUTPUT>()};
121 return descriptor;
122 }
123
124 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
125 const ParameterCollectionDescriptor& getParameters() const override
126 {
127 // coverity[autosar_cpp14_a3_3_2_violation]
128 static const ParameterCollectionDescriptor descriptor{createParameterCollectionDescriptor<NodeT>()};
129 return descriptor;
130 }
131
132 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
133 const PassCollectionDescriptor& getPasses() const override
134 {
135 // coverity[autosar_cpp14_a3_3_2_violation]
136 static const PassCollectionDescriptor descriptor{createPassCollectionDescriptor<NodeT>()};
137 return descriptor;
138 }
139
140 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
141 std::unique_ptr<Node> create(ParameterProvider& provider) const override
142 {
143 return NodeT::create(provider);
144 }
145
146 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
147 GenericDataReference createInputPortSpecimen(const dw::core::StringView& identifier) const override
148 {
149 size_t const inputDescriptorIndex{getInputPorts().getDescriptorIndex(identifier.data())};
150 return dw::framework::detail::createPortSpecimen<NodeT, PortDirection::INPUT>(inputDescriptorIndex);
151 }
152
153 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
154 GenericDataReference createOutputPortSpecimen(const dw::core::StringView& identifier) const override
155 {
156 size_t const outputDescriptorIndex{getOutputPorts().getDescriptorIndex(identifier.data())};
157 return dw::framework::detail::createPortSpecimen<NodeT, PortDirection::OUTPUT>(outputDescriptorIndex);
158 }
159};
160
161} // namespace detail
162
163template <typename NodeT>
164// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
165void registerNode(const char* className)
166{
167 std::unique_ptr<detail::MetaObject<NodeT>> metaObject{std::make_unique<detail::MetaObject<NodeT>>(className)};
168 if (metaObject.get() == nullptr)
169 {
170 throw ExceptionWithStatus(DW_BAD_ALLOC, "NodeFactory: cannot allocate meta object");
171 }
172
173 // coverity[autosar_cpp14_m0_1_3_violation] RFD Accepted: TID-1995
174 std::lock_guard<std::recursive_mutex> lock{detail::getFactoryMapMutex()};
175 detail::FactoryMap& factoryMap{detail::getFactoryMap()};
176 if (factoryMap.find(className) != factoryMap.end())
177 {
178 // coverity[cert_con51_cpp_violation] FP: nvbugs/3632417
179 throw ExceptionWithStatus(DW_INVALID_ARGUMENT, "registerNode() repeatedly called for the same class name: ", className);
180 }
181 else
182 {
183 factoryMap[className] = std::move(metaObject);
184 }
185}
186
187dw::core::HeapVectorFixed<dw::core::StringView> getNodeNames();
188
189const PortCollectionDescriptor& getInputPorts(const dw::core::StringView& className);
190
191const PortCollectionDescriptor& getOutputPorts(const dw::core::StringView& className);
192
193const ParameterCollectionDescriptor& getParameters(const dw::core::StringView& className);
194
195const PassCollectionDescriptor& getPasses(const dw::core::StringView& className);
196
197std::unique_ptr<Node> createNode(const dw::core::StringView& className, ParameterProvider& provider);
198
200 const dw::core::StringView& className,
201 const dw::core::StringView& identifier);
202
204 const dw::core::StringView& className,
205 const dw::core::StringView& identifier);
206
207} // namespace framework
208} // namespace dw
209
210#define DW_REGISTER_NODE_WITH_SUFFIX_(NodeT, UniqueSuffix) \
211 namespace \
212 { \
213 class Proxy##UniqueSuffix \
214 { \
215 public: \
216 Proxy##UniqueSuffix() \
217 { \
218 dw::framework::registerNode<NodeT>(#NodeT); \
219 } \
220 }; \
221 static Proxy##UniqueSuffix g_registerNode##UniqueSuffix{}; \
222 } // namespace
223
224#define DW_REGISTER_NODE_EXPAND_(NodeT, UniqueSuffixMacro) DW_REGISTER_NODE_WITH_SUFFIX_(NodeT, UniqueSuffixMacro)
225
226#define DW_REGISTER_NODE(NodeT) DW_REGISTER_NODE_EXPAND_(NodeT, __LINE__)
227
228#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)
const PortCollectionDescriptor & getInputPorts(const dw::core::StringView &className)
auto create(const ParameterProvider &provider) -> std::unique_ptr< NodeT >
void registerNode(const char *className)
const ParameterCollectionDescriptor & getParameters(const dw::core::StringView &className)
Definition: Buffer.hpp:40