Loading [MathJax]/jax/input/TeX/config.js
Radium Engine  1.5.28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PortOut.hpp
1#pragma once
2
3#include <Dataflow/RaDataflow.hpp>
4
5#include <Core/Utils/Log.hpp>
6#include <Core/Utils/TypesUtils.hpp>
7
8#include <Dataflow/Core/Port.hpp>
9#include <stdexcept>
10
11namespace Ra {
12namespace Dataflow {
13namespace Core {
14
15class PortBaseIn;
16template <typename T>
17class PortIn;
18
19class RA_DATAFLOW_CORE_API PortBaseOut : public PortBase
20{
21 public:
25 PortBaseOut() = delete;
26 PortBaseOut( const PortBaseOut& ) = delete;
27 PortBaseOut& operator=( const PortBaseOut& ) = delete;
29
32 template <typename T>
33 T& data();
34
38 template <typename T>
39 void set_data( T* data );
40
41 // called by PortIn when connect
42 virtual void increase_link_count() {
43 ++m_linkCount;
44 CORE_ASSERT( m_linkCount >= 0, "link count error" );
45 }
46
47 // called by PortIn when disconnect
48 virtual void decrease_link_count() {
49 --m_linkCount;
50 CORE_ASSERT( m_linkCount >= 0, "link count error" );
51 }
52 virtual int link_count() { return m_linkCount; }
53
54 protected:
59 PortBaseOut( Node* node, const std::string& name, std::type_index type );
61 int m_linkCount { 0 };
62
63}; // class PortBaseOut
64
71template <typename T>
72class PortOut : public PortBaseOut
73{
74 public:
75 using DataType = T;
76
80 PortOut() = delete;
81 PortOut( const PortOut& ) = delete;
82 PortOut& operator=( const PortOut& ) = delete;
83
87 PortOut( Node* node, const std::string& name ) : PortBaseOut( node, name, typeid( T ) ) {
88 add_port_type<T>();
89 }
90
91 PortOut( Node* node, T* data, const std::string& name ) :
92 PortBaseOut( node, name, typeid( T ) ), m_data { data } {
93 add_port_type<T>();
94 }
96
98 T& data() { return *m_data; }
101 void set_data( T* data ) { m_data = data; }
103 bool has_data() override { return ( m_data ); }
104
105 private:
110 T* m_data { nullptr };
111
112}; // class PortOut<T>
113
114template <typename Type>
115using PortOutPtr = PortPtr<PortOut<Type>>;
116template <typename Type>
117using PortOutRawPtr = typename PortOutPtr<Type>::element_type*;
118
119using PortBaseOutRawPtr = PortRawPtr<PortBaseOut>;
120using PortBaseOutPtr = PortPtr<PortBaseOut>;
121
122template <typename T>
123T& PortBaseOut::data() {
124 auto thisOut = dynamic_cast<PortOut<T>*>( this );
125 if ( thisOut && thisOut->has_data() ) { return thisOut->data(); }
126
127 using namespace Ra::Core::Utils;
128 LOG( Ra::Core::Utils::logERROR )
129 << "Unable to get data with type " << simplifiedDemangledType<T>() << " on port " << name()
130 << " which expect " << port_typename() << ".\n";
131 throw std::runtime_error( "data reference do not exists" );
132}
133
134template <typename T>
135void PortBaseOut::set_data( T* data ) {
136 auto thisOut = dynamic_cast<PortOut<T>*>( this );
137 if ( thisOut ) {
138 thisOut->set_data( data );
139 return;
140 }
141
142 using namespace Ra::Core::Utils;
143 LOG( Ra::Core::Utils::logERROR )
144 << "Unable to set data with type " << simplifiedDemangledType( *data ) << " on port "
145 << name() << " which expect " << port_typename() << ".\n";
146 throw std::runtime_error( "data reference do not exists" );
147}
148
149} // namespace Core
150} // namespace Dataflow
151} // namespace Ra
Base abstract class for all the nodes added and used by the node system.
Definition Node.hpp:40
Base class for nodes' ports A port is a strongly typed extremity of connections between nodes.
Definition Port.hpp:30
std::string port_typename() const
Gets the human readable type of the port object.
Definition Port.hpp:62
const std::string & name() const
Gets the port's name.
Definition Port.hpp:52
Node * node() const
Gets a pointer to the node this port belongs to.
Definition Port.hpp:58
Input port accepting data of type T.
Definition PortOut.hpp:17
Forward PortOut classes used by getLink and reflect.
Definition PortOut.hpp:73
PortOut(Node *node, const std::string &name)
Definition PortOut.hpp:87
bool has_data() override
Returns true if the pointer to the data is not null.
Definition PortOut.hpp:103
T & data()
Gets a reference to the data this ports points to.
Definition PortOut.hpp:98
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:4