GrPPI  1.0
Generic and Reusable Parallel Pattern Interface
filter_nodes.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Universidad Carlos III de Madrid
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef GRPPI_FF_DETAIL_FILTER_NODES_H
17 #define GRPPI_FF_DETAIL_FILTER_NODES_H
18 
19 #include "fastflow_allocator.h"
20 
21 #include <ff/node.hpp>
22 
23 namespace grppi {
24 
25 namespace detail_ff {
26 
31 template <typename T>
32 constexpr T * filtered_value() {
33  static_assert(sizeof(std::size_t) == sizeof(std::uintptr_t),
34  "std::size_t and pointers have different sizes");
35  return reinterpret_cast<T*>(std::size_t(ff::FF_EOS - 0x11));
36 }
37 
42 template <typename Item, typename Predicate>
43 class filter_worker : public ff::ff_node_t<Item> {
44 public:
45  filter_worker(Predicate && predicate) :
46  predicate_{std::forward<Predicate>(predicate)}
47  {}
48 
49  Item * svc(Item * p_item) {
50  if (predicate_(*p_item)) {
51  return p_item;
52  }
53  else {
54  operator delete(p_item,ff_arena);
55  return filtered_value<Item>();
56  }
57  }
58 
59 private:
60  Predicate predicate_;
61 };
62 
66 template <typename Item>
67 class filter_collector : public ff::ff_node_t<Item> {
68 public:
69  filter_collector() = default;
70 
71  Item * svc(Item * p_item) {
72  if (p_item == filtered_value<Item>()) {
73  return this->GO_ON;
74  }
75  else {
76  return p_item;
77  }
78  }
79 };
80 
84 template <typename Item>
85 class filter_emitter : public ff::ff_node_t<Item> {
86 public:
87  filter_emitter() = default;
88 
89  Item * svc(Item * p_item) { return p_item; }
90 };
91 
92 
93 
94 } // namespace detail_ff
95 
96 } // namespace grppi
97 
98 #endif
Collector node for a filter.
Definition: filter_nodes.h:67
Item * svc(Item *p_item)
Definition: filter_nodes.h:71
Emitter for a filter stage.
Definition: filter_nodes.h:85
Item * svc(Item *p_item)
Definition: filter_nodes.h:89
Worker that passes a value to next stage if the predicate is satisfied or the filtered_value constant...
Definition: filter_nodes.h:43
filter_worker(Predicate &&predicate)
Definition: filter_nodes.h:45
Item * svc(Item *p_item)
Definition: filter_nodes.h:49
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:36
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:32
Definition: callable_traits.h:21