GrPPI  1.0
Generic and Reusable Parallel Pattern Interface
pipeline_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_PIPELINE_PATTERN_H
17 #define GRPPI_COMMON_PIPELINE_PATTERN_H
18 
19 #include <type_traits>
20 
21 namespace grppi {
22 
29 template <typename ... Transformers>
30 class pipeline_t {
31 public:
32 
33  using transformers_type = std::tuple<Transformers...>;
34 
40  pipeline_t(Transformers && ... others) noexcept :
41  transformers_{others...}
42  {}
43 
44 
45 
46  template <std::size_t index, typename T, typename std::enable_if< index == (sizeof...(Transformers) -1) ,int>::type = 0 >
47  auto invoke_all(T item) const
48  {
49  return invoke<index>(item);
50  }
51 
52  template <std::size_t index, typename T, typename std::enable_if< index != (sizeof...(Transformers) -1) ,int>::type = 0 >
53  auto invoke_all(T item) const
54  {
55  return invoke_all<index+1>(invoke<index>(item));
56  }
57 
58  template <typename T>
59  auto operator()(T item) const{
60  return invoke_all<0>(item);
61  }
62 
67  template <std::size_t I, typename T>
68  auto invoke(T && item) const {
69  auto f = std::get<I>(transformers_);
70  return f(std::forward<T>(item));
71  }
72 
78  template <std::size_t I>
79  auto stage() const noexcept {
80  static_assert(I<sizeof...(Transformers),
81  "Pipeline has not so many transformers");
82  return std::get<I>(transformers_);
83  }
84 
85  auto transformers() const noexcept {
86  return transformers_;
87  }
88 
89 private:
90  std::tuple<Transformers...> transformers_;
91 };
92 
93 namespace internal {
94 
95 template<typename T>
96 struct is_pipeline : std::false_type {};
97 
98 template <typename ... T>
99 struct is_pipeline<pipeline_t<T...>> :std::true_type {};
100 
101 } // namespace internal
102 
103 template <typename T>
105 
106 template <typename T>
107 using requires_pipeline = typename std::enable_if_t<is_pipeline<T>, int>;
108 
109 namespace internal {
110 
111 template <typename I, typename T>
113  using type = std::decay_t<typename std::result_of<T(I)>::type>;
114 };
115 
116 template <typename I, typename T, typename ... U>
117 struct output_value_type<I,pipeline_t<T,U...>> {
118  using first_result = std::decay_t<typename std::result_of<T(I)>::type>;
119  using type = std::conditional_t<sizeof...(U)==0,
120  first_result,
122  >;
123 };
124 
125 }
126 
127 template <typename I, typename T>
129 
130 }
131 
132 #endif
Representation of pipeline pattern. Represents a pipeline with multiple chained transformers.
Definition: pipeline_pattern.h:30
auto invoke(T &&item) const
Invokes a trasnformer from the pipeline.
Definition: pipeline_pattern.h:68
auto invoke_all(T item) const
Definition: pipeline_pattern.h:47
auto transformers() const noexcept
Definition: pipeline_pattern.h:85
auto stage() const noexcept
Gets a transformer from the pipeline.
Definition: pipeline_pattern.h:79
auto operator()(T item) const
Definition: pipeline_pattern.h:59
pipeline_t(Transformers &&... others) noexcept
Constructs a pipeline with several transformers.
Definition: pipeline_pattern.h:40
std::tuple< Transformers... > transformers_type
Definition: pipeline_pattern.h:33
Definition: callable_traits.h:21
typename std::enable_if_t< is_pipeline< T >, int > requires_pipeline
Definition: pipeline_pattern.h:107
typename internal::output_value_type< I, T >::type output_value_type
Definition: pipeline_pattern.h:128
static constexpr bool is_pipeline
Definition: pipeline_pattern.h:104
Definition: pipeline_pattern.h:96
std::decay_t< typename std::result_of< T(I)>::type > first_result
Definition: pipeline_pattern.h:118
std::conditional_t< sizeof...(U)==0, first_result, typename output_value_type< first_result, U... >::type > type
Definition: pipeline_pattern.h:122
Definition: pipeline_pattern.h:112
std::decay_t< typename std::result_of< T(I)>::type > type
Definition: pipeline_pattern.h:113