Compute Graph Framework SDK Reference  5.12
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>
40
41#include <dwshared/dwfoundation/dw/core/container/VectorFixed.hpp>
42#include <dwshared/dwfoundation/dw/core/container/StringView.hpp>
43
44#include <iostream>
45#include <map>
46#include <memory>
47#include <mutex>
48
49namespace dw
50{
51namespace framework
52{
53
54class Node;
55class ParameterProvider;
56
57namespace detail
58{
59
60class AbstractMetaObject
61{
62public:
63 AbstractMetaObject(const dw::core::StringView className);
64
65 virtual ~AbstractMetaObject() = default;
66
67 const dw::core::StringView& className() const;
68
69 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
70 virtual const PortCollectionDescriptor& getInputPorts() const = 0;
71
72 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
73 virtual const PortCollectionDescriptor& getOutputPorts() const = 0;
74
75 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
76 virtual const ParameterCollectionDescriptor& getParameters() const = 0;
77
78 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
79 virtual const PassCollectionDescriptor& getPasses() const = 0;
80
81 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
82 virtual std::unique_ptr<Node> create(ParameterProvider& provider) const = 0;
83
84protected:
85 dw::core::StringView m_className;
86};
87
88using FactoryMap = std::map<dw::core::StringView, std::unique_ptr<AbstractMetaObject>>;
89
90FactoryMap& getFactoryMap();
91
92std::recursive_mutex& getFactoryMapMutex();
93
94// coverity[autosar_cpp14_a14_1_1_violation]
95template <typename NodeT>
96class MetaObject : public AbstractMetaObject
97{
98public:
99 using AbstractMetaObject::AbstractMetaObject;
100
101 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
102 const PortCollectionDescriptor& getInputPorts() const override
103 {
104 // coverity[autosar_cpp14_a3_3_2_violation]
105 static const PortCollectionDescriptor descriptor{createPortCollectionDescriptor<NodeT, PortDirection::INPUT>()};
106 return descriptor;
107 }
108
109 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
110 const PortCollectionDescriptor& getOutputPorts() const override
111 {
112 // coverity[autosar_cpp14_a3_3_2_violation]
113 static const PortCollectionDescriptor descriptor{createPortCollectionDescriptor<NodeT, PortDirection::OUTPUT>()};
114 return descriptor;
115 }
116
117 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
118 const ParameterCollectionDescriptor& getParameters() const override
119 {
120 // coverity[autosar_cpp14_a3_3_2_violation]
121 static const ParameterCollectionDescriptor descriptor{createParameterCollectionDescriptor<NodeT>()};
122 return descriptor;
123 }
124
125 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
126 const PassCollectionDescriptor& getPasses() const override
127 {
128 // coverity[autosar_cpp14_a3_3_2_violation]
129 static const PassCollectionDescriptor descriptor{createPassCollectionDescriptor<NodeT>()};
130 return descriptor;
131 }
132
133 // coverity[autosar_cpp14_a2_10_5_violation] RFD Pending: TID-2053
134 std::unique_ptr<Node> create(ParameterProvider& provider) const override
135 {
136 return NodeT::create(provider);
137 }
138};
139
140} // namespace detail
141
142template <typename NodeT>
143// coverity[autosar_cpp14_a2_10_5_violation] FP: nvbugs/3907242
144void registerNode(const char* className)
145{
146 std::unique_ptr<detail::MetaObject<NodeT>> metaObject{std::make_unique<detail::MetaObject<NodeT>>(className)};
147 if (metaObject.get() == nullptr)
148 {
149 throw ExceptionWithStatus(DW_BAD_ALLOC, "NodeFactory: cannot allocate meta object");
150 }
151
152 // coverity[autosar_cpp14_m0_1_3_violation] RFD Accepted: TID-1995
153 std::lock_guard<std::recursive_mutex> lock{detail::getFactoryMapMutex()};
154 detail::FactoryMap& factoryMap{detail::getFactoryMap()};
155 if (factoryMap.find(className) != factoryMap.end())
156 {
157 // coverity[cert_con51_cpp_violation] FP: nvbugs/3632417
158 throw ExceptionWithStatus(DW_INVALID_ARGUMENT, "registerNode() repeatedly called for the same class name: ", className);
159 }
160 else
161 {
162 factoryMap[className] = std::move(metaObject);
163 }
164}
165
166dw::core::HeapVectorFixed<dw::core::StringView> getNodeNames();
167
168const PortCollectionDescriptor& getInputPorts(const dw::core::StringView& className);
169
170const PortCollectionDescriptor& getOutputPorts(const dw::core::StringView& className);
171
172const ParameterCollectionDescriptor& getParameters(const dw::core::StringView& className);
173
174const PassCollectionDescriptor& getPasses(const dw::core::StringView& className);
175
176std::unique_ptr<Node> createNode(const dw::core::StringView& className, ParameterProvider& provider);
177
178} // namespace framework
179} // namespace dw
180
181#define DW_REGISTER_NODE_WITH_SUFFIX_(NodeT, UniqueSuffix) \
182 namespace \
183 { \
184 class Proxy##UniqueSuffix \
185 { \
186 public: \
187 Proxy##UniqueSuffix() \
188 { \
189 dw::framework::registerNode<NodeT>(#NodeT); \
190 } \
191 }; \
192 static Proxy##UniqueSuffix g_registerNode##UniqueSuffix{}; \
193 } // namespace
194
195#define DW_REGISTER_NODE_(NodeT) \
196 DW_REGISTER_NODE_WITH_SUFFIX_(NodeT, UniqueSuffix)
197
198#define DW_REGISTER_NODE_GET_3RD_ARG_(arg1, arg2, arg3, ...) arg3
199#define DW_REGISTER_NODE_MACRO_CHOOSER_(...) \
200 DW_REGISTER_NODE_GET_3RD_ARG_(__VA_ARGS__, DW_REGISTER_NODE_WITH_SUFFIX_, DW_REGISTER_NODE_, )
201
202#define DW_REGISTER_NODE(...) \
203 DW_REGISTER_NODE_MACRO_CHOOSER_(__VA_ARGS__) \
204 (__VA_ARGS__)
205
206#endif //DW_FRAMEWORK_NODEFACTORY_HPP_
The interface to access parameter values identified by name and/or (semantic) type.
const PortCollectionDescriptor & getOutputPorts(const dw::core::StringView &className)
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