GrPPI  1.0
Generic and Reusable Parallel Pattern Interface
simple_node.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_SIMPLE_NODE_H
17 #define GRPPI_FF_DETAIL_SIMPLE_NODE_H
18 
19 #include "fastflow_allocator.h"
20 
21 #include <ff/allocator.hpp>
22 #include <ff/node.hpp>
23 
24 namespace grppi {
25 
26 namespace detail_ff {
27 
34 template <typename Input, typename Output, typename Transformer>
35 class node_impl : public ff::ff_node_t<Input,Output> {
36 public:
37 
38  node_impl(Transformer && transform_op) :
39  transform_op_{transform_op}
40  {}
41 
42  Output * svc(Input * p_item) {
43  return new (ff_arena) Output{transform_op_(*p_item)};
44  }
45 
46 private:
47  Transformer transform_op_;
48 };
49 
55 template <typename Output, typename Generator>
56 class node_impl<void,Output,Generator> : public ff::ff_node {
57 public:
58 
59  node_impl(Generator && generate_op) :
60  generate_op_{generate_op}
61  {}
62 
63  void * svc(void *) {
64  std::experimental::optional<Output> result{generate_op_()};
65  if (result) {
66  return new (ff_arena) Output{*result};
67  }
68  else {
69  return EOS;
70  }
71  }
72 
73 private:
74  Generator generate_op_;
75 };
76 
82 template <typename Input, typename Consumer>
83 class node_impl<Input,void,Consumer> : public ff::ff_node_t<Input,void> {
84 public:
85 
86  node_impl(Consumer && consume_op) :
87  consume_op_{consume_op}
88  {}
89 
90  void * svc(Input * p_item) {
91  consume_op_(*p_item);
92  operator delete(p_item, ff_arena);
93  return GO_ON;
94  }
95 
96 private:
97  Consumer consume_op_;
98 };
99 
100 
101 } // namespace detail_ff
102 
103 } // namespace grppi
104 
105 #endif
node_impl(Consumer &&consume_op)
Definition: simple_node.h:86
void * svc(Input *p_item)
Definition: simple_node.h:90
void * svc(void *)
Definition: simple_node.h:63
node_impl(Generator &&generate_op)
Definition: simple_node.h:59
Fastflow node for a pipeline transformation stage.
Definition: simple_node.h:35
node_impl(Transformer &&transform_op)
Definition: simple_node.h:38
Output * svc(Input *p_item)
Definition: simple_node.h:42
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
Definition: callable_traits.h:21