GrPPI  0.2
Generic and Reusable Parallel Pattern Interface
polymorphic_execution.h
Go to the documentation of this file.
1 
21 #ifndef GRPPI_POLY_POLYMORPHIC_EXECUTION_H
22 #define GRPPI_POLY_POLYMORPHIC_EXECUTION_H
23 
24 #include "../seq/sequential_execution.h"
25 #include "../native/parallel_execution_native.h"
26 #include "../tbb/parallel_execution_tbb.h"
27 #include "../omp/parallel_execution_omp.h"
28 
29 #include <typeinfo>
30 #include <memory>
31 
32 namespace grppi{
33 
35 template <typename E>
36 constexpr bool is_execution_policy() {
37  return is_sequential_execution<E>()
38  || is_parallel_execution_native<E>()
39  || is_parallel_execution_tbb<E>()
40  || is_parallel_execution_omp<E>()
41  ;
42 }
43 
45 template <typename E>
47  std::enable_if_t<is_execution_policy<E>(), int>;
48 
50 template <typename E>
52  std::enable_if_t<!is_execution_policy<E>(), int>;
53 
54 // Forward declare polymorphic execution
56 
57 // Forward declare make execution for polymorphic execution
58 template <typename E>
60 
64 public:
65 
67  polymorphic_execution() noexcept :
68  execution_{},
69  execution_type_{nullptr}
70  {}
71 
73  bool has_execution() const noexcept { return execution_.get() != nullptr; }
74 
77  const std::type_info & type() const noexcept {
78  return *execution_type_;
79  }
80 
81  // Check if the current execution is of type E
82  template <typename E>
83  bool is_execution() const {
84  if (!has_execution()) return false;
85  return typeid(E) == *execution_type_;
86  }
87 
89  template <typename E,
91  E * execution_ptr() {
92  if (!has_execution()) return nullptr;
93  if (*execution_type_ != typeid(E)) return nullptr;
94  return static_cast<E*>(execution_.get());
95  }
96 
98  template <typename E,
101  return nullptr;
102  }
103 
104 private:
106  std::shared_ptr<void> execution_;
107 
109  const std::type_info * execution_type_;
110 
111 private:
112 
116  template <typename E>
119  if (!is_supported<E>()) return e;
120  e.execution_ = std::make_shared<E>();
121  e.execution_type_ = &typeid(E);
122  return e;
123  }
124 
125 };
126 
127 } // end namespace grppi
128 
129 #endif
Definition: callable_traits.h:24
friend polymorphic_execution make_polymorphic_execution()
Definition: polymorphic_execution.h:117
polymorphic_execution make_polymorphic_execution()
Definition: polymorphic_execution.h:117
bool is_execution() const
Definition: polymorphic_execution.h:83
const std::type_info & type() const noexcept
Definition: polymorphic_execution.h:77
std::enable_if_t< is_execution_policy< E >(), int > requires_execution_policy
Simulate concept requirement for being an execution policy.
Definition: polymorphic_execution.h:47
constexpr bool is_execution_policy()
Meta-function to determine if a type is an execution policy.
Definition: polymorphic_execution.h:36
bool has_execution() const noexcept
Determine if there is an execution stored.
Definition: polymorphic_execution.h:73
E * execution_ptr()
Get the execution pointer for a given type.
Definition: polymorphic_execution.h:91
Definition: polymorphic_execution.h:63
polymorphic_execution() noexcept
Create empty polymorphic execution.
Definition: polymorphic_execution.h:67
std::enable_if_t<!is_execution_policy< E >(), int > requires_not_execution_policy
Simulate concept requirement for not being an execution policy.
Definition: polymorphic_execution.h:52