Loading [MathJax]/extensions/tex2jax.js
Radium Engine  1.5.28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FunctionNode.hpp
1#pragma once
2#include <Dataflow/Core/Node.hpp>
3
4#include <functional>
5
6namespace Ra {
7namespace Dataflow {
8namespace Core {
9namespace Functionals {
10
11template <typename Input, typename Output = Input>
12class FunctionNode : public Node
13{
14 public:
15 using Function = std::function<Output( const Input& )>;
16 static auto IdentityFunction( const Input& x ) -> Output { return Output { x }; }
17
18 FunctionNode( const std::string& instanceName, Function function = IdentityFunction );
19
20 void init() override { Node::init(); }
21 bool execute() override;
22
23 void set_function( Function function ) { m_port_in_op->set_default_value( function ); }
24
25 static const std::string& node_typename();
26
27 protected:
28 FunctionNode( const std::string& instanceName, const std::string& typeName, Function function );
29
30 void toJsonInternal( nlohmann::json& data ) const override { Node::toJsonInternal( data ); }
31 bool fromJsonInternal( const nlohmann::json& data ) override {
32 return Node::fromJsonInternal( data );
33 }
34
35 private:
36 RA_NODE_PORT_IN( Input, data );
37 RA_NODE_PORT_IN( Function, op );
38 RA_NODE_PORT_OUT_WITH_DATA( Output, result );
39};
40
41// -----------------------------------------------------------------
42
43template <typename Input, typename Output>
44FunctionNode<Input, Output>::FunctionNode( const std::string& instanceName, Function function ) :
45 FunctionNode( instanceName, node_typename(), function ) {}
46
47template <typename Input, typename Output>
48bool FunctionNode<Input, Output>::execute() {
49 const auto& f = m_port_in_op->data();
50 const auto& x = m_port_in_data->data();
51
52 m_result = f( x );
53
54 return true;
55}
56
57template <typename Input, typename Output>
58const std::string& FunctionNode<Input, Output>::node_typename() {
59 static std::string demangledName =
60 std::string { "Function<" } + Ra::Core::Utils::simplifiedDemangledType<Input>() + ">";
61 return demangledName;
62}
63
64template <typename Input, typename Output>
65FunctionNode<Input, Output>::FunctionNode( const std::string& instanceName,
66 const std::string& typeName,
67 Function function ) :
68 Node( instanceName, typeName ) {
69 m_port_in_op->set_default_value( function );
70}
71
72} // namespace Functionals
73} // namespace Core
74} // namespace Dataflow
75} // namespace Ra
virtual void init()
Initializes the node content.
Definition Node.hpp:428
virtual bool fromJsonInternal(const nlohmann::json &data)
Internal json representation of the Node.
Definition Node.cpp:91
virtual void toJsonInternal(nlohmann::json &data) const
Internal json representation of the Node.
Definition Node.cpp:109
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:4