GrPPI  0.3.1
Generic and Reusable Parallel Pattern Interface
filter_nodes.h
Go to the documentation of this file.
1 
21 #ifndef GRPPI_FF_DETAIL_FILTER_NODES_H
22 #define GRPPI_FF_DETAIL_FILTER_NODES_H
23 
24 #include "fastflow_allocator.h"
25 
26 #include <ff/node.hpp>
27 
28 namespace grppi {
29 
30 namespace detail_ff {
31 
36 template <typename T>
37 constexpr T * filtered_value() {
38  static_assert(sizeof(std::size_t) == sizeof(std::uintptr_t));
39  return reinterpret_cast<T*>(std::size_t(ff::FF_EOS - 0x11));
40 }
41 
46 template <typename Item, typename Predicate>
47 class filter_worker : public ff::ff_node_t<Item> {
48 public:
49  filter_worker(Predicate && predicate) :
50  predicate_{std::forward<Predicate>(predicate)}
51  {}
52 
53  Item * svc(Item * p_item) {
54  if (predicate_(*p_item)) {
55  return p_item;
56  }
57  else {
58  operator delete(p_item,ff_arena);
59  return filtered_value<Item>();
60  }
61  }
62 
63 private:
64  Predicate predicate_;
65 };
66 
70 template <typename Item>
71 class filter_collector : public ff::ff_node_t<Item> {
72 public:
73  filter_collector() = default;
74 
75  Item * svc(Item * p_item) {
76  if (p_item == filtered_value<Item>()) {
77  return this->GO_ON;
78  }
79  else {
80  return p_item;
81  }
82  }
83 };
84 
88 template <typename Item>
89 class filter_emitter : public ff::ff_node_t<Item> {
90 public:
91  filter_emitter() = default;
92 
93  Item * svc(Item * p_item) { return p_item; }
94 };
95 
96 
97 
98 } // namespace detail_ff
99 
100 } // namespace grppi
101 
102 #endif
Definition: callable_traits.h:26
Worker that passes a value to next stage if the predicate is satisfied or the filtered_value constant...
Definition: filter_nodes.h:47
constexpr ff_arena_t ff_arena
Fastflow arena object. This object will be passed to placement new/delete to use FastFlow allocation ...
Definition: fastflow_allocator.h:41
Item * svc(Item *p_item)
Definition: filter_nodes.h:93
Item * svc(Item *p_item)
Definition: filter_nodes.h:75
Emitter for a filter stage.
Definition: filter_nodes.h:89
Item * svc(Item *p_item)
Definition: filter_nodes.h:53
filter_worker(Predicate &&predicate)
Definition: filter_nodes.h:49
Colletor node for a filter.
Definition: filter_nodes.h:71
constexpr T * filtered_value()
Get a pointer representation of the filter constant to be used as special value when a value is filte...
Definition: filter_nodes.h:37