Skip to content

Commit 213bd31

Browse files
committed
[dataflow] Use macro for node_typename.
1 parent fb51507 commit 213bd31

19 files changed

Lines changed: 75 additions & 131 deletions

File tree

examples/DataflowExamples/GraphEditor/main.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "Dataflow/RaDataflow.hpp"
12
#include <QApplication>
23
#include <QCommandLineOption>
34
#include <QCommandLineParser>
@@ -17,10 +18,7 @@ class StringInput : public Ra::Dataflow::Core::Sources::SingleDataSourceNode<std
1718
public:
1819
using base = Ra::Dataflow::Core::Sources::SingleDataSourceNode<std::string>;
1920
explicit StringInput( const std::string& name ) : base( name, node_typename() ) {}
20-
static const std::string& node_typename() {
21-
static std::string n { "StringInput" };
22-
return n;
23-
}
21+
RA_NODE_TYPENAME( "StringInput" );
2422
};
2523

2624
class SquareFunction : public Ra::Dataflow::Core::Sources::FunctionSourceNode<Scalar, const Scalar&>
@@ -30,10 +28,7 @@ class SquareFunction : public Ra::Dataflow::Core::Sources::FunctionSourceNode<Sc
3028
explicit SquareFunction( const std::string& name ) : base( name, node_typename() ) {
3129
set_data( []( const Scalar& b ) { return b * b; } );
3230
}
33-
static const std::string& node_typename() {
34-
static std::string n { "SquareFunction" };
35-
return n;
36-
}
31+
RA_NODE_TYPENAME( "SquareFunction" );
3732
};
3833

3934
int main( int argc, char* argv[] ) {

src/Dataflow/Core/DataflowGraph.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class RA_DATAFLOW_CORE_API DataflowGraph : public Node
208208

209209
bool shouldBeSaved() { return m_should_save; }
210210

211-
static const std::string& node_typename();
211+
RA_NODE_TYPENAME( "Core DataflowGraph" )
212212

213213
/**
214214
* \brief Load a graph from the given file.
@@ -447,11 +447,6 @@ inline void DataflowGraph::remove_unlinked_input_output_ports() {
447447
generate_ports();
448448
}
449449

450-
inline const std::string& DataflowGraph::node_typename() {
451-
static std::string demangledTypeName { "Core DataflowGraph" };
452-
return demangledTypeName;
453-
}
454-
455450
} // namespace Core
456451
} // namespace Dataflow
457452
} // namespace Ra

src/Dataflow/Core/Functionals/BinaryOpNode.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include "Core/CoreMacros.hpp"
33
#include "Dataflow/Core/Port.hpp"
4+
#include "Dataflow/RaDataflow.hpp"
45
#include <Dataflow/Core/Node.hpp>
56

67
#include <functional>
@@ -187,13 +188,10 @@ class BinaryOpNode : public Node
187188
/// \brief Sets the operator to be evaluated by the node.
188189
void set_operator( BinaryOperator op ) { m_port_in_op->set_default_value( op ); }
189190

190-
static const std::string& node_typename() {
191-
static std::string demangledName =
192-
std::string { "BinaryOp<" } + Ra::Core::Utils::simplifiedDemangledType<t_a>() + " x " +
193-
Ra::Core::Utils::simplifiedDemangledType<t_b>() + " -> " +
194-
Ra::Core::Utils::simplifiedDemangledType<t_result>() + ">";
195-
return demangledName;
196-
}
191+
RA_NODE_TYPENAME( std::string { "BinaryOp<" } +
192+
Ra::Core::Utils::simplifiedDemangledType<t_a>() + " x " +
193+
Ra::Core::Utils::simplifiedDemangledType<t_b>() + " -> " +
194+
Ra::Core::Utils::simplifiedDemangledType<t_result>() + ">" );
197195

198196
protected:
199197
BinaryOpNode( const std::string& instanceName,

src/Dataflow/Core/Functionals/FilterNode.hpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include "Dataflow/RaDataflow.hpp"
23
#include <Dataflow/Core/Node.hpp>
34

45
#include <functional>
@@ -46,7 +47,8 @@ class FilterNode : public Node
4647
* \param predicate
4748
*/
4849
FilterNode( const std::string& instanceName, UnaryPredicate predicate );
49-
50+
RA_NODE_TYPENAME( std::string { "Filter<" } +
51+
Ra::Core::Utils::simplifiedDemangledType<coll_t>() + ">" );
5052
void init() override;
5153
bool execute() override;
5254

@@ -67,9 +69,6 @@ class FilterNode : public Node
6769
RA_NODE_PORT_IN( coll_t, data );
6870
RA_NODE_PORT_IN( UnaryPredicate, predicate );
6971
RA_NODE_PORT_OUT_WITH_DATA( coll_t, result );
70-
71-
public:
72-
static const std::string& node_typename();
7372
};
7473

7574
// -----------------------------------------------------------------
@@ -105,13 +104,6 @@ bool FilterNode<coll_t, v_t>::execute() {
105104
return true;
106105
}
107106

