2#include <Dataflow/RaDataflow.hpp>
4#include <Core/Containers/VariableSet.hpp>
5#include <Core/Utils/Index.hpp>
6#include <Dataflow/Core/PortFactory.hpp>
7#include <Dataflow/Core/PortIn.hpp>
8#include <Dataflow/Core/PortOut.hpp>
10#include <nlohmann/json.hpp>
39class RA_DATAFLOW_CORE_API
Node
42 using PortIndex = Ra::Core::Utils::Index;
44 template <
typename Port>
47 template <
typename Port>
49 template <
typename Port>
50 using PortRawPtr = Port*;
52 template <
typename Type>
54 template <
typename Type>
57 template <
typename Type>
59 template <
typename Type>
63 using PortBaseRawPtr = PortRawPtr<PortBase>;
67 using PortBaseInRawPtr = PortRawPtr<PortBaseIn>;
71 using PortBaseOutRawPtr = PortRawPtr<PortBaseOut>;
74 template <
typename Port>
86 Node& operator=(
const Node& ) =
delete;
93 bool operator==(
const Node& node );
114 virtual bool compile();
131 virtual void destroy();
159 PortBaseRawPtr port_by_index(
const std::string& type, PortIndex index )
const;
161 auto input_by_index( PortIndex index )
const {
return port_base( m_inputs, index ); }
163 auto output_by_index( PortIndex index )
const {
return port_base( m_outputs, index ); }
165 template <
typename T>
167 return port<T>( m_inputs, index );
170 template <
typename T>
172 return port<T>( m_outputs, index );
180 const PortBaseInCollection& inputs()
const;
187 const PortBaseOutCollection& outputs()
const;
217 void toJson( nlohmann::json& data )
const;
224 bool fromJson(
const nlohmann::json& data );
235 void add_metadata(
const nlohmann::json& data );
238 const nlohmann::json& metadata();
240 inline bool is_initialized()
const {
return m_initialized; }
249 inline bool is_output();
252 inline bool is_input();
272 template <
typename PortType>
274 const std::string& name )
const -> IndexAndPort<PortRawPtr<PortType>>;
283 template <
typename PortType>
284 auto port_base(
const PortCollection<
PortPtr<PortType>>& ports, PortIndex idx )
const
285 -> PortRawPtr<PortType>;
295 template <
typename T,
typename PortType>
309 virtual bool fromJsonInternal(
const nlohmann::json& data );
317 virtual void toJsonInternal( nlohmann::json& data )
const;
325 template <
typename PortType>
339 template <
typename T,
typename... U>
342 return input_port<T>( idx );
351 template <
typename T,
typename... U>
354 return output_port<T>( idx );
364 template <
typename T>
370 template <
typename T>
386 template <
typename T>
387 auto add_parameter(
const std::string& name,
const T& value ) {
388 return m_parameters.insertVariable<T>( name, value ).first;
391 template <
typename T>
393 return m_parameters.deleteVariable( name );
396 template <
typename T>
397 bool remove_parameter( ParamHandle<T>& handle ) {
398 return m_parameters.deleteVariable( handle );
402 bool m_initialized {
false };
467template <
typename PortType>
472 auto it =
std::find_if( ports.begin(), ports.end(), [](
const auto& p ) { return !p; } );
473 if ( it != ports.end() ) {
479 index = ports.size() - 1;
508 ret = ret && ( p->link_count() == 0 );
517 ret = ret && p->has_default_value() && !p->is_linked();
522template <
typename PortType>
527 ports.begin(), ports.end(), [n = name](
const auto& p ) { return p && p->name() == n; } );
528 PortRawPtr<PortType> port {
nullptr };
530 if ( itr != ports.cend() ) {
534 return { portIndex, port };
537template <
typename PortType>
539 -> PortRawPtr<PortType> {
540 if ( 0 <= index &&
size_t( index ) < ports.size() ) {
return ports[index].get(); }
39class RA_DATAFLOW_CORE_API
Node {
…};
Heterogeneous container storing "Variables", that maps a name (std::string) to a value (of any type T...
typename VariableContainer< T >::iterator VariableHandle
Handle of a variable A handle on a variable with type T is an iterator into the BaseContainer....
void clear()
remove all elements from the container
Base abstract class for all the nodes added and used by the node system.
PortIndex add_output(PortBaseOutPtr out)
Convenience alias to add_port(outputs(), out)
auto input_port(PortIndex index)
Gets a typed (input/output) port.
void remove_input(PortIndex index)
Remove the given port from the managed (input/output) ports.
std::string m_model_name
The type name of the node. Initialized once at construction.
std::string m_instance_name
The instance name of the node.
auto input_by_index(PortIndex index) const
Convenience alias with typed port.
Ra::Core::VariableSet m_input_variables
virtual ~Node()=default
make Node a base abstract class
const std::string & instance_name() const
Gets the instance name of the node.
bool operator==(const Node &node)
Two nodes are considered equal if there model and instance names are the same.
virtual bool compile()
Compile the node to check its validity.
virtual void init()
Initializes the node content.
PortIndex add_input(PortBaseInPtr in)
Convenience alias to add_port(inputs(), in)
const PortBaseInCollection & inputs() const
Gets the in ports of the node.
bool is_output()
Node is output if none of the output ports is linked.
const std::string & display_name() const
Gets the display name of the node (e.g. for gui), no need to be unique in a graph.
void set_instance_name(const std::string &name)
Sets the instance name the node (unused?) instance name must be unique in a graph.
auto add_input(U &&... u)
Adds a typed input port.
void set_display_name(const std::string &name)
Set the display name.
Ra::Core::VariableSet & input_variables()
Return a variable set of input ports default value reference, if any.
auto port(const PortCollection< PortPtr< PortType > > &ports, PortIndex index) const
Gets a port in a collection by its index.
auto output_by_index(PortIndex index) const
Convenience alias with typed port.
auto input_by_index(PortIndex index) const
Convenience alias to port_by_index("in", index)
PortCollection< PortPtr< PortBaseIn > > m_inputs
The in ports of the node (own by the node)
PortCollection< PortPtr< PortBaseOut > > m_outputs
The out ports of the node (own by the node)
const std::string & model_name() const
Gets the model (type/class) name of the node.
bool is_input()
Node is input if all input ports have default values and not linked.
Ra::Core::VariableSet & parameters()
Return node's parameters.
auto port_by_name(const std::string &type, const std::string &name) const -> IndexAndPort< PortBaseRawPtr >
Get a port by its name.
PortIndex add_port(PortCollection< PortPtr< PortType > > &, PortPtr< PortType > port)
Adds a port to port collection.
bool m_initialized
Flag that checks if the node is already initialized.
auto add_output(U &&... u)
Adds a typed output port.
virtual bool execute()=0
Executes the node.
nlohmann::json m_metadata
Additional data on the node, added by application or gui or ...
const nlohmann::json & metadata()
Give access to extra json data stored on the node.
void remove_output(PortIndex index)
Remove the given port from the managed (input/output) ports.
auto output_by_index(PortIndex index) const
Convenience alias to port_by_index("out", index)
auto port_base(const PortCollection< PortPtr< PortType > > &ports, PortIndex idx) const -> PortRawPtr< PortType >
Gets the PortBase In or Out by its index.
virtual void destroy()
Delete the node's content.
auto output_port(PortIndex index)
Gets a typed (input/output) port.
Ra::Core::VariableSet m_parameters
The editable parameters of the node.
const PortBaseOutCollection & outputs() const
Gets the out ports of the node.
Input port accepting data of type T.
Forward PortOut classes used by getLink and reflect.
hepler function to manage enum as underlying types in VariableSet
T static_pointer_cast(T... args)