GrPPI  1.0
Generic and Reusable Parallel Pattern Interface
farm_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_FARM_PATTERN_H
17 #define GRPPI_COMMON_FARM_PATTERN_H
18 
19 #include <type_traits>
20 
21 namespace grppi {
22 
28 template <typename Transformer>
29 class farm_t {
30 public:
31 
32  using transformer_type = Transformer;
33 
39  farm_t(int n, Transformer && t) noexcept :
40  cardinality_{n}, transformer_{t}
41  {}
42 
47  int cardinality() const noexcept {
48  return cardinality_;
49  }
50 
55  Transformer & transformer(){
56  return transformer_;
57  }
61  template <typename I>
62  auto operator()(I && item) const {
63  return transformer_(std::forward<I>(item));
64  }
65 
66 private:
67  int cardinality_;
68  Transformer transformer_;
69 };
70 
71 namespace internal {
72 
73 template<typename T>
74 struct is_farm : std::false_type {};
75 
76 template<typename T>
77 struct is_farm<farm_t<T>> : std::true_type {};
78 
79 } // namespace internal
80 
81 template <typename T>
82 static constexpr bool is_farm = internal::is_farm<std::decay_t<T>>();
83 
84 template <typename T>
85 using requires_farm = typename std::enable_if_t<is_farm<T>, int>;
86 
87 }
88 
89 #endif
Representation of farm pattern. Represents a farm of n replicas from a transformer.
Definition: farm_pattern.h:29
auto operator()(I &&item) const
Invokes the transformer of the farm over a data item.
Definition: farm_pattern.h:62
int cardinality() const noexcept
Farm's cardinality or number of replicas.
Definition: farm_pattern.h:47
Transformer transformer_type
Definition: farm_pattern.h:32
farm_t(int n, Transformer &&t) noexcept
Constructs a farm with a cardinality and a transformer.
Definition: farm_pattern.h:39
Transformer & transformer()
Return the transformer function stored in the farm pattern.
Definition: farm_pattern.h:55
Definition: callable_traits.h:21
typename std::enable_if_t< is_farm< T >, int > requires_farm
Definition: farm_pattern.h:85
static constexpr bool is_farm
Definition: farm_pattern.h:82
Definition: farm_pattern.h:74