GrPPI  1.0
Generic and Reusable Parallel Pattern Interface
iteration_pattern.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_COMMON_ITERATION_PATTERN_H
17 #define GRPPI_COMMON_ITERATION_PATTERN_H
18 
19 #include <type_traits>
20 
21 namespace grppi {
22 
28 template <typename Transformer, typename Predicate>
29 class iteration_t {
30 public:
31 
36  iteration_t(Transformer && t, Predicate && p) noexcept :
37  transform_{t},
38  predicate_{p}
39  {}
40 
44  template <typename Item>
45  bool predicate(Item && item) const {
46  return predicate_(std::forward<Item>(item));
47  }
48 
52  template <typename Item>
53  auto transform(Item && item) const {
54  return transform_(std::forward<Item>(item));
55  }
56 
60  template<typename T>
61  auto operator()(T && item){
62  return transform(item);
63  }
64 
65 private:
66  Transformer transform_;
67  Predicate predicate_;
68 };
69 
70 namespace internal {
71 
72 template<typename T>
73 struct is_iteration : std::false_type {};
74 
75 template<typename T, typename P>
76 struct is_iteration<iteration_t<T,P>> : std::true_type {};
77 
78 } // namespace internal
79 
80 template <typename T>
82 
83 template <typename T>
84 using requires_iteration = typename std::enable_if_t<is_iteration<T>, int>;
85 
86 }
87 
88 #endif
Representation of iteration pattern. Represents a iteration that can be used as a stage on a pipeline...
Definition: iteration_pattern.h:29
auto transform(Item &&item) const
Applies the transformation over a data item.
Definition: iteration_pattern.h:53
bool predicate(Item &&item) const
Invokes the predicate of the iteration over a data item.
Definition: iteration_pattern.h:45
auto operator()(T &&item)
Applies the transformation over a data item.
Definition: iteration_pattern.h:61
iteration_t(Transformer &&t, Predicate &&p) noexcept
Constructs a iteration with a predicate.
Definition: iteration_pattern.h:36
Definition: callable_traits.h:21
typename std::enable_if_t< is_iteration< T >, int > requires_iteration
Definition: iteration_pattern.h:84
static constexpr bool is_iteration
Definition: iteration_pattern.h:81
Definition: iteration_pattern.h:73