dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
NodeFactory.hpp
1// SPDX-FileCopyrightText: 2025-2026 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <functional>
13
14// Third-party includes
15#include <nlohmann/json.hpp>
16
17// Project includes
18#include <dfx-core/Node.hpp>
20#include <dfx-utilities/JsonValidator.hpp>
21#include <dfx-utilities/StringMap.hpp>
22
23namespace dfx::Graph
24{
60{
61public:
66 using NodeBuilder = std::move_only_function<dfx::Core::NodePtr (Core::Node::Id, std::string)>;
67
68public:
81
87 bool hasBuilderForType(std::string_view type) const noexcept
88 { return _builders.find(type) != _builders.end(); }
89
91 std::vector<std::string> allNodeType() const;
92
107 bool isConfigValidForType(std::string_view type, nlohmann::json const & config, std::vector<std::string> * errors = nullptr) const;
108
118 nlohmann::json getSchemaOfType(std::string_view type) const;
119
130 nlohmann::json getMetadataOfType(std::string_view type) const;
131
150 Core::NodePtr create(std::string_view type, Core::Node::Id id, std::string_view name, nlohmann::json config = {});
151
152public:
159 void registerBuilder(std::string type, nlohmann::json metadata, nlohmann::json configSchema, NodeBuilder builder);
160
161private:
162 void _initNode(Core::NodePtr node, nlohmann::json config);
163
164private:
166 Utils::JsonValidator _configValidator;
167 Utils::JsonValidator _metadataValidator;
169};
170} // !namespace dfx::Core
Convenience macros to explicitly control copy and move semantics.
Base class for all runtime-executed nodes in a dfx dataflow graph.
uint32_t Id
Node identifier type (unique and stable within a graph instance).
Definition Node.hpp:116
Core::NodePtr create(std::string_view type, Core::Node::Id id, std::string_view name, nlohmann::json config={})
Create a node instance of the given type and apply its configuration.
nlohmann::json getMetadataOfType(std::string_view type) const
Get the metadata JSON associated with a node type.
std::vector< std::string > allNodeType() const
List all registered node types.
bool hasBuilderForType(std::string_view type) const noexcept
Check whether a node type is registered.
Definition NodeFactory.hpp:87
NodeFactory()
Construct the factory and register built-in node types.
bool isConfigValidForType(std::string_view type, nlohmann::json const &config, std::vector< std::string > *errors=nullptr) const
Validate a configuration object against the schema of a given node type.
void registerBuilder(std::string type, nlohmann::json metadata, nlohmann::json configSchema, NodeBuilder builder)
Register a new node builder for a specific type in the factory.
nlohmann::json getSchemaOfType(std::string_view type) const
Get the JSON schema associated with a node type configuration.
std::move_only_function< dfx::Core::NodePtr(Core::Node::Id, std::string)> NodeBuilder
Callable used to build a node instance for a given type.
Definition NodeFactory.hpp:66
DFX_ENABLE_DEFAULT_MOVE_DISABLE_COPY(NodeFactory)
Move is allowed, copy is disabled.
Registry of JSON Schemas with validation helpers.
Definition JsonValidator.hpp:46
std::shared_ptr< Node > NodePtr
Shared ownership pointer type for Nodes..
Definition Endpoint.hpp:21
Definition Node.hpp:43
std::unordered_map< std::string, T, TransparentHash, TransparentEqual, Allocator > UnorderedStringMap
Convenience alias for an unordered map keyed by std::string with transparent lookup.
Definition StringMap.hpp:76