31#ifndef DW_FRAMEWORK_NODEFACTORY_HPP_
32#define DW_FRAMEWORK_NODEFACTORY_HPP_
36#include <dwshared/dwfoundation/dw/core/logger/Logger.hpp>
42#include <dwshared/dwfoundation/dw/core/container/VectorFixed.hpp>
43#include <dwshared/dwfoundation/dw/core/container/StringView.hpp>
57class ParameterProvider;
62class AbstractMetaObject
65 AbstractMetaObject(dw::core::StringView&& className);
67 virtual ~AbstractMetaObject() =
default;
69 const dw::core::StringView& className()
const;
72 virtual const PortCollectionDescriptor&
getInputPorts()
const = 0;
75 virtual const PortCollectionDescriptor&
getOutputPorts()
const = 0;
78 virtual const ParameterCollectionDescriptor&
getParameters()
const = 0;
81 virtual const PassCollectionDescriptor&
getPasses()
const = 0;
84 virtual std::unique_ptr<Node> create(ParameterProvider& provider)
const = 0;
93 dw::core::StringView m_className;
96using FactoryMap = std::map<dw::core::StringView, std::unique_ptr<AbstractMetaObject>>;
98FactoryMap& getFactoryMap();
100using FactoryErrorMap = std::map<dw::core::StringView, std::vector<dw::core::StringView>>;
102FactoryErrorMap& getFactoryErrorMap();
104std::recursive_mutex& getFactoryMutex();
106template <
typename NodeT>
107class MetaObject :
public AbstractMetaObject
109 static_assert(std::is_base_of<Node, NodeT>::value,
"NodeT must inherit from Node");
112 using AbstractMetaObject::AbstractMetaObject;
115 const PortCollectionDescriptor&
getInputPorts()
const override
118 static const PortCollectionDescriptor descriptor{createPortCollectionDescriptor<NodeT, PortDirection::INPUT>()};
126 static const PortCollectionDescriptor descriptor{createPortCollectionDescriptor<NodeT, PortDirection::OUTPUT>()};
131 const ParameterCollectionDescriptor&
getParameters()
const override
134 static const ParameterCollectionDescriptor descriptor{createParameterCollectionDescriptor<NodeT>()};
139 const PassCollectionDescriptor&
getPasses()
const override
142 static const PassCollectionDescriptor descriptor{createPassCollectionDescriptor<NodeT>()};
147 std::unique_ptr<Node> create(ParameterProvider& provider)
const override
149 return NodeT::create(provider);
156 return dw::framework::detail::createPortSpecimen<NodeT, PortDirection::INPUT>(inputDescriptorIndex);
163 return dw::framework::detail::createPortSpecimen<NodeT, PortDirection::OUTPUT>(outputDescriptorIndex);
169template <
typename NodeT>
173 std::unique_ptr<detail::MetaObject<NodeT>> metaObject{std::make_unique<detail::MetaObject<NodeT>>(className)};
176 std::lock_guard<std::recursive_mutex> lock{detail::getFactoryMutex()};
177 detail::FactoryMap& factoryMap{detail::getFactoryMap()};
178 if (factoryMap.find(metaObject->className()) != factoryMap.end())
180 detail::FactoryErrorMap& factoryErrorMap{detail::getFactoryErrorMap()};
181 factoryErrorMap[metaObject->className()].push_back(dw::core::StringView(
"Repeated registration of the same class name"));
185 factoryMap[metaObject->className()] = std::move(metaObject);
201 const dw::core::StringView& className,
202 const dw::core::StringView& identifier);
205 const dw::core::StringView& className,
206 const dw::core::StringView& identifier);
213dw::core::HeapVectorFixed<dw::core::StringView> getNodeNamesWithErrors();
215dw::core::HeapVectorFixed<dw::core::StringView> getRegistrationErrors(
const dw::core::StringView& className);
221#define DW_CGF_NODE_FACTORY_JOIN(a, b) a##b
223#define DW_REGISTER_NODE_WITH_SUFFIX_(NodeT, UniqueSuffix) \
226 class DW_CGF_NODE_FACTORY_JOIN(Proxy, UniqueSuffix) \
229 DW_CGF_NODE_FACTORY_JOIN(Proxy, UniqueSuffix) \
232 dw::framework::registerNode<NodeT>(#NodeT); \
235 static DW_CGF_NODE_FACTORY_JOIN(Proxy, UniqueSuffix) DW_CGF_NODE_FACTORY_JOIN(g_registerNode, UniqueSuffix){}; \
238#define DW_REGISTER_NODE_EXPAND_(NodeT, UniqueSuffixMacro) DW_REGISTER_NODE_WITH_SUFFIX_(NodeT, UniqueSuffixMacro)
240#define DW_REGISTER_NODE(NodeT) DW_REGISTER_NODE_EXPAND_(NodeT, __LINE__)
A collection of parameter descriptors.
The interface to access parameter values identified by name and/or (semantic) type.
A collection of pass descriptors.
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)