Loading [MathJax]/extensions/tex2jax.js
Radium Engine  1.5.29
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PortFactory.hpp
1#pragma once
2#include <Dataflow/RaDataflow.hpp>
3
4#include <Core/Utils/BijectiveAssociation.hpp>
5#include <Core/Utils/Singleton.hpp>
6#include <Dataflow/Core/PortIn.hpp>
7#include <Dataflow/Core/PortOut.hpp>
8
9namespace Ra {
10namespace Dataflow {
11namespace Core {
12
13class RA_DATAFLOW_CORE_API PortFactory
14{
15 RA_SINGLETON_INTERFACE( PortFactory );
16
17 public:
18 using PortInCtorFunctor = std::function<PortBaseInPtr( Node*, const std::string& )>;
19 using PortOutCtorFunctor = std::function<PortBaseOutPtr( Node*, const std::string& )>;
20 using PortOutSetter = std::function<void( PortBaseOut*, std::any )>;
21 using PortInGetter = std::function<std::any( PortBaseIn* )>;
22
23 PortBaseInPtr make_input_port( Node* node, const std::string& name, std::type_index type ) {
24 if ( auto itr = m_input_ctor.find( type ); itr != m_input_ctor.end() ) {
25 return itr->second( node, name );
26 }
27 LOG( Ra::Core::Utils::logERROR )
28 << "input ctor type not found " << Ra::Core::Utils::simplifiedDemangledType( type );
29 return {};
30 }
31 PortBaseOutPtr make_output_port( Node* node, const std::string& name, std::type_index type ) {
32 if ( auto itr = m_output_ctor.find( type ); itr != m_output_ctor.end() ) {
33 return itr->second( node, name );
34 }
35 LOG( Ra::Core::Utils::logERROR )
36 << "output ctor type not found " << Ra::Core::Utils::simplifiedDemangledType( type );
37 return {};
38 }
39
40 PortBaseInPtr
41 make_input_port_from_name( Node* node, const std::string& name, std::string type ) {
42 return make_input_port( node, name, m_type_to_string.key( type ) );
43 }
44 PortBaseOutPtr
45 make_output_port_from_name( Node* node, const std::string& name, std::string type ) {
46 return make_output_port( node, name, m_type_to_string.key( type ) );
47 }
48
49 PortOutSetter output_setter( std::type_index type ) { return m_output_setter.at( type ); }
50 PortInGetter input_getter( std::type_index type ) { return m_input_getter.at( type ); }
51
52 template <typename T>
53 void add_port_type() {
54
55 auto type = std::type_index( typeid( T ) );
56 if ( !m_type_to_string.valueIfExists( type ) ) {
57 m_input_ctor[type] = []( Node* node, const std::string& name ) {
58 return std::make_shared<PortIn<T>>( node, name );
59 };
60
61 m_output_ctor[type] = []( Node* node, const std::string& name ) {
62 return std::make_shared<PortOut<T>>( node, name );
63 };
64
65 m_input_getter[type] = []( PortBaseIn* port ) -> std::any {
66 auto casted = dynamic_cast<PortIn<T>*>( port );
67 return &( casted->data() );
68 };
69 m_output_setter[type] = []( PortBaseOut* port, std::any any ) {
70 T* data = std::any_cast<T*>( any );
71 auto casted = dynamic_cast<PortOut<T>*>( port );
72 casted->set_data( data );
73 };
74
75 m_type_to_string.insert( type, Ra::Core::Utils::simplifiedDemangledType( type ) );
76 }
77 }
78
79 private:
80 PortFactory() {
81 // add_port_type is done in port ctor.
82 // might be needed here for serialization (if add_port_type before create node with these
83 // port.
84 using namespace Ra::Core;
85 add_port_type<Scalar>();
86 add_port_type<int>();
87 add_port_type<unsigned int>();
88 add_port_type<Utils::Color>();
89 add_port_type<Vector2>();
90 add_port_type<Vector3>();
91 add_port_type<Vector4>();
92 add_port_type<std::function<float( const float& )>>();
93 }
94
99
101};
102template <typename T>
103void add_port_type() {
104 PortFactory::getInstance()->add_port_type<T>();
105}
106} // namespace Core
107} // namespace Dataflow
108} // namespace Ra
Bijective association between two sets {keys} and {values} having the same cardinality....
T make_shared(T... args)
This namespace contains everything "low level", related to data, datastuctures, and computation.
Definition Cage.cpp:5
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:4