GrPPI  1.0
Generic and Reusable Parallel Pattern Interface
common/context.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_CONTEXT_H
17 #define GRPPI_COMMON_CONTEXT_H
18 
19 #include <type_traits>
20 
21 namespace grppi {
22 
31 template <typename ExecutionPolicy, typename Transformer>
32 class context_t {
33 public:
34 
35  using transformer_type = Transformer;
36  using execution_policy_type = ExecutionPolicy;
37 
43  context_t(ExecutionPolicy & e, Transformer && t) noexcept :
44  execution_policy_{e}, transformer_{t}
45  {}
46 
51  ExecutionPolicy & execution_policy(){
52  return execution_policy_;
53  }
54 
55 
60  Transformer & transformer(){
61  return transformer_;
62  }
63 
67  template <typename I>
68  auto operator()(I && item) const {
69  return transformer_(std::forward<I>(item));
70  }
71 
72 private:
73  ExecutionPolicy& execution_policy_;
74  Transformer transformer_;
75 };
76 
77 namespace internal {
78 
79 template<typename T>
80 struct is_context : std::false_type {};
81 
82 template<typename E, typename T>
83 struct is_context<context_t<E,T>> : std::true_type {};
84 
85 } // namespace internal
86 
87 template <typename T>
89 
90 template <typename T>
91 using requires_context = typename std::enable_if_t<is_context<T>, int>;
92 
93 }
94 
95 #endif
Representation of a context pattern. Represents a context that uses a given policy to run a transform...
Definition: common/context.h:32
ExecutionPolicy execution_policy_type
Definition: common/context.h:36
Transformer transformer_type
Definition: common/context.h:35
context_t(ExecutionPolicy &e, Transformer &&t) noexcept
Constructs a context with a execution policy and a transformer.
Definition: common/context.h:43
auto operator()(I &&item) const
Invokes the transformer of the context over a data item.
Definition: common/context.h:68
ExecutionPolicy & execution_policy()
Return the execution policy used in the context.
Definition: common/context.h:51
Transformer & transformer()
Return the transformer function.
Definition: common/context.h:60
Definition: callable_traits.h:21
static constexpr bool is_context
Definition: common/context.h:88
typename std::enable_if_t< is_context< T >, int > requires_context
Definition: common/context.h:91
Definition: common/context.h:80