108-
template <typename coll_t, typename v_t>
109-
const std::string& FilterNode<coll_t, v_t>::node_typename() {
110-
static std::string demangledName =
111-
std::string { "Filter<" } + Ra::Core::Utils::simplifiedDemangledType<coll_t>() + ">";
112-
return demangledName;
113-
}
114-
115107
template <typename coll_t, typename v_t>
116108
FilterNode<coll_t, v_t>::FilterNode( const std::string& instanceName,
117109
const std::string& typeName,

src/Dataflow/Core/Functionals/FunctionNode.hpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include "Dataflow/RaDataflow.hpp"
23
#include <Dataflow/Core/Node.hpp>
34

45
#include <functional>
@@ -22,7 +23,8 @@ class FunctionNode : public Node
2223

2324
void set_function( Function function ) { m_port_in_op->set_default_value( function ); }
2425

25-
static const std::string& node_typename();
26+
RA_NODE_TYPENAME( std::string { "Function<" } +
27+
Ra::Core::Utils::simplifiedDemangledType<Input>() + ">" );
2628

2729
protected:
2830
FunctionNode( const std::string& instanceName, const std::string& typeName, Function function );
@@ -54,13 +56,6 @@ bool FunctionNode<Input, Output>::execute() {
5456
return true;
5557
}
5658

57-
template <typename Input, typename Output>
58-
const 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-
6459
template <typename Input, typename Output>
6560
FunctionNode<Input, Output>::FunctionNode( const std::string& instanceName,
6661
const std::string& typeName,

src/Dataflow/Core/Functionals/ReduceNode.hpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include "Dataflow/RaDataflow.hpp"
23
#include <Dataflow/Core/Node.hpp>
34

45
#include <functional>
@@ -52,6 +53,9 @@ class ReduceNode : public Node
5253
*/
5354
ReduceNode( const std::string& instanceName, ReduceOperator op, v_t initialValue = v_t {} );
5455

56+
RA_NODE_TYPENAME( std::string { "Reduce<" } +
57+
Ra::Core::Utils::simplifiedDemangledType<coll_t>() + ">" );
58+
5559
void init() override;
5660
bool execute() override;
5761

@@ -74,9 +78,6 @@ class ReduceNode : public Node
7478
RA_NODE_PORT_IN( coll_t, data );
7579
RA_NODE_PORT_IN( ReduceOperator, op );
7680
RA_NODE_PORT_OUT_WITH_DATA( v_t, result );
77-
78-
public:
79-
static const std::string& node_typename();
8081
};
8182

8283
// -----------------------------------------------------------------
@@ -118,13 +119,6 @@ bool ReduceNode<coll_t, v_t>::execute() {
118119
return true;
119120
}
120121

121-
template <typename coll_t, typename v_t>
122-
const std::string& ReduceNode<coll_t, v_t>::node_typename() {
123-
static std::string demangledName =
124-
std::string { "Reduce<" } + Ra::Core::Utils::simplifiedDemangledType<coll_t>() + ">";
125-
return demangledName;
126-
}
127-
128122
template <typename coll_t, typename v_t>
129123
ReduceNode<coll_t, v_t>::ReduceNode( const std::string& instanceName,
130124
const std::string& typeName,

src/Dataflow/Core/Functionals/TransformNode.hpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include "Dataflow/RaDataflow.hpp"
23
#include <Core/Utils/TypesUtils.hpp>
34
#include <Dataflow/Core/Node.hpp>
45

@@ -37,7 +38,8 @@ class TransformNode : public Node
3738
* \param instanceName
3839
*/
3940
explicit TransformNode( const std::string& instanceName );
40-
41+
RA_NODE_TYPENAME( std::string { "Transform<" } +
42+
Ra::Core::Utils::simplifiedDemangledType<coll_t>() + ">" );
4143
/**
4244
* \brief Construct a transformer with the given operator
4345
* \param instanceName
@@ -65,9 +67,6 @@ class TransformNode : public Node
6567
RA_NODE_PORT_IN( coll_t, data );
6668
RA_NODE_PORT_IN( TransformOperator, op );
6769
RA_NODE_PORT_OUT_WITH_DATA( coll_t, result );
68-
69-
public:
70-
static const std::string& node_typename();
7170
};
7271

7372
// -----------------------------------------------------------------
@@ -104,13 +103,6 @@ bool TransformNode<coll_t, v_t>::execute() {
104103
return true;
105104
}
106105

107-
template <typename coll_t, typename v_t>
108-
const std::string& TransformNode<coll_t, v_t>::node_typename() {
109-
static std::string demangledName =
110-
std::string { "Transform<" } + Ra::Core::Utils::simplifiedDemangledType<coll_t>() + ">";
111-
return demangledName;
112-
}
113-
114106
template <typename coll_t, typename v_t>
115107
TransformNode<coll_t, v_t>::TransformNode( const std::string& instanceName,
116108
const std::string& typeName,

src/Dataflow/Core/GraphNodes.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Ra {
66
namespace Dataflow {
77
namespace Core {
8-
8+
namespace {
99
template <typename T>
1010
void make_port_helper(
1111
Node* n,
@@ -19,15 +19,12 @@ void make_port_helper(
1919
port_map[index] = ctor( n, name, type );
2020
}
2121
}
22+
} // namespace
2223

2324
// ---------------------------
2425
// GraphNode
2526
// ---------------------------
2627

27-
const std::string& GraphNode::node_typename() {
28-
static std ::string demangledName { "GraphNode" };
29-
return demangledName;
30-
}
3128
bool GraphNode::execute() {
3229
CORE_ASSERT( m_inputs.size() == m_outputs.size(), "GraphNode input and output size differ" );
3330

@@ -80,6 +77,7 @@ auto GraphNode::add_ports( PortBaseRawPtr port )
8077
}
8178
return std::make_tuple( PortIndex {}, PortIndex {}, in, out );
8279
}
80+
8381
bool GraphNode::fromJsonInternal( const nlohmann::json& data ) {
8482
auto factory = PortFactory::getInstance();
8583
std::map<size_t, PortBaseInPtr> inputs;
@@ -114,11 +112,6 @@ bool GraphNode::fromJsonInternal( const nlohmann::json& data ) {
114112
// GraphInputNode
115113
// ---------------------------
116114

117-
const std::string& GraphInputNode::node_typename() {
118-
static std::string demangledName { "GraphInputNode" };
119-
// std::cerr << " node typename\n";
120-
return demangledName;
121-
}
122115
auto GraphInputNode::add_output_port( PortBaseInRawPtr port ) -> PortIndex {
123116
auto [input_idx, output_idx, in, out] = add_ports( port );
124117
if ( in && out ) port->connect( out.get() );
@@ -129,16 +122,12 @@ auto GraphInputNode::add_output_port( PortBaseInRawPtr port ) -> PortIndex {
129122
// GraphOutputNode
130123
// ---------------------------
131124

132-
const std::string& GraphOutputNode::node_typename() {
133-
static std ::string demangledName { "GraphOutputNode" };
134-
return demangledName;
135-
}
136-
137125
auto GraphOutputNode::add_input_port( PortBaseOutRawPtr port ) -> PortIndex {
138126
auto [input_idx, output_idx, in, out] = add_ports( port );
139127
if ( in && out ) in->connect( port );
140128
return output_idx;
141129
}
130+
142131
} // namespace Core
143132
} // namespace Dataflow
144133
} // namespace Ra

src/Dataflow/Core/GraphNodes.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55
#include <Core/Types.hpp>
66
#include <Dataflow/Core/Node.hpp>
77

8-
#include <string>
9-
108
namespace Ra {
119
namespace Dataflow {
1210
namespace Core {
1311

1412
#define BASIC_NODE_INIT( TYPE, BASE ) \
1513
explicit TYPE( const std::string& name ) : TYPE( name, TYPE::node_typename() ) {} \
16-
static const std::string& node_typename(); \
14+
RA_NODE_TYPENAME( #TYPE ) \
1715
TYPE( const std::string& instanceName, const std::string& typeName ) : \
18-
BASE( instanceName, typeName )
16+
BASE( instanceName, typeName ) {}
1917

2018
class RA_DATAFLOW_CORE_API GraphNode : public Node
2119
{
2220
public:
23-
BASIC_NODE_INIT( GraphNode, Node ) {}
21+
BASIC_NODE_INIT( GraphNode, Node );
2422

2523
bool execute() override;
2624
void remove_unlinked_ports();
@@ -45,7 +43,8 @@ class RA_DATAFLOW_CORE_API GraphNode : public Node
4543
class RA_DATAFLOW_CORE_API GraphInputNode : public GraphNode
4644
{
4745
public:
48-
BASIC_NODE_INIT( GraphInputNode, GraphNode ) {}
46+
BASIC_NODE_INIT( GraphInputNode, GraphNode );
47+
4948
/**
5049
* Given an input port (from another node) add an output port to this GraphInputNode and
5150
* connect the newly created output port to input port in argument
@@ -56,7 +55,8 @@ class RA_DATAFLOW_CORE_API GraphInputNode : public GraphNode
5655
class RA_DATAFLOW_CORE_API GraphOutputNode : public GraphNode
5756
{
5857
public:
59-
BASIC_NODE_INIT( GraphOutputNode, GraphNode ) {}
58+
BASIC_NODE_INIT( GraphOutputNode, GraphNode );
59+
6060
/**
6161
* Given an output port (from another node) add an input port to this GraphOutputNode and
6262
* connect the newly created input port to output port in argument

src/Dataflow/Core/Node.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class NodeJsonDeserializer : public Ra::Core::DynamicVisitor
9999
* sent to their output ports.
100100
*
101101
* Derived class must implement bool execute() and static const std::string & node_typename()
102+
* use `RA_NODE_TYPENAME( "node type name" );` as an helper.
102103
*
103104
* static const std::string& node_typename() returns the demangled type name of the node or any
104105
* human readable representation of the type name. This is a public static member each node concrete

0 commit comments

Comments
 (0)