Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.5.28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FilterNode.hpp
1#pragma once
2#include <Dataflow/Core/Node.hpp>
3
4#include <functional>
5
6namespace Ra {
7namespace Dataflow {
8namespace Core {
9namespace Functionals {
10
28template <typename coll_t, typename v_t = typename coll_t::value_type>
29class FilterNode : public Node
30{
31 public:
35 using UnaryPredicate = std::function<bool( const v_t& )>;
36
41 explicit FilterNode( const std::string& instanceName );
42
48 FilterNode( const std::string& instanceName, UnaryPredicate predicate );
49
50 void init() override;
51 bool execute() override;
52
54 void set_predicate( UnaryPredicate predicate );
55
56 protected:
57 FilterNode( const std::string& instanceName,
58 const std::string& typeName,
59 UnaryPredicate predicate );
60
61 void toJsonInternal( nlohmann::json& data ) const override { Node::toJsonInternal( data ); }
62 bool fromJsonInternal( const nlohmann::json& data ) override {
63 return Node::fromJsonInternal( data );
64 }
65
66 private:
67 RA_NODE_PORT_IN( coll_t, data );
68 RA_NODE_PORT_IN( UnaryPredicate, predicate );
69 RA_NODE_PORT_OUT_WITH_DATA( coll_t, result );
70
71 public:
72 static const std::string& node_typename();
73};
74
75// -----------------------------------------------------------------
76// ---------------------- inline methods ---------------------------
77
78template <typename coll_t, typename v_t>
80 FilterNode( instanceName, node_typename(), []( v_t ) { return true; } ) {}
81
82template <typename coll_t, typename v_t>
84 FilterNode( instanceName, node_typename(), predicate ) {}
85
86template <typename coll_t, typename v_t>
88 m_port_in_predicate->set_default_value( predicate );
89}
90
91template <typename coll_t, typename v_t>
93 Node::init();
94 m_result.clear();
95}
96
97template <typename coll_t, typename v_t>
99 const auto& f = m_port_in_predicate->data();
100 const auto& inData = m_port_in_data->data();
101 m_result.clear();
102 // since we do not know how many inData respect the predicate, do not reserve m_result
103 std::copy_if( inData.begin(), inData.end(), std::back_inserter( m_result ), f );
104
105 return true;
106}
107
108template <typename coll_t, typename v_t>
110 static std::string demangledName =
111 std::string { "Filter<" } + Ra::Core::Utils::simplifiedDemangledType<coll_t>() + ">";
112 return demangledName;
113}
114
115template <typename coll_t, typename v_t>
117 const std::string& typeName,
118 UnaryPredicate predicate ) :
119 Node( instanceName, typeName ) {
120 m_port_in_predicate->set_default_value( predicate );
121}
122
123} // namespace Functionals
124} // namespace Core
125} // namespace Dataflow
126} // namespace Ra
T back_inserter(T... args)
Filter on iterable collection.
bool fromJsonInternal(const nlohmann::json &data) override
Internal json representation of the Node.
bool execute() override
Executes the node.
FilterNode(const std::string &instanceName)
Construct a filter accepting all its input ( true() lambda )
void toJsonInternal(nlohmann::json &data) const override
Internal json representation of the Node.
void set_predicate(UnaryPredicate predicate)
Sets the filtering predicate on the node.
void init() override
Initializes the node content.
std::function< bool(const v_t &)> UnaryPredicate
Base abstract class for all the nodes added and used by the node system.
Definition Node.hpp:40
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
T copy_if(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:4